//
//  CityDayTimesCell.swift
//  1Weather
//
//  Created by Dmitry Stepanets on 25.02.2021.
//

import UIKit

class CityDayTimesCell: UITableViewCell {
    //Private
    private let headingLabel = UILabel()
    private let headingButton = ArrowButton()
    private let stackViewContainer = UIView()
    private let stackView = UIStackView()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        prepareCell()
        prepareHeading()
        prepareStackViewContainer()
        prepareStackView()
        updateUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        updateUI()
    }
    
    //Private
    @objc private func handleArrowButton() {
        #warning("Not implemented!")
        //TODO: Implement!
    }
    
    private func updateUI() {
        stackViewContainer.backgroundColor = ThemeManager.currentTheme.containerBackgroundColor
        
        switch interfaceStyle  {
        case .light:
            headingLabel.textColor = ThemeManager.currentTheme.secondaryTextColor
        case .dark:
            headingLabel.textColor = .white
        }
    }
    
    //Public
    public func configure(with location: Location) {
        for subview in stackView.subviews {
            stackView.removeArrangedSubview(subview)
            subview.removeFromSuperview()
        }
        
        let maxNumberOfItems = 4
        var currentlyShowing = 0
        var i = 0
        while i < location.dayTimeForecast.count && currentlyShowing < maxNumberOfItems {
            let dayTimeWeather = location.dayTimeForecast[i]
            i += 1
            
            if dayTimeWeather.date >= Date() {
                let view = DayTimeView(dayTimeWeather: dayTimeWeather,
                                       withSeparator: currentlyShowing != maxNumberOfItems - 1)
                stackView.addArrangedSubview(view)
                currentlyShowing += 1
            }
        }
    }
}

//MARK:- Prepare
private extension CityDayTimesCell {
    func prepareCell() {
        selectionStyle = .none
        contentView.backgroundColor = ThemeManager.currentTheme.baseBackgroundColor
    }
    
    func prepareHeading() {
        //Label
        headingLabel.font = AppFont.SFPro.bold(size: 16)
        headingLabel.textColor = ThemeManager.currentTheme.secondaryTextColor
        headingLabel.text = "next24.title".localized().uppercased()
        contentView.addSubview(headingLabel)
        
        headingLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().inset(18)
            make.top.equalToSuperview().inset(15)
        }
        
        //Arrow button
        headingButton.addTarget(self, action: #selector(handleArrowButton), for: .touchUpInside)
        contentView.addSubview(headingButton)
        
        headingButton.snp.makeConstraints { (make) in
            make.width.height.equalTo(30)
            make.right.equalToSuperview().inset(18)
            make.centerY.equalTo(headingLabel)
        }
    }
    
    func prepareStackViewContainer() {
        stackViewContainer.layer.cornerRadius = 12
        contentView.addSubview(stackViewContainer)
        
        stackViewContainer.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview().inset(18)
            make.top.equalTo(headingLabel.snp.bottom).offset(18)
            make.bottom.equalToSuperview().inset(15)
        }
    }
    
    func prepareStackView() {
        stackView.axis = .horizontal
        stackView.distribution = .fillEqually
        stackView.alignment = .center
        stackView.spacing = 0
        stackView.clipsToBounds = false
        stackViewContainer.addSubview(stackView)
        
        stackView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
}
