//
//  ForecastHourlyCell.swift
//  1Weather
//
//  Created by Dmitry Stepanets on 22.03.2021.
//

import UIKit

class ForecastHourlyCell: UITableViewCell {
    //Private
    private let tempLabel = UILabel()
    private let forecastTimePeriodView = ForecastTimePeriodView()
    private let descriptionView = ForecastDescriptionView(lightStyleBackgroundColor: UIColor(hex: 0xfaedda).withAlphaComponent(0.5),
                                                          gradientColors: [UIColor(hex: 0xe81e15).withAlphaComponent(0.33).cgColor,
                                                                           UIColor(hex: 0xf71d11).withAlphaComponent(0).cgColor])

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        prepareCell()
        prepareTempLabel()
        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
        tempLabel.textColor = ThemeManager.currentTheme.primaryTextColor
    }
    
    //Public
    public func configure(hourly:[HourlyWeather]) {
        self.forecastTimePeriodView.set(daily: nil, hourly: hourly)
        if self.forecastTimePeriodView.isEmpty {
            self.forecastTimePeriodView.set(forecastType: .hourly, buttonType: ForecastPeriodButton.self)
        }
    }
}

//MARK:- Prepare
private extension ForecastHourlyCell {
    func prepareCell() {
        selectionStyle = .none
    }
    
    func prepareTempLabel() {
        tempLabel.font = AppFont.SFPro.bold(size: 18)
        tempLabel.text = "condition.temperature".localized()
        contentView.addSubview(tempLabel)
        
        tempLabel.snp.makeConstraints { (make) in
            make.left.top.equalToSuperview().inset(18)
        }
    }
    
    func prepareTimePeriodView() {
        contentView.addSubview(forecastTimePeriodView)
        forecastTimePeriodView.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.top.equalTo(tempLabel.snp.bottom).offset(18).priority(.medium)
            make.height.equalTo(267)
        }
    }
    
    func prepareDescriptionView() {
        descriptionView.set(image: UIImage(named: "hot_indicator"))
        descriptionView.set(text: "Hottest part of the day 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(forecastTimePeriodView.snp.bottom).offset(20)
            make.bottom.equalToSuperview().inset(15)
        }
    }
}
