//
//  TSGennerateCellView.swift
//  KittensTravelNotes
//
//  Created by 100Years on 2025/7/7.
//



let progressH:CGFloat = 6.0
class TSGennerateCellProgressView: TSBaseView {
    
    lazy var generateProgressBgView: UIView = {
        let generateProgressBgView = UIView()
        generateProgressBgView.backgroundColor = "#01E2FF".uiColor.withAlphaComponent(0.2)
        generateProgressBgView.cornerRadius = progressH/2.0
        return generateProgressBgView
    }()
    
    lazy var generateProgressView: UIImageView = {
        let generateProgressView = UIImageView.createImageView(image: .gennerateProgress,contentMode: .scaleToFill)
        generateProgressView.cornerRadius = progressH/2.0
        return generateProgressView
    }()
    
    lazy var iconView: UIImageView = {
        let iconView = UIImageView.createImageView(image: .gennerateProgressHeader,contentMode: .scaleToFill)
        return iconView
    }()
    
    override func creatUI() {

        contentView.snp.remakeConstraints { make in
            make.edges.equalToSuperview()
            make.height.equalTo(24)
        }
        
        contentView.addSubview(generateProgressBgView)
        generateProgressBgView.snp.remakeConstraints { make in
            make.leading.bottom.trailing.equalToSuperview()
            make.height.equalTo(progressH)
        }
        
        generateProgressBgView.addSubview(generateProgressView)
        generateProgressView.snp.remakeConstraints { make in
            make.edges.equalToSuperview()
        }

        let maskLayer = CAShapeLayer()
        maskLayer.path = UIBezierPath(
            roundedRect: CGRect(x: 0, y: 0, width: self.width * CGFloat(progress) , height: progressH),
            cornerRadius: progressH/2.0
        ).cgPath
        generateProgressView.layer.mask = maskLayer
        
        contentView.addSubview(iconView)
        iconView.snp.remakeConstraints { make in
            make.bottom.leading.equalTo(0)
            make.width.height.equalTo(24)
        }
        
    }

    func setProgress(progress:Float) {
        let width:CGFloat = self.width * CGFloat(progress)
        if let maskLayer = generateProgressView.layer.mask as? CAShapeLayer {
            maskLayer.path = UIBezierPath(
                roundedRect: CGRect(x: 0, y: 0, width: width , height: progressH),
                cornerRadius: progressH/2.0
            ).cgPath
        }
        
        
        var leading:CGFloat = width - 24.0
        if leading < 0 {
            leading = 0
        }
  
        iconView.snp.updateConstraints { make in
            make.leading.equalTo(leading)
        }
    }
    
    
    var progress:Float = 0.0{
        didSet{
            setProgress(progress: progress)
        }
    }
    
}

class TSGennerateCellView: TSBaseView {
    var clickHandel:((Int)->Void)?  //0 退出 1 重试 2 删除
    lazy var infoLabel: UILabel = {
        let infoLabel = UILabel.createLabel(font: .font(size: 14,weight: .medium),textColor: .mainText,textAlignment: .center,numberOfLines: 0)
        return infoLabel
    }()
    private var isCancelClick:Bool = false
    
