//
//  SCNetWorkDashBoardView.swift
//  superCleaner
//
//  Created by 明津李 on 2020/8/4.
//  Copyright © 2020 Mazy. All rights reserved.
//

import UIKit

class SCNetWorkDashBoardView: UIView {

    lazy var mbpsValueLabel: UILabel = UILabel()
    lazy var nameLabel: UILabel = UILabel()
    
    let boardLayer = ICDbDashboardLayer()
    
    @objc public var progressValue: CGFloat = 0 {
        didSet {
            boardLayer.number = progressValue
            boardLayer.setNeedsDisplay()
        }
    }
    
    @objc public var speedUnit: String = " "
    
    @objc public var mbpsValue: CGFloat = 0 {
        didSet {
        
            let str = String(format: "%.2f", mbpsValue)+speedUnit
            let att = NSMutableAttributedString.init(string: str)
            let range = str.range(of: String(format: "%.2f", mbpsValue))!
            let mbpsRange = str.range(of: speedUnit)!
            att.addAttributes([.font:UIFont.systemFont(ofSize: 36), .foregroundColor:massColor], range: NSRange(range, in: str))
            att.addAttributes([.font:UIFont.systemFont(ofSize: 12), .foregroundColor:UIColor.init(red: 188/255.0, green: 188/255.0, blue: 188/255.0, alpha: 1.0), .baselineOffset:(0.75 * (36 - 12))], range: NSRange(mbpsRange, in: str))
            
            self.mbpsValueLabel.attributedText = att
        }
    }
    
    @objc public var name: String = "" {
        didSet {
            self.nameLabel.text = name;
        }
    }
    
    public override func layoutSubviews() {
        super.layoutSubviews()
        boardLayer.frame = self.bounds
        mbpsValueLabel.frame = self.bounds
        nameLabel.frame = CGRect(x: self.bounds.width / 2 - 50, y: self.bounds.height - 60, width: 100, 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()
        
        mbpsValueLabel.font = UIFont.systemFont(ofSize: 40, weight: .medium)
        mbpsValueLabel.frame = self.bounds
        mbpsValueLabel.textAlignment = .center
        mbpsValueLabel.text = "0"
        mbpsValueLabel.textColor = UIColor.black
        addSubview(mbpsValueLabel)
        
        nameLabel.font = UIFont.systemFont(ofSize: 12)
        nameLabel.frame = CGRect(x: self.bounds.width / 2 - 50, y: self.bounds.height - 60, width: 100, height: 30)
        nameLabel.textAlignment = .center
        nameLabel.text = nil
        nameLabel.textColor = UIColor.lightGray
        nameLabel.numberOfLines = 0;
        addSubview(nameLabel)
    }
}
