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

import UIKit

class CityAirQualityCell: UITableViewCell {
    //Private
    private let headingLabel = UILabel()
    private let valueCircle = CAShapeLayer()
    private let valueProgressGradient = CAGradientLayer()
    private let airQualityValueLabel = UILabel()
    private let airQualityLabel = UILabel()
    private let airDescLabel = UILabel()
    private let stackView = UIStackView()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        prepareCell()
        prepareHeading()
        prepareAirLabels()
        prepareValueProgress()
        
        setAirQuality(value: 48)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        valueCircle.path = UIBezierPath(arcCenter: airQualityValueLabel.center,
                                        radius: 36,
                                        startAngle: 0,
                                        endAngle: 2 * .pi,
                                        clockwise: false).cgPath
        let mask = CAShapeLayer()
        mask.lineWidth = 6
        mask.lineCap = .round
        mask.strokeColor = UIColor.red.cgColor
        mask.fillColor = UIColor.clear.cgColor
        mask.path = UIBezierPath(arcCenter: airQualityValueLabel.center,
                                               radius: 36,
                                               startAngle: -.pi/2,
                                               endAngle: 0,
                                               clockwise: true).cgPath
        
        valueProgressGradient.frame = .init(x: 0,
                                            y: 0,
                                            width: 100,
                                            height: 100)
//        valueProgressGradient.mask = valueProgressShape
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setAirQuality(value:CGFloat) {
        airQualityValueLabel.text = "\(Int(value))"
        
        let aqiText = "air.quality.is".localized()
        let aqiConditionText = "air.quality.good".localized().uppercased()
        let attrString = NSMutableAttributedString(string: "\(aqiText)\n\(aqiConditionText)",
                                                   attributes: [.font : AppFont.SFPro.regular(size: 24),
                                                                .foregroundColor :ThemeManager.currentTheme.secondaryTextColor])
        attrString.addAttributes([.font : AppFont.SFPro.medium(size: 34)],
                                 range: NSRange(location: aqiText.count + 1,
                                                length: aqiConditionText.count))
        airQualityLabel.attributedText = attrString
    }
}

//MARK:- Prepare
private extension CityAirQualityCell {
    func prepareCell() {
        selectionStyle = .none
        contentView.backgroundColor = ThemeManager.currentTheme.baseBackgroundColor
    }
    
    func prepareHeading() {
        headingLabel.font = AppFont.SFPro.bold(size: 16)
        headingLabel.textColor = ThemeManager.currentTheme.secondaryTextColor
        headingLabel.text = "AQI"
        contentView.addSubview(headingLabel)
        
        headingLabel.snp.makeConstraints { (make) in
            make.top.equalToSuperview().inset(15)
            make.left.equalToSuperview().inset(18)
        }
    }
    
    func prepareAirLabels() {
        airQualityValueLabel.font = AppFont.SFPro.bold(size: 18)
        airQualityValueLabel.textColor = ThemeManager.currentTheme.secondaryTextColor
        contentView.addSubview(airQualityValueLabel)
        
        airQualityLabel.numberOfLines = 0
        airQualityLabel.lineBreakMode = .byWordWrapping
        airQualityLabel.textAlignment = .left
        contentView.addSubview(airQualityLabel)
        
        airDescLabel.textAlignment = .left
        airDescLabel.numberOfLines = 0
        airDescLabel.lineBreakMode = .byWordWrapping
        airDescLabel.font = AppFont.SFPro.regular(size: 16)
        airDescLabel.textColor = ThemeManager.currentTheme.secondaryTextColor
        airDescLabel.text = "Slightly elevated pollution, but general public likely not affected"
        contentView.addSubview(airDescLabel)
        
        //Constraints
        airQualityValueLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().inset(72)
            make.top.equalTo(headingLabel.snp.bottom).offset(72)
        }
        
        airQualityLabel.snp.makeConstraints { (make) in
            make.left.equalTo(airQualityValueLabel.snp.right).offset(54)
            make.centerY.equalTo(airQualityValueLabel)
        }
        
        airDescLabel.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview().inset(50)
            make.top.equalTo(airQualityLabel.snp.bottom).offset(9)
            make.bottom.equalToSuperview().inset(30)
        }
    }
    
    func prepareValueProgress() {
        valueCircle.strokeColor = UIColor(hex: 0xe9ebfc).cgColor
        valueCircle.fillColor = UIColor.clear.cgColor
        valueCircle.lineWidth = 2
        contentView.layer.addSublayer(valueCircle)
    
        valueProgressGradient.type = .radial
        valueProgressGradient.startPoint = .init(x: 0.5, y: 0.5)
        valueProgressGradient.endPoint = .init(x: 0, y: 0)
        valueProgressGradient.colors = [UIColor.red.cgColor, UIColor.yellow.cgColor]
        contentView.layer.addSublayer(valueProgressGradient)
    }
}
