//
//  TodayConditionButton.swift
//  1Weather
//
//  Created by Dmitry Stepanets on 12.02.2021.
//

import UIKit

class TodayConditionButton: UIControl {
    //Private
    private let conditionType:WeatherConditionType
    private let imageView = UIImageView()
    private let valueLabel = UILabel()
    private let nameLabel = UILabel()
    private let descriptionLabel = UILabel()
    
    init(type: WeatherConditionType) {
        self.conditionType = type
        super.init(frame: .zero)
        
        prepareButton()
        prepareImageView()
        prepareLabels()
    }
    
    public func configure(with location:Location) {
        switch conditionType {
        case .precipitation:
            valueLabel.text = "\(location.today?.precipitationProbability ?? 0)%"
        case .humidity:
            valueLabel.text = "\(location.today?.humidity ?? 0)%"
        case .uvIndex:
            valueLabel.text = "4"
        case .pressure:
            valueLabel.text = location.today?.pressure?.settingsConverted.string
        case .dewPoint:
            valueLabel.text = "12°"
        case .visibility:
            valueLabel.text = location.today?.visibility?.settingsConverted.string
        case .wind:
            let speed = location.today?.windSpeed?.settingsConverted.string ?? "--"
            let direction = location.today?.windDirection?.rawValue ?? ""
            valueLabel.text = "\(direction) \(speed)"
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

//MARK:- Prepare
private extension TodayConditionButton {
    func prepareButton() {
        self.backgroundColor = ThemeManager.currentTheme.containerBackgroundColor
        self.clipsToBounds = false
        self.layer.cornerRadius = 12
        
        if self.interfaceStyle == .light {
            self.layer.shadowColor = UIColor(hex: 0x0c192f).cgColor
            self.layer.shadowOffset = .init(width: 0, height: 5)
            self.layer.shadowOpacity = 0.06
            self.layer.shadowRadius = 20
        }
    }
    
    func prepareImageView() {
        imageView.isUserInteractionEnabled = false
        imageView.contentMode = .scaleAspectFit
        imageView.clipsToBounds = true
        imageView.image = conditionType.image
        addSubview(imageView)
        
        imageView.snp.makeConstraints { (make) in
            make.left.top.equalToSuperview().inset(20)
            make.size.equalTo(CGSize(width: 32, height: 32))
        }
    }
    
    func prepareLabels() {
        //Value
        valueLabel.isUserInteractionEnabled = false
        valueLabel.font = AppFont.SFPro.bold(size: 18)
        valueLabel.textColor = ThemeManager.currentTheme.secondaryTextColor
        valueLabel.textAlignment = .left
        valueLabel.text = "21%"
        addSubview(valueLabel)
        
        valueLabel.snp.makeConstraints { (make) in
            make.left.equalTo(imageView.snp.right).offset(13)
            make.top.equalToSuperview().inset(16)
        }
        
        //Name
        nameLabel.isUserInteractionEnabled = false
        nameLabel.font = AppFont.SFPro.regular(size: 16)
        nameLabel.textColor = ThemeManager.currentTheme.secondaryTextColor
        nameLabel.textAlignment = .left
        nameLabel.text = conditionType.localized
        addSubview(nameLabel)
        
        nameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(valueLabel)
            make.top.equalTo(valueLabel.snp.bottom).offset(2)
        }
        
        //Description
        descriptionLabel.isUserInteractionEnabled = false
        descriptionLabel.font = AppFont.SFPro.regular(size: 12)
        descriptionLabel.textColor = ThemeManager.currentTheme.secondaryTextColor
        descriptionLabel.textAlignment = .left
        descriptionLabel.text = "80% chance of rain Between 3-7 PM"
        addSubview(descriptionLabel)
        
        descriptionLabel.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview().inset(20)
            make.top.equalTo(imageView.snp.bottom).offset(20)
            make.bottom.equalToSuperview().inset(13)
        }
    }
}
