//
//  PrecipitationView.swift
//  1Weather
//
//  Created by Dmitry Stepanets on 04.03.2021.
//

import UIKit

class PrecipitationView: UIView {
    //Private
    private let kLevelWidth:CGFloat = 12
    private let kCapHeight:CGFloat = 4
    private let levelLayer = CAShapeLayer()
    private let innerShadowLayer = CAShapeLayer()
    private let gradientLayer = CAGradientLayer()
    private let capLayer = CAShapeLayer()
    private var currentValue:CGFloat = 1.0
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareView()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let visualPrettyLevel = max(0.25, currentValue)
        let targetHeight = self.bounds.height * visualPrettyLevel
        let yOffset = bounds.height - targetHeight
        
        //Main level
        levelLayer.frame = .init(x: (self.bounds.width - kLevelWidth) / 2,
                                 y: yOffset,
                                 width: kLevelWidth,
                                 height: targetHeight)
        
        //Inner shadow
        innerShadowLayer.frame = .init(x: 0, y: 0, width: self.levelLayer.frame.width, height: self.levelLayer.frame.height)
        
        //Gradient
        gradientLayer.frame = .init(x: 0, y: 17, width: levelLayer.bounds.width, height: levelLayer.bounds.height - 17)
        
        //Cap
        capLayer.path = UIBezierPath(ovalIn: .init(x: 0,
                                                   y: gradientLayer.frame.origin.y - kCapHeight/2,
                                                   width: levelLayer.bounds.width,
                                                   height: kCapHeight)).cgPath
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public func set(value:CGFloat) {
        currentValue = max(0, value)
        currentValue = min(1, value)
        self.layoutSubviews()
    }
}

//MARK:- Prepare
private extension PrecipitationView {
    func prepareView() {
        //Main
        levelLayer.masksToBounds = true
        levelLayer.backgroundColor = UIColor(hex: 0xe9ebfc).cgColor
        levelLayer.cornerRadius = kLevelWidth / 2
        layer.addSublayer(levelLayer)
        
        //Shadow
        innerShadowLayer.cornerRadius = kLevelWidth / 2
        innerShadowLayer.shadowColor = UIColor(hex: 0x55acfe).cgColor
        innerShadowLayer.shadowOpacity = 0.46
        innerShadowLayer.shadowOffset = .init(width: 0, height: 1)
        innerShadowLayer.shadowRadius = 3
        levelLayer.addSublayer(innerShadowLayer)
        
        //Gradient
        gradientLayer.colors = [UIColor(hex: 0x2d9aff).cgColor, UIColor(hex: 0x8fc6fb).cgColor]
        gradientLayer.startPoint = .init(x: 0.5, y: 0)
        gradientLayer.endPoint = .init(x: 0.5, y: 1)
        levelLayer.addSublayer(gradientLayer)
        
        //Cup
        capLayer.fillColor = UIColor(hex: 0x78beff).cgColor
        levelLayer.addSublayer(capLayer)
    }
}
