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

import Foundation
import UIKit

public class ICNetworkDashBoard: UIView {
    
    let pointerImage: UIImageView = UIImageView(image: UIImage(named: "new_network_pointer"))
    
    let imageLayer = CALayer()
    
    @objc public var progressValue: CGFloat = 0 {
        didSet {
            boardLayer.number = progressValue
            boardLayer.setNeedsDisplay()
            
            let endAngle = progressValue * CGFloat.pi * 1.5 + CGFloat.pi * 1.25
            pointerImage.transform = CGAffineTransform(rotationAngle: endAngle)
        }
    }
    let boardLayer = DashboardLayer()
    
    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
        layer.addSublayer(boardLayer)
        boardLayer.contentsScale = UIScreen.main.scale
        boardLayer.setNeedsDisplay()
        
        pointerImage.frame = self.bounds
        pointerImage.contentMode = .center
        addSubview(pointerImage)
    }
    
    public override func layoutSubviews() {
        super.layoutSubviews()
        boardLayer.frame = self.bounds
        pointerImage.frame = self.bounds
    }
}


class DashboardLayer: 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)
        ctx.setLineWidth(5)
        ctx.setStrokeColor(UIColor(red: 225/255, green: 225/255, blue: 225/255, alpha: 1).cgColor)
        ctx.setLineCap(.round)
        ctx.addArc(center: center, radius: radius, startAngle: CGFloat.pi * 0.75, endAngle: CGFloat.pi * 0.25, clockwise: false)
        ctx.strokePath()
        
        let endAngle = self.number * CGFloat.pi * 1.5 + CGFloat.pi * 0.75
        ctx.setLineWidth(5)
        ctx.setStrokeColor(massColor.cgColor)
        ctx.setLineCap(.round)
        ctx.addArc(center: center, radius: radius, startAngle: CGFloat.pi * 0.75, endAngle: endAngle, clockwise: false)
        ctx.strokePath()
        
        ctx.setLineWidth(4)
        ctx.setStrokeColor(massColor.cgColor)
//        ctx.setLineCap(.round)
        // phase表示开始绘制之前跳过多少点进行绘制，默认一般设置为0
        // lengths通常都包含两个数字，第一个是绘制的宽度，第二个表示跳过的宽度，也可以设置多个
        let aaa = (CGFloat.pi * 3 / 2) * (radius-5) / 7 + 1
        //((CGFloat.pi * 3 / 2) / 7) * (radius - 10)
        ctx.setLineDash(phase: 0, lengths: [1, aaa])
        
        ctx.addArc(center: center, radius: radius - 10, startAngle: CGFloat.pi * 0.75, endAngle: endAngle, clockwise: false)
        ctx.strokePath()
    }
}
