//
//  ForecastConditionView.swift
//  1Weather
//
//  Created by Dmitry Stepanets on 18.03.2021.
//

import UIKit

class ForecastConditionView: UIView {
    //Private
    private let type:WeatherConditionType
    private let imageView = UIImageView()
    private let valueLabel = UILabel()
    private let nameLabel = UILabel()
    
    init(type:WeatherConditionType) {
        self.type = type
        super.init(frame: .zero)
        
        prepareImageView()
        prepareLabels()
    }
    
    func configure(dailyWeather:DailyWeather) {
        switch type {
        case .precipitation:
            valueLabel.text = "\(dailyWeather.precipitationProbability ?? 0)%"
        case .humidity:
            valueLabel.text = "--"
        case .uvIndex:
            valueLabel.text = "--"
        case .pressure:
            valueLabel.text = "--"
        case .dewPoint:
            valueLabel.text = "--"
        case .visibility:
            valueLabel.text = "--"
        case .wind:
            let speed = dailyWeather.windSpeed?.string ?? "--"
            let direction = dailyWeather.windDirection?.rawValue ?? ""
            valueLabel.text = "\(direction) \(speed)"
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

private extension ForecastConditionView {
    func prepareImageView() {
        imageView.contentMode = .scaleAspectFit
        imageView.image = type.image
        addSubview(imageView)
        
        imageView.snp.makeConstraints { (make) in
            make.width.height.equalTo(32)
            make.top.left.bottom.equalToSuperview()
        }
    }
    
    func prepareLabels() {
        valueLabel.textColor = ThemeManager.currentTheme.secondaryTextColor
        valueLabel.font = AppFont.SFPro.bold(size: 14)
        valueLabel.setContentHuggingPriority(.fittingSizeLevel, for: .vertical)
        addSubview(valueLabel)
        
        nameLabel.textColor = ThemeManager.currentTheme.secondaryTextColor
        nameLabel.font = AppFont.SFPro.regular(size: 12)
        nameLabel.text = type.localized
        nameLabel.setContentCompressionResistancePriority(.init(751), for: .vertical)
        addSubview(nameLabel)
        
        //Constraints
        valueLabel.snp.makeConstraints { (make) in
            make.left.equalTo(imageView.snp.right).offset(10)
            make.top.equalToSuperview()
            make.right.equalToSuperview()
        }
        
        nameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(valueLabel)
            make.top.equalTo(valueLabel.snp.bottom).offset(5)
            make.bottom.equalToSuperview()
            make.right.equalToSuperview()
        }
    }
}
