//
//  ICDbDashboardView.swift
//  TestCAShapeLayer
//
//  Created by Mazy on 2020/7/30.
//  Copyright © 2020 Mazy. All rights reserved.
//

import Foundation
import UIKit

public class ICDbDashboardView: UIView {
    
    lazy var dbValueLabel: UILabel = UILabel()
    lazy var minValueLabel: UILabel = UILabel()
    lazy var maxValueLabel: UILabel = UILabel()
    
    let boardLayer = ICDbDashboardLayer()
    
    @objc public var progressValue: CGFloat = 0 {
        didSet {
            boardLayer.number = progressValue
            boardLayer.setNeedsDisplay()
        }
    }
    
   @objc public var dbValue: CGFloat = 0 {
        didSet {
            self.dbValueLabel.text = String(format: "%.0fdb", dbValue)
        }
    }
    
    public override func layoutSubviews() {
        super.layoutSubviews()
        boardLayer.frame = self.bounds
        dbValueLabel.frame = self.bounds
        minValueLabel.frame = CGRect(x: self.bounds.width / 2 - 50, y: self.bounds.height / 2 + 45, width: 100, height: 30)
        maxValueLabel.frame = CGRect(x: self.bounds.width / 2 - 50, y: self.bounds.height / 2 + 45, width: 105, height: 30)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    public override func awakeFromNib() {
        super.awakeFromNib()
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupUI()
    }
    
    func setupUI() {
        boardLayer.frame = self.bounds
        boardLayer.number = 0.5
        boardLayer.contentsScale = UIScreen.main.scale
        layer.addSublayer(boardLayer)
        boardLayer.setNeedsDisplay()
        
        dbValueLabel.font = UIFont.systemFont(ofSize: 40, weight: .medium)
        dbValueLabel.frame = self.bounds
        dbValueLabel.textAlignment = .center
        dbValueLabel.text = "0db"
        dbValueLabel.textColor = UIColor.black
        addSubview(dbValueLabel)
        
        minValueLabel.font = UIFont.systemFont(ofSize: 12)
        minValueLabel.frame = CGRect(x: self.bounds.width / 2 - 50, y: self.bounds.height / 2 + 45, width: 100, height: 30)
        minValueLabel.textAlignment = .left
        minValueLabel.text = "0db"
        minValueLabel.textColor = UIColor.lightGray
        addSubview(minValueLabel)
        
        maxValueLabel.font = UIFont.systemFont(ofSize: 12)
        maxValueLabel.frame = CGRect(x: self.bounds.width / 2 - 50, y: self.bounds.height / 2 + 45, width: 105, height: 30)
        maxValueLabel.textAlignment = .right
        maxValueLabel.text = "110db"
        maxValueLabel.textColor = UIColor.lightGray
        addSubview(maxValueLabel)
    }
}


class ICDbDashboardLayer: CALayer {
    
    var number: CGFloat = 0
    
    override func draw(in ctx: CGContext) {
        
        let radius = self.frame.width * 0.45
        let center = CGPoint(x: self.frame.width * 0.5, y: self.frame.height * 0.5)

        let endAngle = self.number * CGFloat.pi * 1.5 + CGFloat.pi * 0.75

        ctx.setLineWidth(24)
        ctx.setStrokeColor(UIColor(hexString: "E5E5E5").cgColor)
        ctx.setLineCap(.butt)
        ctx.addArc(center: center, radius: radius - 10, startAngle: CGFloat.pi * 0.75, endAngle: CGFloat.pi * 0.25, clockwise: false)
        // phase表示开始绘制之前跳过多少点进行绘制，默认一般设置为0
        ctx.setLineDash(phase: 0, lengths: [3, 10])
        ctx.strokePath()
        
        ctx.setLineWidth(24)
        ctx.setStrokeColor(massColor.cgColor)
        ctx.setLineCap(.butt)
        ctx.addArc(center: center, radius: radius - 10, startAngle: CGFloat.pi * 0.75, endAngle: endAngle, clockwise: false)
        // phase表示开始绘制之前跳过多少点进行绘制，默认一般设置为0
        // lengths通常都包含两个数字，第一个是绘制的宽度，第二个表示跳过的宽度，也可以设置多个
        ctx.setLineDash(phase: 0, lengths: [3, 10])
        ctx.strokePath()
        
        ctx.setLineWidth(5)
        ctx.setStrokeColor(UIColor(hexString: "E5E5E5").cgColor)
        ctx.setLineCap(.butt)
        ctx.addArc(center: center, radius: radius - 30, startAngle: CGFloat.pi * 0.75, endAngle: CGFloat.pi * 0.25, clockwise: false)
        // phase表示开始绘制之前跳过多少点进行绘制，默认一般设置为0
        // lengths通常都包含两个数字，第一个是绘制的宽度，第二个表示跳过的宽度，也可以设置多个
        ctx.setLineDash(phase: 0, lengths: [3, 10])
        ctx.strokePath()
    }
}
