//
//  SettingsCell.swift
//  1Weather
//
//  Created by Dmitry Stepanets on 26.03.2021.
//

import UIKit

class SettingsCell: UITableViewCell {
    //Private
    private let container = UIView()
    private let settingsNameLabel = UILabel()
    private let selectedValueLabel = UILabel()
    private let arrowLabel = UILabel()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        prepareCell()
        prepareContainer()
        prepareLabels()
        updateUI()
    }
    
    func configure(settingsName:String, selectedValue:String, roundCorners:CACornerMask) {
        settingsNameLabel.text = settingsName
        selectedValueLabel.text = selectedValue
        
        if roundCorners.isEmpty {
            container.layer.cornerRadius = 0
        }
        else {
            container.layer.cornerRadius = 12
            container.layer.maskedCorners = roundCorners
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        updateUI()
    }
    
    private func updateUI() {
        contentView.backgroundColor = ThemeManager.currentTheme.baseBackgroundColor
        container.backgroundColor = ThemeManager.currentTheme.containerBackgroundColor
        
        switch interfaceStyle {
        case .light:
            settingsNameLabel.textColor = ThemeManager.currentTheme.secondaryTextColor
            selectedValueLabel.textColor = ThemeManager.currentTheme.secondaryTextColor
            arrowLabel.textColor = ThemeManager.currentTheme.secondaryTextColor
        case .dark:
            settingsNameLabel.textColor = ThemeManager.currentTheme.primaryTextColor
            selectedValueLabel.textColor = ThemeManager.currentTheme.primaryTextColor
            arrowLabel.textColor = ThemeManager.currentTheme.primaryTextColor
        }
    }
}


//MARK:- Prepare
private extension SettingsCell {
    func prepareCell() {
        selectionStyle = .none
    }
    
    func prepareContainer() {
        contentView.addSubview(container)
        
        container.snp.makeConstraints { (make) in
            make.top.bottom.equalToSuperview()
            make.left.right.equalToSuperview().inset(18)
        }
    }
    
    func prepareLabels() {
        settingsNameLabel.font = AppFont.SFPro.bold(size: 16)
        settingsNameLabel.textAlignment = .left
        container.addSubview(settingsNameLabel)
        
        selectedValueLabel.font = AppFont.SFPro.regular(size: 16)
        selectedValueLabel.textAlignment = .right
        container.addSubview(selectedValueLabel)
        
        arrowLabel.font = AppFont.SFPro.regular(size: 18)
        arrowLabel.textAlignment = .right
        arrowLabel.text = "􀆊"
        container.addSubview(arrowLabel)
        
        //Constraints
        settingsNameLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().inset(18)
            make.centerY.equalToSuperview()
            make.top.bottom.equalToSuperview().inset(18)
        }
        
        arrowLabel.snp.makeConstraints { (make) in
            make.right.equalToSuperview().inset(18)
            make.centerY.equalToSuperview()
        }
        
        selectedValueLabel.snp.makeConstraints { (make) in
            make.right.equalTo(arrowLabel.snp.left).inset(-8)
            make.centerY.equalToSuperview()
        }
    }
}
