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

import UIKit

class SettingsThemeCell: UITableViewCell {
    //Private
    private let container = UIView()
    private let checkmarkSelectedImage = UIImage(named: "checkmark_selected")
    private let checkmarkUnselectedImage = UIImage(named: "checkmark_unselected")
    private let lightThemeImageView = UIImageView()
    private let lightThemeLabel = UILabel()
    private let lightThemeButton = UIButton()
    
    private let darkThemeImageView = UIImageView()
    private let darkThemeLabel = UILabel()
    private let darkThemeButton = UIButton()
    
    private let separatorView = UIView()
    
    private let automaticThemeLabel = UILabel()
    private let automaticDescriptionLabel = UILabel()
    private let automaticSwitchButton = UISwitch()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        prepareCell()
        prepareContainer()
        prepareLightTheme()
        prepareDarkTheme()
        prepareSeparator()
        prepareAutomaticViews()
        updateUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        updateUI()
    }
    
    //Private
    private func updateUI() {
        contentView.backgroundColor = ThemeManager.currentTheme.baseBackgroundColor
        container.backgroundColor = ThemeManager.currentTheme.containerBackgroundColor
        configure(settings: Settings.shared)
        
        switch interfaceStyle {
        case .light:
            lightThemeLabel.textColor = ThemeManager.currentTheme.secondaryTextColor
            automaticThemeLabel.textColor = ThemeManager.currentTheme.secondaryTextColor
            automaticDescriptionLabel.textColor = ThemeManager.currentTheme.secondaryTextColor
        case .dark:
            lightThemeLabel.textColor = ThemeManager.currentTheme.primaryTextColor
            automaticThemeLabel.textColor = ThemeManager.currentTheme.primaryTextColor
            automaticDescriptionLabel.textColor = ThemeManager.currentTheme.primaryTextColor
        }
    }
    
    @objc private func handleThemeButton(button: UIButton) {
        switch button {
        case lightThemeButton:
            Settings.shared.appTheme = .light
        case darkThemeButton:
            Settings.shared.appTheme = .dark
        default:
            break
        }
    }
    
    @objc private func handleAutomaticSwith() {
        Settings.shared.appTheme = .system
    }
    
    //Public
    public func configure(settings: Settings) {
        lightThemeButton.setImage(interfaceStyle == .light ? checkmarkSelectedImage : checkmarkUnselectedImage, for: .normal)
        darkThemeButton.setImage(interfaceStyle == .dark ? checkmarkSelectedImage : checkmarkUnselectedImage, for: .normal)
        automaticSwitchButton.isOn = settings.appTheme == .system
    }
}

//Private
private extension SettingsThemeCell {
    func prepareCell() {
        selectionStyle = .none
    }
    
