//
//  CityForecastTimePeriodCell.swift
//  1Weather
//
//  Created by Dmitry Stepanets on 15.02.2021.
//

import UIKit

class CityForecastTimePeriodCell: UITableViewCell {
    //Private
    private let periodSegmentedControl = ForecastTimePeriodControl(items: ["forecast.timePeriod.daily".localized(),
                                                                           "forecast.timePeriod.hourly".localized()])
    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])
    private var graphIsDrawn = false
    
    //MARK:- Cell life cycle
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        prepareCell()
        prepareSegmentedControl()
        prepareForecastTimePeriodView()
        prepareDescriptionView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //Public
    public func configure(with location:Location) {
        self.forecastTimePeriodView.set(daily: location.daily, hourly: location.hourly)
        
        if graphIsDrawn == false {
            self.handleSegmentDidChange()
            self.graphIsDrawn = true
        }
    }
    
    @objc private func handleSegmentDidChange() {
        guard let timePeriod = TimePeriod(rawValue: self.periodSegmentedControl.selectedSegmentIndex) else {
            return
        }
        
        let forecastType = timePeriod == .daily ? ForecastType.daily : ForecastType.hourly
        self.forecastTimePeriodView.set(forecastType: forecastType, buttonType: ForecastPeriodButton.self)
    }
}

private extension CityForecastTimePeriodCell {
    func prepareCell() {
        selectionStyle = .none
        contentView.backgroundColor = ThemeManager.currentTheme.baseBackgroundColor
    }
    
    func prepareSegmentedControl() {
        periodSegmentedControl.selectedSegmentIndex = 0
        periodSegmentedControl.addTarget(self, action: #selector(handleSegmentDidChange), for: .valueChanged)
        contentView.addSubview(periodSegmentedControl)
        
        periodSegmentedControl.snp.makeConstraints { (make) in
            make.top.equalToSuperview().inset(15)
            make.left.right.equalToSuperview().inset(16)
            make.height.equalTo(40)
        }
    }
    
    func prepareForecastTimePeriodView() {
        contentView.addSubview(forecastTimePeriodView)
        forecastTimePeriodView.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.top.equalTo(periodSegmentedControl.snp.bottom).offset(30).priority(.medium)
            make.height.equalTo(240)
        }
    }
    
    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)
        }
    }
}
