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

import UIKit

protocol ForecastDailyCellDelegate:class {
    func timePeriodCell(cell:ForecastDailyCell, didSelectButtonAt index:Int)
    func timePeriodCell(cell:ForecastDailyCell, offsetDidChage offset: CGFloat)
}

class ForecastDailyCell: UITableViewCell {
    //Private
    private let forecastTimePeriodView = ForecastTimePeriodView()
    private let gradientView = GradientView(startColor: UIColor(hex: 0xffffff).withAlphaComponent(0),
                                            endColor: UIColor(hex: 0xdaddec),
                                            opacity: 0.5)
    private var graphIsDrawn = false
    
    //Public
    weak var delegate:ForecastDailyCellDelegate?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        prepareCell()
        prepareGradient()
        prepareTimePeriodView()
        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
        gradientView.isHidden = interfaceStyle == .dark
    }
    
    //Public
    public func configure(daily:[DailyWeather], offset:CGFloat = 0, selectedButtonIndex:Int = 0) {
        self.forecastTimePeriodView.set(daily: daily, hourly: nil)
        if self.forecastTimePeriodView.isEmpty {
            self.forecastTimePeriodView.set(forecastType: .daily, buttonType: ForecastDetailPeriodButton.self)
        }
        self.forecastTimePeriodView.selectButtonAt(index: selectedButtonIndex)
        self.forecastTimePeriodView.update(offset: offset)
    }
}

private extension ForecastDailyCell {
    func prepareCell() {
        selectionStyle = .none
    }
    
    func prepareGradient() {
        contentView.addSubview(gradientView)
        gradientView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(172)
        }
    }
    
    func prepareTimePeriodView() {
        forecastTimePeriodView.delegate = self
        contentView.addSubview(forecastTimePeriodView)
        forecastTimePeriodView.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.top.equalToSuperview().inset(20).priority(.medium)
            make.bottom.equalToSuperview().inset(30)
            make.height.equalTo(267)
        }
    }
}

//MARK:- ForecastTimePeriodView Delegate
extension ForecastDailyCell: ForecastTimePeriodViewDelegate {
    func forecastTimePeriodView(view: ForecastTimePeriodView, didSelectButtonAt index: Int) {
        self.delegate?.timePeriodCell(cell: self, didSelectButtonAt: index)
    }
    
    func offsetDidChange(offset: CGFloat) {
        self.delegate?.timePeriodCell(cell: self, offsetDidChage: offset)
    }
}
