//
//  MenuCell.swift
//  1Weather
//
//  Created by Dmitry Stepanets on 25.03.2021.
//

import UIKit

class MenuCell: UITableViewCell {
    //Private
    private let container = UIView()
    private let typeImageView = UIImageView()
    private let typeLabel = UILabel()
    private let arrowLabel = UILabel()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        prepareCell()
        prepareContainer()
        prepareImageView()
        prepareLabel()
        prepareArrowLabel()
        updateUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        updateUI()
    }
    
    //Public
    public func configure(image:UIImage?, text:String, roundendCorners:CACornerMask) {
        self.typeImageView.image = image
        self.typeLabel.text = text
        
        if roundendCorners.isEmpty {
            self.container.layer.cornerRadius = 0
            self.container.layer.maskedCorners = []
        }
        else {
            self.container.layer.cornerRadius = 12
            self.container.layer.maskedCorners = roundendCorners
        }
    }
    
    //Private
    private func updateUI() {
        contentView.backgroundColor = ThemeManager.currentTheme.baseBackgroundColor
        container.backgroundColor = ThemeManager.currentTheme.containerBackgroundColor
        
        switch interfaceStyle {
        case .light:
            arrowLabel.textColor = ThemeManager.currentTheme.secondaryTextColor
            typeLabel.textColor = ThemeManager.currentTheme.secondaryTextColor
            typeImageView.tintColor = ThemeManager.currentTheme.secondaryTextColor
            self.tintColor = ThemeManager.currentTheme.secondaryTextColor
        case .dark:
            arrowLabel.textColor = ThemeManager.currentTheme.primaryTextColor
            typeLabel.textColor = ThemeManager.currentTheme.primaryTextColor
            typeImageView.tintColor = ThemeManager.currentTheme.primaryTextColor
            self.tintColor = ThemeManager.currentTheme.primaryTextColor
        }
    }
}

//MARK:- Prepare
private extension MenuCell {
    func prepareCell() {
        selectionStyle = .none
    }
    
    func prepareContainer() {
        contentView.addSubview(container)
        container.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview().inset(18)
            make.top.bottom.equalToSuperview()
        }
    }
    
    func prepareImageView() {
        typeImageView.contentMode = .scaleAspectFit
        container.addSubview(typeImageView)
        
        typeImageView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().inset(15)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(17)
        }
    }
    
    func prepareLabel() {
        typeLabel.font = AppFont.SFPro.regular(size: 16)
        typeLabel.textAlignment = .left
        container.addSubview(typeLabel)
        
        typeLabel.snp.makeConstraints { (make) in
            make.left.equalTo(typeImageView.snp.right).offset(15)
            make.centerY.equalToSuperview()
        }
    }
    
    func prepareArrowLabel() {
        arrowLabel.text = "􀆊"
        arrowLabel.font = AppFont.SFPro.regular(size: 16)
        arrowLabel.textAlignment = .right
        container.addSubview(arrowLabel)
        
        arrowLabel.snp.makeConstraints { (make) in
            make.right.equalToSuperview().inset(18)
            make.centerY.equalToSuperview()
        }
    }
}
