//
//  CurrentForecastCell.swift
//  1Weather
//
//  Created by Dmitry Stepanets on 12.11.2020.
//

import UIKit

class CurrentForecastCell: UITableViewCell {
    //Public
    static let kIdentifier = "currentForecastCell"
    
    //Private
    private let container = UIView()
    private let forecastImageView = UIImageView()
    private let temperatureLabel = UILabel()
    private let currentForecastLabel = UILabel()
    private let maxMinTemperatureLabel = UILabel()
    private let feelsLikeLabel = UILabel()
    private let windLabel = UILabel()
    private let gradient = GradientView()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        prepareCell()
        prepareContainer()
        prepareForecastView()
        prepareDetailsView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

private extension CurrentForecastCell {
    func prepareCell() {
        contentView.backgroundColor = .clear
        backgroundColor = .clear
    }
    
    func prepareContainer() {
        container.layer.cornerRadius = 6
        container.clipsToBounds = true
        
        //Set gradient
        container.addSubview(gradient)
        gradient.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        contentView.addSubview(container)
        container.snp.makeConstraints { (make) in
            make.left.equalToSuperview().inset(8)
            make.right.equalToSuperview().inset(8)
            make.bottom.equalToSuperview()
            make.top.equalToSuperview().inset(16)
        }
    }
    
    func prepareForecastView() {
        //Temperature
        temperatureLabel.backgroundColor = .clear
        temperatureLabel.font = UIFont.systemFont(ofSize: 110)
        temperatureLabel.text = "23°"
        temperatureLabel.textColor = .white
        
        container.addSubview(temperatureLabel)
        temperatureLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().inset(8)
            make.top.equalToSuperview()
            make.height.equalTo(95)
        }
        
        //Text forecast
        currentForecastLabel.font = UIFont.systemFont(ofSize: 20, weight: .medium)
        currentForecastLabel.textColor = .white
        currentForecastLabel.lineBreakMode = .byWordWrapping
        currentForecastLabel.numberOfLines = 0
        currentForecastLabel.text = "Light Rain & Snow shower"
        currentForecastLabel.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        
        container.addSubview(currentForecastLabel)
        currentForecastLabel.snp.makeConstraints { (make) in
            make.left.equalTo(temperatureLabel.snp.right).offset(16)
            make.bottom.equalTo(temperatureLabel)
            make.right.equalToSuperview().inset(8)
        }
        
        //Image
        forecastImageView.backgroundColor = .red
        forecastImageView.contentMode = .scaleAspectFit
        contentView.addSubview(forecastImageView)
        forecastImageView.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 70, height: 62))
            make.top.equalToSuperview()
            make.right.equalToSuperview().inset(16)
        }
        
    }
    
    func prepareDetailsView() {
        let stackView = UIStackView()
        stackView.isLayoutMarginsRelativeArrangement = true
        stackView.layoutMargins = .init(top: 0, left: 8, bottom: 0, right: 0)
        stackView.spacing = 14
        stackView.axis = .horizontal
        stackView.distribution = .equalSpacing
        stackView.alignment = .center
        
        container.addSubview(stackView)
        stackView.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.bottom.equalToSuperview()
            make.height.equalTo(32)
            make.top.equalTo(temperatureLabel.snp.bottom).offset(12).priority(.medium)
        }
        
        //Labels
        maxMinTemperatureLabel.font = UIFont.systemFont(ofSize: 15)
        maxMinTemperatureLabel.textColor = .white
        maxMinTemperatureLabel.text = "52˚/45˚"
        stackView.addArrangedSubview(maxMinTemperatureLabel)
        
        //Dot image
        let firstDot = UIImageView(image: #imageLiteral(resourceName: "dot"))
        firstDot.tintColor = .white
        stackView.addArrangedSubview(firstDot)
        
        feelsLikeLabel.font = UIFont.systemFont(ofSize: 15)
        feelsLikeLabel.textColor = .white
        feelsLikeLabel.text = "Feels like 54˚"
        stackView.addArrangedSubview(feelsLikeLabel)
        
        //Dot image
        let secondDot = UIImageView(image: #imageLiteral(resourceName: "dot"))
        secondDot.tintColor = .white
        stackView.addArrangedSubview(secondDot)
        
        windLabel.font = UIFont.systemFont(ofSize: 15)
        windLabel.textColor = .white
        windLabel.text = "Wind 26 KM/H SW"
        stackView.addArrangedSubview(windLabel)
    }
}
