//
//  BFSlider.swift
//  BFFramework
//
//  Created by ak on 2021/8/4.
//  功能：自定义 UISlider

import Foundation

class BFUISlider: UISlider {
    
    //slider的value文本
    var valueText:String?{
        didSet {
            valueLabel.text = valueText
            valueLabel.sizeToFit()
            
            let trackRect = convert(bounds, to: nil)
            let thumbRectTemp = thumbRect(forBounds: bounds, trackRect: trackRect, value: value)
            valueLabel.center = CGPoint.init(x: (thumbRectTemp.origin.x - thumbRectTemp.origin.x + thumbRectTemp.size.width / 2), y: -self.frame.size.height)
            
//            CGPointMake((thumbRect.origin.x - trackRect.origin.x + thumbRect.size.width / 2), -self.frame.size.height);
            
//            [super trackRectForBounds:bounds];
//           return CGRectMake(bounds.origin.x, bounds.origin.y + (bounds.size.height - 3.0) / 2, bounds.size.width, 3.0);
       
        }
    }
    override var value: Float{
        didSet {
            sliderValueChanged(sender: self)
        }
    }
    //slider的value字体
    var valueFont:UIFont?
    //slider的value文本颜色
    var valueTextColor:UIColor?
    
    //返回的数值是否为整形
    var valueIsInt:Bool = false
    
    lazy var valueLabel:UILabel = {
        let valueLabel = UILabel.init()
        valueLabel.text = "0"
        valueLabel.textColor = valueTextColor != nil ? valueTextColor : self.thumbTintColor
        valueLabel.font = valueFont != nil ? valueFont : UIFont.systemFont(ofSize: 14);
        valueLabel.textAlignment = .center
        
        return valueLabel
        
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        addTarget(self, action: #selector(sliderTouchDown(sender:)), for: .touchDown)
        addTarget(self, action: #selector(sliderValueChanged(sender:)), for: .valueChanged)
        addTarget(self, action: #selector(sliderTouchUpInside(sender:)), for: .touchUpInside)
 
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        addSubview(valueLabel)
        sliderValueChanged(sender: self)
    }
    
    override func setValue(_ value: Float, animated: Bool) {
        super.setValue(value, animated: animated)
        sliderValueChanged(sender: self)
    }
 
    //重写方法-返回进度条的bounds-修改进度条的高度
    override func trackRect(forBounds bounds: CGRect) -> CGRect {
 
        let bounds = super.trackRect(forBounds: bounds)
        return CGRect.init(origin: CGPoint.init(x: bounds.origin.x, y: bounds.origin.y + (bounds.size.height - 3.0) / 2 ), size: CGSize.init(width:  bounds.size.width, height: 3))
    }
    
    @objc func sliderTouchDown(sender: BFUISlider) {
         
    }
    @objc func sliderValueChanged(sender: BFUISlider) {
        
        if(valueIsInt){
            valueText = "\(Int(sender.value))x"
        }else{
            valueText = "\(sender.value.decimalString())x"
        }
      
 
    }
    @objc func sliderTouchUpInside(sender: BFUISlider) {
         
    }
    
}
