//
//  TSRingToneCellView.swift
//  AIRingtone
//
//  Created by 100Years on 2025/3/3.
//

class TSRingToneCellView: TSBaseView {
    
    
    var clickPlayHandel:((Bool)->Void)?
    
    let coverImageView:UIImageView = UIImageView()
    
    lazy var progressView: TSCircularProgressView = {
        let progressView = TSCircularProgressView(frame: CGRect(x: 0, y: 0, width: 49, height: 49))
        progressView.trackColor = .clear
        progressView.progressColor = .white
        progressView.lineWidth = 2.0 // 设置线条粗细
        progressView.isClockwise = true // 设置为逆时针
        progressView.progress = 0.0
        return progressView
    }()
    
    let playBtn = UIButton.createButton(image: UIImage(named: "ringtone_play"))
    let nameLab = UILabel.createLabel(text: "--",font: .font(size: 14),textColor: .white)
    let timeLab = UILabel.createLabel(text:"--:--",font: .font(size: 12),textColor: .white.withAlphaComponent(0.6))
    
    
    
    lazy var clearBtn = UIButton.createButton{[weak self]  in
        guard let self = self else { return }
        clickPlayHandel?(self.isPlay)
    }
    
    var isPlay:Bool = false {
        didSet{
            playBtn.stopRotating()
            playBtn.setImage(UIImage(named: isPlay ? "ringtone_pause" : "ringtone_play"), for: .normal)
            backgroundColor = isPlay ? "#3C213F".uiColor : .cardColor
        }
    }
    
    var isloading:Bool = false {
        didSet{
            if isloading {
                playBtn.setImage(UIImage(named:"ringtone_loading"), for: .normal)
                playBtn.startRotating()
            }else{
                let b = isPlay
                isPlay = b
                playBtn.stopRotating()
            }
        }
    }
    
    override func creatUI() {
        backgroundColor = .cardColor
        cornerRadius = 16
        coverImageView.cornerRadius = 24
        coverImageView.backgroundColor = .black
        
        contentView.addSubview(coverImageView)
        contentView.addSubview(progressView)
        coverImageView.addSubview(playBtn)
        contentView.addSubview(nameLab)
        contentView.addSubview(timeLab)
        
        coverImageView.snp.makeConstraints { make in
            make.leading.equalTo(12)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(48)
        }

        progressView.snp.makeConstraints { make in
            make.leading.equalTo(11)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(progressView.width)
        }
        
        playBtn.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.height.equalTo(24)
        }

        nameLab.snp.makeConstraints { make in
            make.leading.equalTo(76)
            make.trailing.equalTo(-60)
            make.top.equalTo(17)
        }
        
        timeLab.snp.makeConstraints { make in
            make.leading.equalTo(76)
            make.bottom.equalTo(-17)
            make.height.equalTo(12)
        }
        
        let rightShade = UIImageView.createImageView(imageName: "play_right_shade",contentMode: .scaleAspectFill)
        contentView.addSubview(rightShade)
        rightShade.snp.makeConstraints { make in
            make.trailing.top.bottom.equalToSuperview()
            make.width.equalTo(103*kDesignScale)
        }
        
        
        contentView.addSubview(clearBtn)
        clearBtn.snp.makeConstraints { make in
            make.leading.top.bottom.equalToSuperview()
            make.trailing.equalTo(0)
        }
    }
    
    
    func setCoverImageView(urlString:String){
        coverImageView.setAsyncImage(urlString: urlString,placeholder:kRingPlaceholderImage,contentMode: .scaleAspectFill,showLoading: false)
    }
    
}

extension TSRingToneCellView {
    
    func monitorPlayStateDefaultHandle(){
        TSBusinessAudioPlayer.shared.stateChangedHandle = { [weak self] state  in
            guard let self = self else { return }
            self.handelAudioPlayerStateChange(state: state)
        }
    }
    
    func handelAudioPlayerStateChange(state:TSBusinessAudioPlayer.PlayerState){
        switch state {
        case .loading(let progress):
            if progress == 0.0 {
                self.isloading = true
            }else if progress == 1.0 {
                self.isloading = false
            }
        case .play:
            self.isPlay = true
//            backgroundColor = "#3C213F".uiColor
        case .stop:
            self.isloading = false
            self.isPlay = false
            self.progressView.progress = 0.0
//            backgroundColor = .cardColor
        case .currentTime(_):
            self.progressView.progress =  CGFloat(TSBusinessAudioPlayer.shared.playProgress)
        default:
            break
        }
    }
    
}


class TSRingToneGenerateView:TSBaseView {
    
    lazy var generateProgressView: UIImageView = {
        let generateProgressView = UIImageView.createImageView(imageName: "ringGenerateProgress",contentMode: .scaleToFill)
        return generateProgressView
    }()
    
    lazy var iconImageView: UIImageView = {
        let iconImageView = UIImageView.createImageView(imageName: "ring_icon",contentMode: .scaleToFill)
        return iconImageView
    }()
    

    lazy var infoLabel: UILabel = {
        let infoLabel = UILabel.createLabel(font: .font(size: 14),textColor: .white)
        return infoLabel
    }()
    
    lazy var refreshBtn: TSUIExpandedTouchButton = {
        let refreshBtn = TSUIExpandedTouchButton()
        refreshBtn.setUpButton(image: UIImage(named: "refresh_white")){
            [weak self]  in
            guard let self = self else { return }
        }
        refreshBtn.isHidden = true
        return refreshBtn
    }()
    
    
    override func creatUI() {
        backgroundColor = .cardColor
        
        contentView.addSubview(generateProgressView)
        generateProgressView.snp.makeConstraints { make in
            make.leading.top.bottom.equalTo(0)
            make.width.equalToSuperview().multipliedBy(0.0)
        }
        
        contentView.addSubview(iconImageView)
        iconImageView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.width.height.equalTo(48.0)
            make.leading.equalTo(12.0)
        }
        
        contentView.addSubview(infoLabel)
        infoLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalTo(iconImageView.snp.trailing).offset(16)
            make.trailing.equalTo(-50)
        }
        
        contentView.addSubview(refreshBtn)
        refreshBtn.snp.makeConstraints { make in
            make.trailing.equalTo(-16)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(20.0)
        }
        
    }
}
