//
//  PQCustomSpeedSettingView.swift
//  BFFramework
//
//  Created by ak on 2021/8/3.
//  功能：自定义速度界面

import Foundation

class PQCustomSpeedSettingView: UIView {
    // 左上角返回
    lazy var backBtn: BFUIButton = {
        let backBtn = BFUIButton(type: .custom)
        backBtn.addTarget(self, action: #selector(backClick(sender:)), for: .touchUpInside)
        backBtn.setImage(UIImage().BF_Image(named: "customSpeedClosed"), for: .normal)
        backBtn.adjustsImageWhenHighlighted = false
        return backBtn
    }()

    // 标题
    public lazy var titleLab: UILabel = {
        let titleLab = UILabel()
        titleLab.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        titleLab.textAlignment = .center
        titleLab.text = "自定义快慢速"
        titleLab.textColor = PQBFConfig.shared.styleTitleColor
        return titleLab
    }()

    // 确定
    public lazy var confirmBtn: UIButton = {
        let confirmBtn = UIButton(type: .custom)
        confirmBtn.setTitle("确定", for: .normal)
        confirmBtn.setTitleColor(.white, for: .normal)
        confirmBtn.addTarget(self, action: #selector(confirmClick(sender:)), for: .touchUpInside)
        confirmBtn.backgroundColor = UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)
        confirmBtn.titleLabel?.font = UIFont.systemFont(ofSize: 17, weight: .medium)
        confirmBtn.addCorner(corner: 11)
        return confirmBtn
    }()

    // 取消
    public lazy var cancleBtn: UIButton = {
        let cancleBtn = UIButton(type: .custom)
        cancleBtn.setTitle("取消", for: .normal)
        cancleBtn.setTitleColor(UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue), for: .normal)
        cancleBtn.backgroundColor = PQBFConfig.shared.pointEditNamalBackgroundColor
        cancleBtn.addTarget(self, action: #selector(backClick(sender:)), for: .touchUpInside)
        cancleBtn.titleLabel?.font = UIFont.systemFont(ofSize: 17, weight: .medium)
        cancleBtn.addCorner(corner: 11)
        return cancleBtn
    }()

    // 提示1
    lazy var customSpeedFastView: UIImageView = {
        let customSpeedFastView = UIImageView(image: UIImage().BF_Image(named: "customSpeedFast"))
        return customSpeedFastView
    }()

    // 提示2
    lazy var customSpeedSlowView: UIImageView = {
        let customSpeedSlowView = UIImageView(image: UIImage().BF_Image(named: "customSpeedSlow"))
        return customSpeedSlowView
    }()

    lazy var fastSlider: BFUISlider = {
        let fastSlider = BFUISlider()
        let thbImage = UIImage.moduleImage(named: PQBFConfig.shared.silderPinUsedImageName, moduleName: "BFFramework", isAssets: false)
        fastSlider.setMinimumTrackImage(thbImage, for: .normal)
        fastSlider.setMaximumTrackImage(thbImage, for: .normal)
        fastSlider.setThumbImage(thbImage, for: .highlighted)
        fastSlider.setThumbImage(thbImage, for: .normal)
        fastSlider.maximumTrackTintColor = UIColor.hexColor(hexadecimal: "#E6E8E8")
        fastSlider.minimumTrackTintColor = UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)
        fastSlider.tag = 10
//        speechSlidView.addTarget(self, action: #selector(sliderTouchEnded(sender:)), for: .touchUpInside)
        fastSlider.maximumValue = 8
        fastSlider.minimumValue = 0.2
        fastSlider.valueTextColor = UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)
        fastSlider.value = 0.2

        return fastSlider
    }()

    lazy var slowSlider: BFUISlider = {
        let slowSlider = BFUISlider()
        let thbImage =  UIImage.moduleImage(named: PQBFConfig.shared.silderPinUsedImageName, moduleName: "BFFramework", isAssets: false)
        slowSlider.setMinimumTrackImage(thbImage, for: .normal)
        slowSlider.setMaximumTrackImage(thbImage, for: .normal)
        slowSlider.setThumbImage(thbImage, for: .highlighted)
        slowSlider.setThumbImage(thbImage, for: .normal)
        slowSlider.maximumTrackTintColor = UIColor.hexColor(hexadecimal: "#E6E8E8")
        slowSlider.minimumTrackTintColor = UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)
        slowSlider.tag = 10
//        speechSlidView.addTarget(self, action: #selector(sliderTouchEnded(sender:)), for: .touchUpInside)
        slowSlider.maximumValue = 4
        slowSlider.minimumValue = 0.2
        slowSlider.valueTextColor = UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)
        slowSlider.value = 0.2

        return slowSlider
    }()

    lazy var jumpSpeedSlider: BFUISlider = {
        let jumpSpeedSlider = BFUISlider()
        let thbImage =  UIImage.moduleImage(named: PQBFConfig.shared.silderPinUsedImageName, moduleName: "BFFramework", isAssets: false)
        jumpSpeedSlider.setMinimumTrackImage(thbImage, for: .normal)
        jumpSpeedSlider.setMaximumTrackImage(thbImage, for: .normal)
        jumpSpeedSlider.setThumbImage(thbImage, for: .highlighted)
        jumpSpeedSlider.setThumbImage(thbImage, for: .normal)
        jumpSpeedSlider.maximumTrackTintColor = UIColor.hexColor(hexadecimal: "#E6E8E8")
        jumpSpeedSlider.minimumTrackTintColor = UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)
        jumpSpeedSlider.tag = 10
