//
//  TSAIRintoneHistoryCell.swift
//  AIRingtone
//
//  Created by 100Years on 2025/3/6.
//

import SwipeCellKit
class TSAIRintoneHistoryCell: SwipeCollectionViewCell {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        creatUI()
    }
    
    required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    static let cellID = "TSAIRintoneHistoryCell"

    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
        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 model:TSActionInfoModel?{
        didSet{
            guard let model = model else { return }
            ringView.timeLab.text = Float(model.request.duration).floatToMinuteSecond()
            ringView.nameLab.text = model.response.title
            ringView.setCoverImageView(urlString: model.response.coverUrl)
            exampleView.isHidden = model.modelType != .example
        }
    }
    
    override var isSelected: Bool{
        didSet{
            ringView.isloading = isSelected
            if isSelected, self.ringView.isPlay == false{
                TSBusinessAudioPlayer.shared.playRingtone(ringtone: model?.response.musicUrl)
            }else{
                TSBusinessAudioPlayer.shared.stop()
                backgroundColor = .cardColor
            }
        }
    }
    
    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)
                }
                
            }
        }
    }
    
    
    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
        }
    }

    deinit {
        NotificationCenter.default.removeObserver(self)
    }
}

class TSAIRintoneHistorySectionHeaderView: UICollectionReusableView {
    static let reuseIdentifier = "TSAIRintoneHistorySectionHeaderView"
    
    let titleView: TSTGPTitleView = {
        let titleView = TSTGPTitleView()
        return titleView
    }()
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(titleView)
        titleView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