    func prepareContainer() {
        container.layer.cornerRadius = 12
        contentView.addSubview(container)
        
        container.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview().inset(18)
            make.top.bottom.equalToSuperview().inset(8)
        }
    }
    
    func prepareLightTheme() {
        lightThemeImageView.clipsToBounds = true
        lightThemeImageView.layer.cornerRadius = 10
        lightThemeImageView.contentMode = .scaleAspectFill
        lightThemeImageView.image = UIImage(named: "theme_sample_light")
        container.addSubview(lightThemeImageView)
        
        lightThemeLabel.textAlignment = .center
        lightThemeLabel.setContentHuggingPriority(.fittingSizeLevel, for: .vertical)
        lightThemeLabel.font = AppFont.SFPro.regular(size: 16)
        lightThemeLabel.text = "settings.theme.light".localized()
        container.addSubview(lightThemeLabel)
        
        lightThemeButton.addTarget(self, action: #selector(handleThemeButton(button:)), for: .touchUpInside)
        container.addSubview(lightThemeButton)
        
        //Constraints
        lightThemeImageView.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 85, height: 154))
            make.top.equalToSuperview().inset(24)
            make.left.equalToSuperview().inset(57)
        }
        
        lightThemeLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(lightThemeImageView)
            make.top.equalTo(lightThemeImageView.snp.bottom).offset(8)
            
        }
        
        lightThemeButton.snp.makeConstraints { (make) in
            make.width.height.equalTo(22)
            make.centerX.equalTo(lightThemeImageView)
            make.top.equalTo(lightThemeLabel.snp.bottom).offset(6)
        }
    }
    
    func prepareDarkTheme() {
        darkThemeImageView.layer.cornerRadius = 10
        darkThemeImageView.clipsToBounds = true
        darkThemeImageView.contentMode = .scaleAspectFill
        darkThemeImageView.image = UIImage(named: "theme_sample_dark")
        container.addSubview(darkThemeImageView)
        
        darkThemeLabel.textAlignment = .center
        darkThemeLabel.font = AppFont.SFPro.regular(size: 16)
        darkThemeLabel.text = "settings.theme.dark".localized()
        darkThemeLabel.setContentHuggingPriority(.fittingSizeLevel, for: .vertical)
        container.addSubview(darkThemeLabel)
        
        darkThemeButton.addTarget(self, action: #selector(handleThemeButton(button:)), for: .touchUpInside)
        container.addSubview(darkThemeButton)
        
        //Constraints
        darkThemeImageView.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 85, height: 154))
            make.top.equalToSuperview().inset(24)
            make.right.equalToSuperview().inset(57)
        }
        
        darkThemeLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(darkThemeImageView)
            make.top.equalTo(darkThemeImageView.snp.bottom).offset(8)
        }
        
        darkThemeButton.snp.makeConstraints { (make) in
            make.width.height.equalTo(22)
            make.centerX.equalTo(darkThemeImageView)
            make.top.equalTo(darkThemeLabel.snp.bottom).offset(6)
        }
    }
    
    func prepareSeparator() {
        separatorView.backgroundColor = UIColor(hex: 0x979797)
        container.addSubview(separatorView)
        
        separatorView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview().inset(30)
            make.height.equalTo(1)
            make.top.equalTo(lightThemeButton.snp.bottom).offset(16)
        }
    }
    
    func prepareAutomaticViews() {
        automaticThemeLabel.font = AppFont.SFPro.regular(size: 18)
        automaticThemeLabel.text = "settings.theme.automatic".localized()
        container.addSubview(automaticThemeLabel)
        
        automaticDescriptionLabel.font = AppFont.SFPro.regular(size: 12)
        automaticDescriptionLabel.text = "settings.theme.automaticDesc".localized()
        automaticDescriptionLabel.numberOfLines = 0
        automaticDescriptionLabel.lineBreakMode = .byWordWrapping
        automaticDescriptionLabel.setContentCompressionResistancePriority(.init(rawValue: 249), for: .horizontal)
        container.addSubview(automaticDescriptionLabel)
        
        automaticSwitchButton.onTintColor = ThemeManager.currentTheme.graphTintColor
        automaticSwitchButton.isOn = false
        automaticSwitchButton.addTarget(self, action: #selector(handleAutomaticSwith), for: .valueChanged)
        container.addSubview(automaticSwitchButton)
        
        //Constraints
        automaticThemeLabel.snp.makeConstraints { (make) in
            make.top.equalTo(separatorView.snp.bottom).offset(23)
            make.left.equalTo(separatorView)
        }
        
        automaticSwitchButton.snp.makeConstraints { (make) in
            make.centerY.equalTo(automaticThemeLabel)
            make.right.equalToSuperview().inset(18)
        }
        
        automaticDescriptionLabel.snp.makeConstraints { (make) in
            make.left.equalTo(automaticThemeLabel)
            make.top.equalTo(automaticThemeLabel.snp.bottom).inset(-2)
            make.right.equalTo(automaticSwitchButton.snp.left).offset(-25)
            make.bottom.equalToSuperview().inset(24)
        }
    }
}
