//
//  CityConditionsCell.swift
//  1Weather
//
//  Created by Dmitry Stepanets on 12.02.2021.
//

import UIKit
import SnapKit

class CityConditionsCell: UITableViewCell {
    //Private
    private let kIndicatorWidth:CGFloat = 27
    private let scrollView = UIScrollView()
    private let stackView = UIStackView()
    private let progressView = UIView()
    private let progressIndicator = UIView()
    private var indicatorLeftConstraint:Constraint?
    private var indicatorWidthConstraint:Constraint?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        prepareCellStyle()
        prepareSrollView()
        prepareStackView()
        prepareProgress()
    }
    
    public func configure(with location:Location) {
        stackView.arrangedSubviews.forEach {
            if let conditionButton = $0 as? CityConditionButton {
                conditionButton.configure(with: location)
            }
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

//MARK:- Prepare
private extension CityConditionsCell {
    func prepareCellStyle() {
        selectionStyle = .none
        contentView.backgroundColor = ThemeManager.currentTheme.baseBackgroundColor
    }
    
    func prepareSrollView() {
        scrollView.backgroundColor = .clear
        scrollView.contentInset = .init(top: 0, left: 18, bottom: 0, right: 18)
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.delegate = self
        scrollView.clipsToBounds = false
        contentView.addSubview(scrollView)
        
        scrollView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalToSuperview().inset(18)
            make.height.equalTo(115)
        }
    }
    
    func prepareStackView() {
        stackView.axis = .horizontal
        stackView.distribution = .equalSpacing
        stackView.alignment = .center
        stackView.spacing = 12
        stackView.clipsToBounds = false
        scrollView.addSubview(stackView)
        
        stackView.snp.makeConstraints { (make) in
            make.edges.height.equalToSuperview()
        }
        
        for conditionType in WeatherConditionType.allCases {
            let button = CityConditionButton(type: conditionType)
            stackView.addArrangedSubview(button)
        }
    }
    
    func prepareProgress() {
        progressView.backgroundColor = ThemeManager.currentTheme.progressBackgroundColor
        progressView.layer.cornerRadius = 2
        progressView.clipsToBounds = true
        contentView.addSubview(progressView)
        progressView.snp.makeConstraints { (make) in
            make.top.equalTo(scrollView.snp.bottom).offset(18)
            make.centerX.equalToSuperview()
            make.width.equalTo(86)
            make.height.equalTo(4)
            make.bottom.equalToSuperview().inset(15)
        }
        
        progressIndicator.backgroundColor = ThemeManager.currentTheme.progressIndicatorColor
        progressIndicator.layer.cornerRadius = 2
        progressIndicator.clipsToBounds = true
        progressView.addSubview(progressIndicator)
        progressIndicator.snp.makeConstraints {[weak self] (make) in
            make.height.equalToSuperview()
            self?.indicatorLeftConstraint = make.left.equalToSuperview().constraint
            make.centerY.equalToSuperview()
            self?.indicatorWidthConstraint = make.width.equalTo(kIndicatorWidth).constraint
        }
    }
}

extension CityConditionsCell: UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let totalContentWidth = scrollView.contentSize.width + scrollView.contentInset.left + scrollView.contentInset.right
        let scrollProgress = (scrollView.contentOffset.x + scrollView.contentInset.left)
            / (totalContentWidth - scrollView.bounds.width)
        
        let multiplier = (progressView.bounds.width - kIndicatorWidth) / kIndicatorWidth
        indicatorLeftConstraint?.layoutConstraints.first?.constant = multiplier * scrollProgress * kIndicatorWidth
    }
}
