//
//  DayTimeView.swift
//  1Weather
//
//  Created by Dmitry Stepanets on 25.02.2021.
//

import UIKit

class DayTimeView: UIView {
    private let dayTimeLabel = UILabel()
    private let forecastImageView = UIImageView()
    private let tempLabel = UILabel()
    private let dayTimeConditionLabel = UILabel()
    
    public init(dayTimeWeather:DayTimeWeather, withSeparator:Bool = false) {
        super.init(frame: .zero)
        
        prepareView()
        if withSeparator {
            prepareSeparator()
        }
        
        configure(with: dayTimeWeather)
        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(with dayTimeWeather:DayTimeWeather) {
        dayTimeLabel.text = dayTimeWeather.dayTime.localized
        forecastImageView.image = dayTimeWeather.type.image(isDay: dayTimeWeather.isDay)
        tempLabel.text = dayTimeWeather.temp?.shortString ?? "--"
        dayTimeConditionLabel.text = dayTimeWeather.type.localized(isDay: dayTimeWeather.isDay)
    }
    
    //Private
    private func updateUI() {
        switch interfaceStyle {
        case .light:
            dayTimeLabel.textColor = ThemeManager.currentTheme.secondaryTextColor
            tempLabel.textColor = ThemeManager.currentTheme.secondaryTextColor
            dayTimeConditionLabel.textColor = ThemeManager.currentTheme.secondaryTextColor
        case .dark:
            dayTimeLabel.textColor = ThemeManager.currentTheme.primaryTextColor
            tempLabel.textColor = ThemeManager.currentTheme.primaryTextColor
            dayTimeConditionLabel.textColor = ThemeManager.currentTheme.primaryTextColor
        }
    }
}

//MARK:- Prepare
private extension DayTimeView {
    func prepareView() {
        dayTimeLabel.font = AppFont.SFPro.bold(size: 14)
        dayTimeLabel.textColor = ThemeManager.currentTheme.secondaryTextColor
        dayTimeLabel.text = "--"
        addSubview(dayTimeLabel)
        
        forecastImageView.contentMode = .scaleAspectFit
        forecastImageView.image = WeatherType.unknown.image(isDay: true)
        addSubview(forecastImageView)
        
        tempLabel.font = AppFont.SFPro.bold(size: 18)
        tempLabel.textColor = ThemeManager.currentTheme.secondaryTextColor
        tempLabel.text = "--"
        addSubview(tempLabel)
        
        dayTimeConditionLabel.alpha = 0.7
        dayTimeConditionLabel.numberOfLines = 3
        dayTimeConditionLabel.lineBreakMode = .byWordWrapping
        dayTimeConditionLabel.textAlignment = .center
        dayTimeConditionLabel.font = AppFont.SFPro.regular(size: 14)
        dayTimeConditionLabel.textColor = ThemeManager.currentTheme.secondaryTextColor
        dayTimeConditionLabel.text = "--"
        addSubview(dayTimeConditionLabel)
        
        //Constraints
        dayTimeLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().inset(24)
        }
        
        forecastImageView.snp.makeConstraints { (make) in
            make.width.height.equalTo(28)
            make.centerX.equalToSuperview()
            make.top.equalTo(dayTimeLabel.snp.bottom).offset(18)
        }
        
        tempLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(forecastImageView.snp.bottom).offset(18)
        }
        
        dayTimeConditionLabel.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview().inset(8)
            make.top.equalTo(tempLabel.snp.bottom).offset(14).priority(.low)
            make.bottom.equalToSuperview().inset(18)
            make.height.equalTo(52)
        }
        
        self.snp.makeConstraints { (make) in
            make.height.equalTo(210)
        }
    }
    
    func prepareSeparator() {
        let separatorView = UIView()
        separatorView.backgroundColor = UIColor(hex: 0xc2c9d6)
        separatorView.alpha = 0.5
        addSubview(separatorView)
        
        separatorView.snp.makeConstraints { (make) in
            make.top.equalTo(dayTimeLabel)
            make.bottom.equalToSuperview().inset(18)
            make.right.equalToSuperview()
            make.width.equalTo(1)
        }
    }
}
