//
//  PrecipitationCell.swift
//  1Weather
//
//  Created by Dmitry Stepanets on 24.02.2021.
//

import UIKit

class PrecipitationCell: UITableViewCell {
    //Private
    private let headingLabel = UILabel()
    private let headingButton = ArrowButton()
    private let scrollView = UIScrollView()
    private let stackView = UIStackView()
    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()
        prepareHeading()
        prepareScrollView()
        prepareStackView()
        prepareSummaryView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public func configure(with dayily:[DailyWeather]) {
        guard stackView.arrangedSubviews.isEmpty else { return }
        
        self.headingButton.isHidden = false
        for index in 0..<dayily.count {
            let precipButton = PrecipButton()
            precipButton.isSelected = index == 1
            precipButton.configure(with: dayily[index])
            precipButton.addTarget(self, action: #selector(handlePrecipButton(button:)), for: .touchUpInside)
            stackView.addArrangedSubview(precipButton)
        }
        stackView.layoutIfNeeded()
    }
    
    public func configure(with hourly:[HourlyWeather]) {
        guard stackView.arrangedSubviews.isEmpty else { return }
        
        self.headingLabel.font = AppFont.SFPro.bold(size: 18)
        self.headingButton.isHidden = true
        self.headingLabel.text = "precipitation.title".localized().capitalized
        self.headingLabel.textColor = ThemeManager.currentTheme.primaryTextColor
        for index in 0..<hourly.count {
            let precipButton = PrecipButton()
            precipButton.isSelected = index == 0
            precipButton.configure(with: hourly[index])
            precipButton.addTarget(self, action: #selector(handlePrecipButton(button:)), for: .touchUpInside)
            stackView.addArrangedSubview(precipButton)
        }
        stackView.layoutIfNeeded()
    }
    
    //Private
    @objc private func handleArrowButton() {
        
    }
    
    @objc private func handlePrecipButton(button:PrecipButton) {
        stackView.arrangedSubviews.forEach {
            if let precipButton = $0 as? PrecipButton {
                precipButton.isSelected = precipButton === button
            }
        }
    }
}

//MARK:- Prepare
private extension PrecipitationCell {
    func prepareCell() {
        selectionStyle = .none
        contentView.backgroundColor = ThemeManager.currentTheme.baseBackgroundColor
    }
    
    func prepareHeading() {
        //Label
        headingLabel.font = AppFont.SFPro.bold(size: 16)
        headingLabel.textColor = ThemeManager.currentTheme.primaryTextColor
        headingLabel.text = "precipitation.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 prepareScrollView() {
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.clipsToBounds = false
        contentView.addSubview(scrollView)
        
        scrollView.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.top.equalTo(headingLabel.snp.bottom).offset(18)
            make.height.equalTo(240)
        }
    }
    
    func prepareStackView() {
        stackView.axis = .horizontal
        stackView.distribution = .equalSpacing
        stackView.alignment = .center
        stackView.spacing = 10
        stackView.clipsToBounds = false
        stackView.isLayoutMarginsRelativeArrangement = true
        stackView.layoutMargins = .init(top: 0, left: 6, bottom: 0, right: 6)
        scrollView.addSubview(stackView)
        
        stackView.snp.makeConstraints { (make) in
            make.edges.height.equalToSuperview()
        }
    }
    
    func prepareSummaryView() {
        descriptionView.set(image: UIImage(named: "humidity"))
        descriptionView.set(text: "Possible light shower between 1 PM - 2 PM")
        contentView.addSubview(descriptionView)
        
        descriptionView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview().inset(18)
            make.height.equalTo(40)
            make.top.equalTo(scrollView.snp.bottom).offset(26).priority(.medium)
            make.bottom.equalToSuperview().inset(15)
        }
    }
}
