//
//  Untitled.swift
//  AIEmoji
//
//  Created by 100Years on 2025/5/12.
//

import Kingfisher

class TSGeneratoringAnimationView : TSBaseView {

    
    var isShowBackGeneration:Bool = false {
        didSet{
            self.backgroundGenerateBtn.isHidden = !self.isShowBackGeneration
        }
    }
    
    lazy var cusStackView: UIStackView = {
        let cusStackView = UIStackView()
        cusStackView.axis = .vertical
        cusStackView.distribution = .fill
        cusStackView.alignment = .center
        return cusStackView
    }()
    
    lazy var animatedImageView: AnimatedImageView = {
        let animatedImageView = AnimatedImageView()
        animatedImageView.autoPlayAnimatedImage = false
        if let gifURL = Bundle.main.url(forResource: "generat_loading", withExtension: "gif") {
            animatedImageView.kf.setImage(with: gifURL, options: [.cacheOriginalImage]) { result in
                switch result {
                case .success(let value):
                    print("GIF 加载成功: \(value.source.url?.absoluteString ?? "")")
                case .failure(let error):
                    print("GIF 加载失败: \(error.localizedDescription)")
                }
            }
        }
        
        return animatedImageView
    }()
    
    
    lazy var animatedTotalView: UIView = {
        let bgView = UIView()
        let generatLoading2 = UIImageView.createImageView(image: .generatLoading2)
        bgView.addSubview(generatLoading2)
        generatLoading2.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        bgView.addSubview(animatedImageView)
        animatedImageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        let generatLoading1 = UIImageView.createImageView(image: .generatLoading1)
        bgView.addSubview(generatLoading1)
        generatLoading1.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        return bgView
    }()
    
    lazy var timeLabel: UILabel = {
        let textLabel = UILabel.createLabel(font: .font(name: .PoppinsBoldItalic,size: 18,weight: .semibold),textColor: .mainText,textAlignment: .center)
        textLabel.isHidden = true
        return textLabel
    }()
    
    lazy var textLabel: UILabel = {
        let textLabel = UILabel.createLabel(font: .font(size: 18),textColor: .mainText,textAlignment: .center,numberOfLines: 0)
        textLabel.isHidden = true
        return textLabel
    }()
    
    lazy var infoLabel: UILabel = {
        let textLabel = UILabel.createLabel(font: .font(size: 14),textColor: .secondaryText,textAlignment: .center,numberOfLines: 0)
        textLabel.isHidden = true
        return textLabel
    }()
    
    lazy var backgroundGenerateBtn: UIButton = { 
        let btn = UIButton.createButton(title: "Generate in the background".localized,backgroundColor: .themeColor,font: .font(size: 16,weight: .regular),titleColor: UIColor.mainText,corner: 24)
        btn.titleLabel?.adjustsFontSizeToFitWidth = true
        btn.layer.borderColor = (UIColor.themeColor).cgColor
        btn.layer.borderWidth = 1.0
        btn.isHidden = true
        return btn
    }()
    
    override func creatUI() {
        
        contentView.addSubview(cusStackView)
        cusStackView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        setUpGeneratorContentView()
    }
    
    func setUpGeneratorContentView(){
        
        cusStackView.addArrangedSubview(animatedTotalView)
        animatedTotalView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.width.height.equalTo(120)
        }
        
        cusStackView.addSpacing(length: 12.0)
        
        cusStackView.addArrangedSubview(timeLabel)
        timeLabel.snp.makeConstraints { make in
            make.leading.equalTo(kTextLeading)
            make.trailing.equalTo(-kTextLeading)
        }
        
        cusStackView.addSpacing(length: 16.0)
        
        cusStackView.addArrangedSubview(textLabel)
        textLabel.snp.makeConstraints { make in
            make.leading.equalTo(kTextLeading)
            make.trailing.equalTo(-kTextLeading)
        }
        
        cusStackView.addSpacing(length: 8.0)
        
        cusStackView.addArrangedSubview(infoLabel)
        infoLabel.snp.makeConstraints { make in
            make.leading.equalTo(kTextLeading)
            make.trailing.equalTo(-kTextLeading)
        }
        
        cusStackView.addSpacing(length: 20.0)
        
        cusStackView.addArrangedSubview(backgroundGenerateBtn)
        backgroundGenerateBtn.snp.makeConstraints { make in
            make.width.lessThanOrEqualTo(k_ScreenWidth - 64)
            make.width.greaterThanOrEqualTo(250)
            make.height.equalTo(48)
            make.bottom.equalTo(0)
        }

    }

}

extension TSGeneratoringAnimationView {
    
    
    func startAnimation() {
        kMainShort {
            self.animatedImageView.startAnimating()
        }
    }

    func stopAnimation() {
        self.animatedImageView.stopAnimating()
    }
    
    func setText(time:String,info:String) {
        timeLabel.text = time
        infoLabel.text = info
        
        timeLabel.isHidden = time.isEmpty
        infoLabel.isHidden = info.isEmpty
    }
    
    func setProgressText(text:String) {
        textLabel.text = text
        textLabel.isHidden = text.isEmpty
    }

}
