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

import UIKit

class SunUvLineView: UIView {
    //Private
    private let kLineWidth:CGFloat =  1
    private let leftCircle = CAShapeLayer()
    private let rightCircle = CAShapeLayer()
    private let line = CAShapeLayer()
    
    override init(frame: CGRect) {
        super.init(frame: .zero)
        
        prepareView()
        prepareLayers()
        updateUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        updateUI()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let radius = ((bounds.height - kLineWidth) / 2).rounded(.down)
        leftCircle.path = UIBezierPath(arcCenter: .init(x: radius + kLineWidth, y: bounds.height / 2),
                                       radius: radius,
                                       startAngle: 0,
                                       endAngle: 2 * .pi,
                                       clockwise: false).cgPath
        rightCircle.path = UIBezierPath(arcCenter: .init(x: bounds.width - radius - kLineWidth, y: bounds.height / 2),
                                        radius: radius,
                                        startAngle: 0,
                                        endAngle: 2 * .pi,
                                        clockwise: false).cgPath
        let linePath = UIBezierPath()
        linePath.move(to: .init(x: (radius * 2) + kLineWidth, y: bounds.height / 2))
        linePath.addLine(to: .init(x: bounds.width - (radius * 2) - kLineWidth, y: bounds.height / 2))
        line.path = linePath.cgPath
    }
    
    //Private
    private func updateUI() {
        switch interfaceStyle {
        case .light:
            leftCircle.strokeColor = UIColor(hex: 0x3452aa).withAlphaComponent(0.5).cgColor
            rightCircle.strokeColor = UIColor(hex: 0x3452aa).withAlphaComponent(0.5).cgColor
            line.strokeColor = UIColor(hex: 0x3452aa).withAlphaComponent(0.5).cgColor
        case .dark:
            leftCircle.strokeColor = UIColor(hex: 0xcae0ec).cgColor
            rightCircle.strokeColor = UIColor(hex: 0xcae0ec).cgColor
            line.strokeColor = UIColor(hex: 0xcae0ec).cgColor
        }
    }
}

//MARK:- Prepare
private extension SunUvLineView {
    func prepareView() {
        clipsToBounds = true
        backgroundColor = .clear
    }
    
    func prepareLayers() {
        leftCircle.lineWidth = kLineWidth
        leftCircle.fillColor = UIColor.clear.cgColor
        layer.addSublayer(leftCircle)
        
        rightCircle.lineWidth = kLineWidth
        rightCircle.fillColor = UIColor.clear.cgColor
        layer.addSublayer(rightCircle)
        
        line.lineWidth = kLineWidth
        line.fillColor = UIColor.clear.cgColor
        layer.addSublayer(line)
    }
}
