//
//  ForecastDescriptionView.swift
//  1Weather
//
//  Created by Dmitry Stepanets on 24.03.2021.
//

import UIKit

class ForecastDescriptionView: UIView {
    //Private
    private let imageView = UIImageView()
    private let textLabel = UILabel()
    private let gradientBorder = CAGradientLayer()
    private var gradientColors = [CGColor]()
    private let lightBackgroundColor:UIColor
    
    init(lightStyleBackgroundColor:UIColor, gradientColors:[CGColor]? = nil) {
        self.lightBackgroundColor = lightStyleBackgroundColor
        super.init(frame: .zero)
        
        if let colors = gradientColors {
            self.gradientColors = colors
        }
        
        prepareViews()
        prepareGradientBorder()
        updateUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        self.gradientBorder.frame = self.bounds
        let shape = CAShapeLayer()
        shape.lineWidth = 1
        shape.path = UIBezierPath(roundedRect: self.bounds, cornerRadius: self.layer.cornerRadius).cgPath
        shape.strokeColor = UIColor.black.cgColor
        shape.fillColor = UIColor.clear.cgColor
        gradientBorder.mask = shape
    }
    
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        updateUI()
    }
    
    //Public
    public func set(text:String?) {
        self.textLabel.text = text
    }
    
    public func set(image:UIImage?) {
        self.imageView.image = image
    }
    
    //Private
    private func updateUI() {
        textLabel.textColor = ThemeManager.currentTheme.secondaryTextColor
        
        if interfaceStyle == .light {
            gradientBorder.isHidden = true
            backgroundColor = lightBackgroundColor
        }
        else {
            gradientBorder.isHidden = false
            backgroundColor = ThemeManager.currentTheme.containerBackgroundColor
        }
    }
}

//MARK:- Prepare
private extension ForecastDescriptionView {
    func prepareViews() {
        self.clipsToBounds = false
        self.layer.cornerRadius = 8
        
        imageView.contentMode = .scaleAspectFit
        imageView.image = nil
        addSubview(imageView)
        
        imageView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().inset(20)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(12)
        }
        
        textLabel.font = AppFont.SFPro.regular(size: 13)
        textLabel.textColor = ThemeManager.currentTheme.secondaryTextColor
        addSubview(textLabel)
        
        textLabel.snp.makeConstraints { (make) in
            make.left.equalTo(imageView.snp.right).offset(8)
            make.right.equalToSuperview().inset(8)
            make.centerY.equalToSuperview()
        }
    }
    
    func prepareGradientBorder() {
        gradientBorder.frame = self.frame
        gradientBorder.colors = self.gradientColors
        gradientBorder.cornerRadius = self.layer.cornerRadius
        gradientBorder.startPoint = .init(x: 0, y: 0.5)
        gradientBorder.endPoint = .init(x: 1, y: 0.5)
        layer.addSublayer(gradientBorder)
    }
}
