//
//  SunUvView.swift
//  1Weather
//
//  Created by Dmitry Stepanets on 24.02.2021.
//

import UIKit

private enum UVLevel:Int, CaseIterable {
    case low = 0
    case mediuim
    case high
    case veryHigh
    case extreme
    
    var color:UIColor {
        switch self {
        case .low:
            return .clear
        case .mediuim:
            return UIColor(hex: 0xffea75)
        case .high:
            return UIColor(hex: 0xffdd00)
        case .veryHigh:
            return UIColor(hex: 0xffb300)
        case .extreme:
            return UIColor(hex: 0xff2e00)
        }
    }
    
    func includedToRange(level:Int) -> Bool {
        switch self {
        case .low:
            return 0...2 ~= level || level > 2
        case .mediuim:
            return 3...5 ~= level || level > 5
        case .high:
            return 6...7 ~= level || level > 7
        case .veryHigh:
            return 8...10 ~= level || level > 10
        case .extreme:
            return level >= 11
        }
    }
}

class SunUvView: UIView {
    //Private
    private var currentUvLevel = 0
    private var levelLayers = [CALayer]()
    
    override init(frame: CGRect) {
        super.init(frame:  .zero)
        
        prepareView()
        prepareLevels()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        layer.cornerRadius = (self.bounds.height / 2).rounded()
        updateLevelsFrame()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //Private
    private func updateLavelVisibility() {
        //Skip low level becasue, this level it's view itself
        UVLevel.allCases.forEach {
            levelLayers[$0.rawValue].isHidden = !$0.includedToRange(level: currentUvLevel)
        }
    }
    
    private func updateLevelsFrame() {
        //Skip low level becasue, this level it's view itself
        for index in 1..<levelLayers.count {
            let levelLayer = levelLayers[index]
            let widthCoefficient = 12 * (CGFloat(index) - 1)
            let multiplier = CGFloat(index) / CGFloat(levelLayers.count)
            let diff = self.bounds.width * multiplier
            let targetWidth = self.bounds.width - diff - widthCoefficient
            levelLayer.frame = .init(x: (bounds.width - targetWidth) / 2, y: 0, width: targetWidth, height: bounds.height)
        }
    }
    
    //Public
    public func set(uvLevel:Int) {
        self.currentUvLevel = uvLevel
        updateLavelVisibility()
    }
}

//MARK:- Prepare
private extension SunUvView {
    func prepareView() {
        self.backgroundColor = UIColor(hex: 0xd8d8d8)
        self.clipsToBounds = true
    }
    
    func prepareLevels() {
        UVLevel.allCases.forEach {
            let levelLayer = CALayer()
            levelLayer.backgroundColor = $0.color.cgColor
            self.layer.addSublayer(levelLayer)
            self.levelLayers.append(levelLayer)
        }
    }
}
