//
//  SettingsDetailsCell.swift
//  1Weather
//
//  Created by Dmitry Stepanets on 27.03.2021.
//

import UIKit

class SettingsDetailsCell: UITableViewCell {
    //Private
    private let container = UIView()
    private let label = UILabel()
    private let checkmarkImageView = UIImageView()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        prepareCell()
        prepareContainer()
        prepareLabel()
        prepareCheckmark()
        updateUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        updateUI()
    }
    
    func configure(item:String, isSelected:Bool, roundedConrers:CACornerMask) {
        if roundedConrers.isEmpty {
            self.container.layer.cornerRadius = 0
        }
        else {
            self.container.layer.cornerRadius = 12
            self.container.layer.maskedCorners = roundedConrers
        }
        label.text = item
        checkmarkImageView.isHidden = !isSelected
    }
    
    private func updateUI() {
        contentView.backgroundColor = ThemeManager.currentTheme.baseBackgroundColor
        container.backgroundColor = ThemeManager.currentTheme.containerBackgroundColor
        
        switch interfaceStyle {
        case .light:
            label.textColor = ThemeManager.currentTheme.secondaryTextColor
        case .dark:
            label.textColor = ThemeManager.currentTheme.primaryTextColor
        }
    }
}

//MARK:- Prepare
private extension SettingsDetailsCell {
    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 prepareLabel() {
        label.font = AppFont.SFPro.regular(size: 16)
        label.textAlignment = .left
        container.addSubview(label)
        
        label.snp.makeConstraints { (make) in
            make.left.equalToSuperview().inset(18)
            make.centerY.equalToSuperview()
            make.top.bottom.equalToSuperview().inset(18)
        }
    }
    
    func prepareCheckmark() {
        checkmarkImageView.contentMode = .scaleAspectFit
        checkmarkImageView.image = UIImage(named: "checkmark")
        checkmarkImageView.tintColor = ThemeManager.currentTheme.graphTintColor
        container.addSubview(checkmarkImageView)
        
        checkmarkImageView.snp.makeConstraints { (make) in
            make.width.height.equalTo(12)
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().inset(30)
        }
    }
}
