//
//  ForecastInfoCell.swift
//  1Weather
//
//  Created by Dmitry Stepanets on 17.03.2021.
//

import UIKit

class ForecastInfoCell: UITableViewCell {
    //Private
    private let topContainer = UIView()
    private let tempLabel = UILabel()
    private let weatherTypeLabel = UILabel()
    private let weatherDescriptionLabel = UILabel()
    private let weatherTypeImageView = UIImageView()
    private let conditionsStackView = UIStackView()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        prepareCell()
        prepareTopContainer()
        prepareWeatherViews()
        prepareConditionsStackView()
        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
        topContainer.backgroundColor = ThemeManager.currentTheme.containerBackgroundColor
        tempLabel.textColor = ThemeManager.currentTheme.primaryTextColor
        weatherTypeLabel.textColor = ThemeManager.currentTheme.primaryTextColor
        weatherDescriptionLabel.textColor = ThemeManager.currentTheme.secondaryTextColor
    }
    
    //Public
    public func configure(dailyWeather: DailyWeather) {
        weatherTypeImageView.image = dailyWeather.type.image(isDay: true)
        let maxTemp = dailyWeather.maxTemp?.shortString ?? "--"
        let minTemp = dailyWeather.minTemp?.shortString ?? "--"
        tempLabel.text = "\(maxTemp)/\(minTemp)"
        weatherTypeLabel.text = dailyWeather.type.localized(isDay: true)
        weatherDescriptionLabel.text = "Feels like --"
        
        //Conditions
        conditionsStackView.removeAll()
        
        let availableConditions:[WeatherConditionType] = [.precipitation, .uvIndex, .wind]
        let rowsCount = (CGFloat(WeatherConditionType.allCases.count) / 3).rounded(.up)
        var currentRow = -1
        var currentConditionsRowStackView = UIStackView()
        
        for (index, type) in availableConditions.enumerated() {
            if currentRow != index / Int(rowsCount) {
                currentConditionsRowStackView = UIStackView()
                currentConditionsRowStackView.axis = .horizontal
                currentConditionsRowStackView.distribution = .equalSpacing
                conditionsStackView.addArrangedSubview(currentConditionsRowStackView)
            }
            
            let conditionView = ForecastConditionView(type: type)
            conditionView.configure(dailyWeather: dailyWeather)
            currentConditionsRowStackView.addArrangedSubview(conditionView)
            currentRow = index / Int(rowsCount)
        }
        
        conditionsStackView.layoutIfNeeded()
    }
}

//MARK:- Prepare
private extension ForecastInfoCell {
    func prepareCell() {
        selectionStyle = .none
    }
    
    func prepareTopContainer() {
        topContainer.backgroundColor = .white
        topContainer.layer.cornerRadius = 15
        contentView.addSubview(topContainer)
        
        topContainer.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview().inset(16)
            make.top.equalToSuperview().inset(18)
        }
    }
    
    func prepareWeatherViews() {
        tempLabel.font = AppFont.SFPro.bold(size: 44)
        tempLabel.setContentHuggingPriority(.fittingSizeLevel, for: .vertical)
        topContainer.addSubview(tempLabel)
        
        weatherTypeLabel.font = AppFont.SFPro.bold(size: 16)
        topContainer.addSubview(weatherTypeLabel)
        
        weatherDescriptionLabel.font = AppFont.SFPro.regular(size: 12)
        topContainer.addSubview(weatherDescriptionLabel)
        
        weatherTypeImageView.contentMode = .scaleAspectFit
        weatherTypeImageView.clipsToBounds = true
        topContainer.addSubview(weatherTypeImageView)
        
        //Constraints
        tempLabel.snp.makeConstraints { (make) in
            make.top.equalToSuperview().inset(25)
            make.left.equalToSuperview().inset(18)
        }
        
        weatherTypeLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().inset(18)
            make.top.equalTo(tempLabel.snp.bottom).offset(10)
        }
        
        weatherDescriptionLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().inset(18)
            make.top.equalTo(weatherTypeLabel.snp.bottom).offset(5)
            make.bottom.equalToSuperview().inset(27)
        }
        
        weatherTypeImageView.snp.makeConstraints { (make) in
            make.width.height.equalTo(90)
            make.top.equalToSuperview().inset(30)
            make.right.equalToSuperview().inset(28)
        }
    }
    
    func prepareConditionsStackView() {
        conditionsStackView.axis = .vertical
        conditionsStackView.alignment = .center
        conditionsStackView.distribution = .fillEqually
        conditionsStackView.spacing = 54
        conditionsStackView.setContentCompressionResistancePriority(.init(rawValue: 751), for: .vertical)
        contentView.addSubview(conditionsStackView)
        
        conditionsStackView.snp.makeConstraints { (make) in
            make.top.equalTo(topContainer.snp.bottom).offset(15)
            make.bottom.equalToSuperview().inset(15).priority(.init(999))
            make.left.right.equalToSuperview().inset(18)
        }
    }
}