    lazy var refreshBtn: TSUIExpandedTouchButton = {
        let refreshBtn = TSUIExpandedTouchButton()
        refreshBtn.setUpButton(title: "Cancel".localized,font: .font(size: 12),titleColor: .lesserText,corner: 10)
        {[weak self]  in
            guard let self = self else { return }
            clickHandel?(isCancelClick ? 0 : 1)
        }
        refreshBtn.contentEdgeInsets = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 8)
        refreshBtn.layer.borderColor = UIColor.lesserText.cgColor
        refreshBtn.layer.borderWidth = 1
        return refreshBtn
    }()
    
    let failedImageView = UIImageView.createImageView(image:.generatorFailed)
    
    lazy var deleteBtn: TSUIExpandedTouchButton = {
        let deleteBtn = TSUIExpandedTouchButton()
        deleteBtn.isHidden = true
        deleteBtn.setUpButton(image: .delete)
        {[weak self]  in
            guard let self = self else { return }
            clickHandel?(2)
        }
        return deleteBtn
    }()
    
    var titleTop:CGFloat = 86.0 {
        didSet{
            infoLabel.snp.updateConstraints { make in
                make.top.equalTo(titleTop)
            }
        }
    }
    
    lazy var locationLabel: UILabel = {
        let locationLabel = UILabel.createLabel(font: .font(size: 12),textColor: .mainText,textAlignment: .right)
        return locationLabel
    }()
    
    let progressView:TSGennerateCellProgressView = TSGennerateCellProgressView()
    

    
    override func creatUI() {
        let bgImageView = UIImageView.createImageView(image: .gennerateCellBg,contentMode: .scaleAspectFill)
        contentView.addSubview(bgImageView)
        bgImageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        let topView = UIView()
        contentView.addSubview(topView)
        topView.snp.makeConstraints { make in
            make.top.leading.trailing.equalToSuperview()
            make.height.equalTo(48*kDesignScale)
        }
 
        let toLabel = UILabel.createLabel(text: "To".localized,font: .font(size: 12),textColor: .lesserText)
        topView.addSubview(toLabel)
        toLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalTo(8)
            make.width.equalTo(30)
        }
        
        topView.addSubview(locationLabel)
        locationLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalTo(-8)
            make.leading.equalTo(40)
        }
        
        
        
        contentView.addSubview(progressView)
        contentView.addSubview(failedImageView)
        contentView.addSubview(infoLabel)
        contentView.addSubview(refreshBtn)
        contentView.addSubview(deleteBtn)
        
        progressView.snp.makeConstraints { make in
            make.bottom.equalTo(infoLabel.snp.top).offset(-12.0)
            make.leading.equalTo(10)
            make.trailing.equalTo(-10)
        }
    
        failedImageView.snp.makeConstraints { make in
            make.bottom.equalTo(infoLabel.snp.top).offset(-4.0)
            make.centerX.equalToSuperview()
            make.width.height.equalTo(40)
        }
        
  
        infoLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()//.offset(18)
            make.leading.equalTo(16)
            make.trailing.equalTo(-16)
        }


        refreshBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(infoLabel.snp.bottom).offset(8)
            make.height.equalTo(20)
        }
        
        deleteBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(refreshBtn.snp.bottom).offset(33)
            make.width.height.equalTo(20)
        }
    }
    
    func updateInfoLabelCenterY() {
//        infoLabel.snp.updateConstraints { make in
//            make.centerY.equalToSuperview().offset(refreshBtn.isHidden ? 0 : -14.0)
//        }
    }
    
    private func setProgressData(progress:Float) {
        let progressInt = Int(progress*100)
        progressView.isHidden = false
        progressView.progress = progress
        infoLabel.text = "Traveling to place".localized + "\n\n" + kPercentlocalized(progressInt) + "..."
    }
    
    func setProgress(progress:Float) {
        isCancelClick = true
        
        setProgressData(progress: progress)

        updateInfoLabelCenterY()
        
        deleteBtn.isHidden = true
        refreshBtn.isHidden = false
        refreshBtn.setTitle("Cancel".localized, for: .normal)
        failedImageView.isHidden = true
    }
    
    
    func setTaskExpired(){
        isCancelClick = false
        setProgress(progress: 0.0)
        infoLabel.text = "This task has expired".localized
        
        refreshBtn.setImage(UIImage(named: "ai_delete"), for: .normal)
        
        updateInfoLabelCenterY()
        
        progressView.isHidden = true
        deleteBtn.isHidden = false
        refreshBtn.isHidden = true
        failedImageView.isHidden = true
    }

    func setFailText(text:String,refresh:Bool = true){
        isCancelClick = false
        
        setProgressData(progress: 0.0)
        infoLabel.text = "Generation failed".localized
        progressView.isHidden = true
        deleteBtn.isHidden = false
        refreshBtn.isHidden = false
        refreshBtn.setTitle("Try again".localized, for: .normal)
        failedImageView.isHidden = false
        updateInfoLabelCenterY()
    }
}
