//
//  TSDiscoverListCell.swift
//  AIRingtone
//
//  Created by 100Years on 2025/3/17.
//

class TSDiscoverListCell: TSBaseCollectionCell {
    
    static let cellID = "TSDiscoverListCell"
    lazy var setRingBtn: TSUIExpandedTouchButton = {
        let setRingBtn = TSUIExpandedTouchButton()
        setRingBtn.setImage(UIImage(named: "ai_setRing_icon"), for: .normal)
        return setRingBtn
    }()
    
    lazy var ringView: TSRingToneCellView = {
        let ringToneView = TSRingToneCellView()
        ringToneView.backgroundColor = .clear
        ringToneView.playBtn.addTarget(self, action: #selector(clickPlay), for: .touchUpInside)
        return ringToneView
    }()
    
    
    lazy var exampleView: UIView = {
        let exampleView = UIView()
        exampleView.backgroundColor = "#7E57F4".uiColor
        
        let textLabel = UILabel.createLabel(
            text: "Example".localized,
            font: .font(size: 12),
            textColor: .white
        )
        
        exampleView.addSubview(textLabel)
        textLabel.snp.makeConstraints { make in
            make.top.edges.equalTo(UIEdgeInsets(top: 4, left: 6, bottom: 4, right: 6))
        }
        
        kDelayMainShort {
            exampleView.makeCorner([.topLeft,.bottomLeft], radius: 10)
        }
//        exampleView.isHidden = true
        return exampleView
    }()
    
    var ringModel:TSRingModel?{
        didSet{
            guard let ringModel = ringModel else { return }
            ringView.timeLab.text = Float(ringModel.duration).floatToMinuteSecond()
            ringView.nameLab.text = ringModel.title
            ringView.setCoverImageView(urlString: "")
            exampleView.isHidden = true
        }
    }
    
    override var isSelected: Bool{
        didSet{
            ringView.isloading = isSelected
            if isSelected, self.ringView.isPlay == false{
                TSBusinessAudioPlayer.shared.playUrlString( ringModel?.audioUrl)
            }else{
                TSBusinessAudioPlayer.shared.stop()
                backgroundColor = .cardColor
            }
        }
    }
    
    override func creatUI() {
        cornerRadius = 16.0
        backgroundColor = .cardColor
  
        contentView.addSubview(exampleView)
        exampleView.snp.makeConstraints { make in
            make.trailing.equalToSuperview()
            make.top.equalTo(0)
            make.height.equalTo(20)
        }
  
        contentView.addSubview(ringView)
        ringView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    
        contentView.addSubview(setRingBtn)
        setRingBtn.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalTo(-16)
            make.width.height.equalTo(20)
        }
        
        NotificationCenter.default.addObserver(forName: .kBusinessAudioStateChange, object: nil, queue: nil) { notification in
            if let userInfo = notification.userInfo as? [String: TSBusinessAudioPlayer.PlayerState], let state = userInfo["PlayerState"] {
                kExecuteOnMainThread {
                    self.changePlayerState(state: state)
                }
                
            }
        }
    }

    override func dealThings() {
        
    }
    override func renderView(with object: Any?, component: TSCollectionViewComponent, attributes: [String : Any]?) {
        super.renderView(with: object, component: component, attributes: attributes)
        if let itemModel = object as? TSColVVMItemModel{
            if let dataModel = itemModel.dataModel as? TSRingModel{
                ringModel = dataModel
                
                
//                if let ringModel = ringModel {
//                    if ringModel.audioPlayerFileTool == nil {
//                        ringModel.audioPlayerFileTool = TSAudioPlayerFileTool(urlString: ringModel.audioUrl)
//                    }
//                }
                
                
            }
        }
    }

    deinit {
        NotificationCenter.default.removeObserver(self)
    }
}
extension TSDiscoverListCell{
    
    @objc func clickPlay(){
        
        
    }
    
    
    func changePlayerState(state:TSBusinessAudioPlayer.PlayerState){
        if self.isSelected == false {
            self.ringView.isPlay = false
            return
        }
        
        switch state {
        case .loading(let progress):
            if progress == 0.0 {
                self.ringView.isloading = true
            }else if progress == 1.0 {
                self.ringView.isloading = false
            }
        case .play:
            self.ringView.isPlay = true
            backgroundColor = "#3C213F".uiColor
        case .stop:
            self.ringView.isPlay = false
            backgroundColor = .cardColor
        default:
            break
        }
    }
}
