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

class TSRingToneCellView: TSBaseView {
    
    
    var clickPlayHandel:((Bool)->Void)?
    
    let coverImageView:UIImageView = UIImageView()
    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)
        }
    }
    
    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 = 8
        coverImageView.backgroundColor = .black
        
        contentView.addSubview(coverImageView)
        coverImageView.addSubview(playBtn)
        contentView.addSubview(nameLab)
        contentView.addSubview(timeLab)
        
        
        playBtn.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.height.equalTo(24)
        }
    
        coverImageView.snp.makeConstraints { make in
            make.leading.equalTo(12)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(48)
        }
        
        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)
        }
        
        
//        clearBtn.backgroundColor = .yellow.withAlphaComponent(0.2)
        contentView.addSubview(clearBtn)
        clearBtn.snp.makeConstraints { make in
            make.leading.top.bottom.equalToSuperview()
//            make.width.equalTo(72)
            make.trailing.equalTo(0)
        }
    }
    
    
    func setCoverImageView(urlString:String){
        coverImageView.setAsyncImage(urlString: urlString,placeholder:kRingPlaceholderImage,contentMode: .scaleAspectFill,showLoading: false)
    }

}
