//
//  ForecastWindSpeedCell.swift
//  1Weather
//
//  Created by Dmitry Stepanets on 23.03.2021.
//

import UIKit

class ForecastWindSpeedCell: UITableViewCell {
    //Private
    private let headingLabel = UILabel()
    private let timePeriodView = ForecastTimePeriodView()
    private let descriptionView = ForecastDescriptionView(lightStyleBackgroundColor: UIColor(hex: 0xd9ebfe),
                                                          gradientColors: [UIColor(hex: 0x44a4ff).withAlphaComponent(0.65).cgColor,
                                                                           UIColor(hex: 0x73bbff).withAlphaComponent(0).cgColor])
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        prepareCell()
        prepareHeadingLabel()
        prepareTimePeriodView()
        prepareDescriptionView()
        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
        headingLabel.textColor = ThemeManager.currentTheme.primaryTextColor
    }
    
    //Public
    public func configure(with hourly:[HourlyWeather]) {
        self.timePeriodView.set(daily: nil, hourly: hourly)
        if self.timePeriodView.isEmpty {
            self.timePeriodView.set(forecastType: .wind, buttonType: ForecastWindButton.self)
        }
    }
}

//MARK:- Prepare
private extension ForecastWindSpeedCell {
    func prepareCell() {
        selectionStyle = .none
    }
    
    func prepareHeadingLabel() {
        headingLabel.font = AppFont.SFPro.bold(size: 18)
        headingLabel.text = "condition.wind".localized()
        contentView.addSubview(headingLabel)
        
        headingLabel.snp.makeConstraints { (make) in
            make.left.top.equalToSuperview().inset(18)
        }
    }
    
    func prepareTimePeriodView() {
        contentView.addSubview(timePeriodView)
        timePeriodView.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.top.equalTo(headingLabel.snp.bottom).offset(18).priority(.medium)
            make.height.equalTo(267)
        }
    }
    
    func prepareDescriptionView() {
        descriptionView.set(image: UIImage(named: "blowingDust"))
        descriptionView.set(text: "Strongest winds between 12 AM - 1 PM")
        contentView.addSubview(descriptionView)
        
        descriptionView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview().inset(18)
            make.height.equalTo(40)
            make.top.equalTo(timePeriodView.snp.bottom).offset(20)
            make.bottom.equalToSuperview().inset(15)
        }
    }
}
