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

import UIKit
import SnapKit

class CityForecastCell: UITableViewCell {
    //Private
    private let container = UIView()
    private let cityImageView = UIImageView()
    private let temperatureLabel = UILabel()
    private let forecastDescriptionLabel = UILabel()
    private let feelsLikeLabel = UILabel()
    private let forecastImageView = UIImageView()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        prepareCell()
        prepareContainer()
        prepareCityImage()
        prepareTemperatureLabel()
        prepareForecastLabel()
        prepareFeelsLikeLabel()
        preprareForecastImage()
        updateUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        updateUI()
    }
    
    //Public
    public func configure(with location:Location) {
        cityImageView.image = UIImage(named: location.imageName ?? "")
        temperatureLabel.text = location.today?.temp?.shortString
        let maxTemp = location.today?.maxTemp?.shortString ?? "--"
        let minTemp = location.today?.minTemp?.shortString ?? "--"
        let feelstemp = location.today?.apparentTemp?.shortString ?? "--"
        forecastDescriptionLabel.text = "\(location.today?.type.localized(isDay: location.today?.isDay ?? true) ?? "") | \(maxTemp)/\(minTemp)"
        feelsLikeLabel.text = "Feels like \(feelstemp) - Due to high humidity"
        forecastImageView.image = location.today?.type.image(isDay: location.today?.isDay ?? true)
    }
    
    //Private
    private func updateUI() {
        temperatureLabel.textColor = ThemeManager.currentTheme.primaryTextColor
        forecastDescriptionLabel.textColor = ThemeManager.currentTheme.primaryTextColor
        feelsLikeLabel.textColor = ThemeManager.currentTheme.secondaryTextColor
        contentView.backgroundColor = ThemeManager.currentTheme.baseBackgroundColor
        container.backgroundColor = ThemeManager.currentTheme.containerBackgroundColor
        
        switch interfaceStyle {
        case .light:
            container.layer.shadowColor = UIColor(hex: 0x020116).cgColor
            container.layer.shadowOffset = .init(width: 0, height: 10)
            container.layer.shadowRadius = 20
            container.layer.shadowOpacity = 0.12
        case .dark:
            container.layer.shadowColor = UIColor.black.cgColor
            container.layer.shadowOffset = .init(width: 8, height: 8)
            container.layer.shadowRadius = 8
            container.layer.shadowOpacity = 1.0
        }
    }
}

//MARK:- Prepare
private extension CityForecastCell {
    func prepareCell() {
        selectionStyle = .none
    }
    
    func prepareContainer() {
        container.clipsToBounds = false
        container.layer.cornerRadius = 12
        contentView.addSubview(container)
        
        container.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview().inset(18)
            make.top.equalToSuperview().inset(18)
            make.bottom.equalToSuperview().inset(30)
        }
    }
    
    func prepareCityImage() {
        cityImageView.layer.cornerRadius = 10
        cityImageView.contentMode = .scaleAspectFill
        cityImageView.image = UIImage(named: "ny_bridge")
        cityImageView.clipsToBounds = true
        container.addSubview(cityImageView)
        
        cityImageView.snp.makeConstraints { (make) in
            make.left.top.right.equalToSuperview().inset(8)
            make.height.equalTo(172)
        }
    }
    
    func prepareTemperatureLabel() {
        temperatureLabel.font = AppFont.SFPro.bold(size: 75)
        temperatureLabel.textAlignment = .left
        temperatureLabel.setContentHuggingPriority(.fittingSizeLevel, for: .vertical)
        container.addSubview(temperatureLabel)
        
        temperatureLabel.snp.makeConstraints { (make) in
            make.top.equalTo(cityImageView.snp.bottom).offset(12)
            make.left.equalToSuperview().inset(18)
        }
    }
    
    func prepareForecastLabel() {
        forecastDescriptionLabel.font = AppFont.SFPro.bold(size: 16)
        container.addSubview(forecastDescriptionLabel)
        
        forecastDescriptionLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().inset(18)
            make.top.equalTo(temperatureLabel.snp.bottom)
        }
    }
    
    func prepareFeelsLikeLabel() {
        feelsLikeLabel.font = AppFont.SFPro.regular(size: 12)
        container.addSubview(feelsLikeLabel)
        
        feelsLikeLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().inset(18)
            make.top.equalTo(forecastDescriptionLabel.snp.bottom).offset(5)
            make.bottom.equalToSuperview().inset(25)
        }
    }
    
    func preprareForecastImage() {
        forecastImageView.contentMode = .scaleAspectFit
        forecastImageView.clipsToBounds = true
        forecastImageView.image = UIImage(named: "partly_cloudy")
        container.addSubview(forecastImageView)
        
        forecastImageView.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 85, height: 85))
            make.top.equalTo(cityImageView.snp.bottom).offset(35)
            make.right.equalToSuperview().inset(28)
        }
    }
}
