//
//  PQCustomSwitchView.swift
//  PQSpeed
//
//  Created by SanW on 2021/5/12.
//  Copyright © 2021 BytesFlow. All rights reserved.
//

import UIKit
import BFCommonKit

class PQCustomSwitchView: UIView {
    /// 当前选中的item
    var currentItemBtn: UIButton?
    /// 按钮点击的回调
    var switchChangeHandle: ((_ sender: UIButton) -> Void)?
 
    var saveBtns:Array<UIButton> = Array.init()
    
    override private init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder _: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    /*
     服务器返回的 （1：快节奏，2：适中，3：慢节奏） ,
     一，快慢速模式下取卡点 2 3 4
     二，跳跃卡点模式下根据不同速度 取卡点 1,2,3
     快节奏为选中区域的所有点位，即0，1，2，3，4 5 6 7 8 9 10 ……
     适中为每两个点位取一个，即0，2，4，6……
     慢节奏为每三个点位取一个，即0，3，6，9……
     */
    init(frame: CGRect, titles: [String], defaultIndex: Int = 1) {
        super.init(frame: frame)
        BFLog(message: "初始时选择的位置 is \(defaultIndex)")
        let itemWidth: CGFloat = frame.width / CGFloat(titles.count)
        for (index, itemTitle) in titles.enumerated() {
            let itemBtn = UIButton(type: .custom)
            //8 是每一个btn 的间隔
            itemBtn.frame = CGRect(x: CGFloat(index) * itemWidth + CGFloat(index) * 8, y: 0, width: itemWidth, height: frame.height)
            itemBtn.tag = (3 - index)
            itemBtn.setTitle(itemTitle, for: .normal)
            itemBtn.setTitleColor(UIColor.hexColor(hexadecimal: "#959595"), for: .normal)
            itemBtn.setTitleColor(UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue), for: .selected)
            itemBtn.addTarget(self, action: #selector(btnClick(sender:)), for: .touchUpInside)
  
            if itemBtn.tag == defaultIndex  {
                itemBtn.isSelected = true
              
                currentItemBtn = itemBtn
                let styleColor = UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)
                itemBtn.backgroundColor = UIColor(red: styleColor.rgbaf[0], green: styleColor.rgbaf[1], blue: styleColor.rgbaf[2], alpha: 0.15)
                
                itemBtn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 13)
            }else{
                itemBtn.backgroundColor = PQBFConfig.shared.pointEditNamalBackgroundColor
                itemBtn.titleLabel?.font = UIFont.systemFont(ofSize: 13)
            }
           
            itemBtn.addCorner(corner:5)
            addSubview(itemBtn)
            
            saveBtns.append(itemBtn)
        }
    }
    
    func selectOneBtn(Index:Int) {
         
        let selectIndex = saveBtns.firstIndex(where: { (svaeBtn) -> Bool in

            (svaeBtn.tag == Index)
        })
        BFLog(message: "选择节奏 \(selectIndex ?? -1)")
        updateSelectBtn(sender: saveBtns[selectIndex ?? 0])
    }
    
    func updateSelectBtn(sender: UIButton) {
        currentItemBtn?.isSelected = false
        currentItemBtn?.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        currentItemBtn?.backgroundColor = PQBFConfig.shared.pointEditNamalBackgroundColor
        
        sender.titleLabel?.font = UIFont.boldSystemFont(ofSize: 13)
        sender.isSelected = true
        let styleColor = UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)
        sender.backgroundColor = UIColor(red: styleColor.rgbaf[0], green: styleColor.rgbaf[1], blue: styleColor.rgbaf[2], alpha: 0.15)
        currentItemBtn = sender
    }
 
    /// 按钮点击事件
    /// - Parameter sender: <#sender description#>
    /// - Returns: <#description#>
    @objc func btnClick(sender: UIButton) {

        updateSelectBtn(sender: sender)
        if switchChangeHandle != nil {
            switchChangeHandle!(sender)
        }
 
    }
}
