//
//  PrecipButton.swift
//  1Weather
//
//  Created by Dmitry Stepanets on 24.02.2021.
//

import UIKit

class PrecipButton: UIControl {
    //Private
    private static var dailyFormatter:DateFormatter = {
        let fmt = DateFormatter()
        fmt.dateFormat = "d, E"
        return fmt
    }()
    private static var hourlyFormatter:DateFormatter = {
        let fmt = DateFormatter()
        fmt.dateFormat = "h a"
        return fmt
    }()
    private let valueLabel = UILabel()
    private let precipView = PrecipitationView()
    private let timeLabel = UILabel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareButton()
        prepareLabels()
        preparePrecipView()
        updateUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override var isSelected: Bool {
        didSet {
            if isSelected {
                self.backgroundColor = ThemeManager.currentTheme.periodButtonSelectedBackgroundColor
                self.valueLabel.font = AppFont.SFPro.bold(size: 16)
                self.timeLabel.font = AppFont.SFPro.bold(size: 12)
                self.layer.shadowOpacity = 0.65
                self.layer.borderWidth = 1 / UIScreen.main.scale
            }
            else {
                self.backgroundColor = ThemeManager.currentTheme.periodButtonBackgroundColor
                self.valueLabel.font = AppFont.SFPro.regular(size: 16)
                self.timeLabel.font = AppFont.SFPro.regular(size: 12)
                self.layer.shadowOpacity = 0
                self.layer.borderWidth = 0
            }
        }
    }
    
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        updateUI()
    }
    
    //Public
    public func configure(with daily:DailyWeather) {
        guard let percent = daily.precipitationProbability else {
            self.precipView.set(value: 0)
            self.valueLabel.text = "0%"
            self.timeLabel.text = nil
            return
        }
        
        self.precipView.set(value: CGFloat(percent)/100.0)
        self.valueLabel.text = "\(Int(percent))%"
        
        if Calendar.timeZoneCalendar(timeZone: daily.timeZone).isDateInToday(daily.date) {
            self.timeLabel.text = "day.today".localized()
        }
        else {
            PrecipButton.dailyFormatter.timeZone = daily.timeZone
            self.timeLabel.text = PrecipButton.dailyFormatter.string(from: daily.date)
        }
    }
    
    public func configure(with hourly:HourlyWeather) {
        guard let percent = hourly.precipitationProbability else {
            self.precipView.set(value: 0)
            self.valueLabel.text = "0%"
            self.timeLabel.text = nil
            return
        }
        
        self.precipView.set(value: CGFloat(percent)/100.0)
        self.valueLabel.text = "\(Int(percent))%"
        
        if Calendar.isNow(fromDate: hourly.date, timeZone: hourly.timeZone) {
            self.timeLabel.text = "day.now".localized().uppercased()
        }
        else {
            PrecipButton.hourlyFormatter.timeZone = hourly.timeZone
            self.timeLabel.text = PrecipButton.hourlyFormatter.string(from: hourly.date)
        }
    }
    
    //Private
    private func updateUI() {
        self.layer.borderColor = ThemeManager.currentTheme.periodButtonBorderColor.cgColor
        self.layer.shadowColor = ThemeManager.currentTheme.periodButtonShadowColor.cgColor
        self.backgroundColor = ThemeManager.currentTheme.periodButtonBackgroundColor
        
        switch interfaceStyle {
        case .light:
            valueLabel.textColor = ThemeManager.currentTheme.secondaryTextColor
            timeLabel.textColor = ThemeManager.currentTheme.secondaryTextColor
            
            self.layer.shadowOffset = .init(width: 5, height: 5)
            self.layer.shadowRadius = 18
            self.layer.shadowOpacity = 0.65
        case .dark:
            valueLabel.textColor = UIColor.white
            timeLabel.textColor = UIColor.white
            
            self.layer.shadowOffset = .init(width: 6, height: 6)
            self.layer.shadowRadius = 6
            self.layer.shadowOpacity = 0.65
        }
    }
}

//MARK:- Prepare
private extension PrecipButton {
    func prepareButton() {
        clipsToBounds = false
        layer.cornerRadius = 12
        
        self.snp.makeConstraints { (make) in
            make.width.equalTo(70)
        }
    }
    
    func prepareLabels() {
        valueLabel.isUserInteractionEnabled = false
        valueLabel.font = AppFont.SFPro.regular(size: 16)
        valueLabel.setContentHuggingPriority(.fittingSizeLevel, for: .vertical)
        addSubview(valueLabel)
        
        valueLabel.snp.makeConstraints { (make) in
            make.top.equalToSuperview().inset(16)
            make.centerX.equalToSuperview()
        }
        
        timeLabel.isUserInteractionEnabled = false
        timeLabel.font = AppFont.SFPro.regular(size: 12)
        addSubview(timeLabel)
        
        timeLabel.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().inset(18)
            make.centerX.equalToSuperview()
        }
    }
    
    func preparePrecipView() {
        precipView.isUserInteractionEnabled = false
        addSubview(precipView)
        precipView.snp.makeConstraints { (make) in
            make.top.equalTo(valueLabel.snp.bottom).offset(24).priority(.medium)
            make.bottom.equalTo(timeLabel.snp.top).offset(-20)
            make.width.equalTo(24)
            make.centerX.equalToSuperview()
            make.height.equalTo(130)
        }
    }
}