//        speechSlidView.addTarget(self, action: #selector(sliderTouchEnded(sender:)), for: .touchUpInside)
        jumpSpeedSlider.maximumValue = 10
        jumpSpeedSlider.minimumValue = 1
        jumpSpeedSlider.valueTextColor = UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)
        jumpSpeedSlider.value = 1

        return jumpSpeedSlider
    }()
 
    // view 初化的类型 1, 快慢速度卡点  2，跳跃卡点 ,3,循环设置
    var viewType: Int = 0 {
        didSet{
            showHiddenView()
            if(viewType == 1){
                titleLab.text = "自定义快慢速"
            }else if(viewType == 2){
                titleLab.text = "自定义跳跃快慢速"
            }else if(viewType == 3){
                titleLab.text = "自定义循环"
                jumpSpeedSlider.maximumValue = 20
                jumpSpeedSlider.valueIsInt = true
            }
        }
    }

    // 点击完成回调
    public var selectSpeedCallBack: ((_ fastSpeed: Float, _ slowSpeed: Float, _ viewType: Int , _ isCancle :Bool) -> Void)?

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = PQBFConfig.shared.otherTintColor
       
        layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.15).cgColor
        layer.shadowOpacity = 1
        layer.shadowOffset = CGSize(width: 0, height: -6)
        layer.shadowRadius = 30
        // 切圆角
        layer.cornerRadius = 15

        addSubview(backBtn)
        addSubview(titleLab)
        addSubview(confirmBtn)
        addSubview(cancleBtn)
        addSubview(customSpeedFastView)
        addSubview(customSpeedSlowView)
        addSubview(fastSlider)
        addSubview(slowSlider)
        addSubview(jumpSpeedSlider)

        autolayout()
        showHiddenView()
        self.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(panGes(_:))))
    }

    required init?(coder _: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit {
        BFLog(1, message: "custom speed setting view release")
    }

    @objc func panGes(_ ges:UIPanGestureRecognizer){
        
    }
    
    func showHiddenView() {
        customSpeedFastView.isHidden = !(viewType == 1)
        customSpeedSlowView.isHidden =  !(viewType == 1)
        fastSlider.isHidden =  !(viewType == 1)
        slowSlider.isHidden =  !(viewType == 1)
        jumpSpeedSlider.isHidden = (viewType == 1)
    }

    func autolayout() {
        backBtn.snp.makeConstraints { make in
            make.height.width.equalTo(24)
            make.left.equalToSuperview().offset(16)
            make.top.equalToSuperview().offset(10)
        }

        titleLab.snp.makeConstraints { make in
            make.height.equalTo(20)
            make.width.equalTo(150)
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(10)
        }
        cancleBtn.snp.makeConstraints { make in
            make.height.equalTo(50)
            make.width.equalTo(61)
            make.left.equalToSuperview().offset(19)
            make.bottom.equalToSuperview().offset(-60)
        }

        confirmBtn.snp.makeConstraints { make in
            make.height.equalTo(50)
            make.width.equalTo(254)
            make.right.equalToSuperview().offset(-30)
            make.top.equalTo(cancleBtn.snp.top)
        }

        customSpeedFastView.snp.makeConstraints { make in
            make.height.width.equalTo(30)
            make.left.equalToSuperview().offset(18)
            make.top.equalToSuperview().offset(98)
        }

        customSpeedSlowView.snp.makeConstraints { make in
            make.height.width.equalTo(30)
            make.left.equalTo(customSpeedFastView.snp.left)
            make.top.equalTo(customSpeedFastView.snp.bottom).offset(31)
        }

        fastSlider.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(65)
            make.right.equalToSuperview().offset(-36)
            make.centerY.equalTo(customSpeedFastView.snp.centerY)
        }
        slowSlider.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(65)
            make.right.equalToSuperview().offset(-36)
            make.centerY.equalTo(customSpeedSlowView.snp.centerY)
        }

        jumpSpeedSlider.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(30)
            make.right.equalToSuperview().offset(-36)
            make.top.equalTo(titleLab.snp.bottom).offset(104)
        }
    }

    // 返回
    @objc func backClick(sender _: UIButton) {
        isHidden = true
        selectSpeedCallBack!(jumpSpeedSlider.value, 0, viewType,true)

    }

    // 确认
    @objc func confirmClick(sender _: UIButton) {
        isHidden = true
        BFLog(message: "fastSlider: \(fastSlider.value.decimalNumber()) slowSlider: \(slowSlider.value.decimalNumber()) jumpSpeedSlider:\(jumpSpeedSlider.value.decimalNumber(0))")
        if selectSpeedCallBack != nil {
            if(viewType == 1){
                selectSpeedCallBack!(fastSlider.value.decimalNumber(), slowSlider.value.decimalNumber(), viewType,false)
            }else{
                selectSpeedCallBack!(jumpSpeedSlider.value.decimalNumber(0), 0, viewType,false)
            }
        
        }
    }
}
