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

class TSGeneratorErrorView: TSBaseView {
    
    var style:TSGeneratorView.Style = .generalError{
        didSet{
            debugPrint("TSGeneratorErrorView style = \(style)")
            switch style {
            case .sensitiveError:
                sensitiveErrorView()
            case .netWorkError:
                netWorkErrorView()
            case .generateTooMuch:
                generateTooMuchView()
            default:
                generalErrorView()
            }
        }
    }
    var isUploadImage:Bool = true
    lazy var cusStackView: UIStackView = {
        let cusStackView = UIStackView()
        cusStackView.axis = .vertical
        cusStackView.distribution = .fill
        cusStackView.alignment = .center
        return cusStackView
    }()
    
    lazy var errorImageView: UIImageView = {
        let errorImageView = UIImageView.createImageView(imageName: "yellow_warning")
        return errorImageView
    }()
    
    lazy var textLabel: UILabel = {
        let textLabel = UILabel.createLabel(font: .font(size: 14),textColor: .secondaryText,textAlignment: .center,numberOfLines: 0)
        return textLabel
    }()
    
    lazy var submitBtn: UIButton = {
        let btn = UIButton.createButton(title: "Try again".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
        return btn
    }()
    
    override func creatUI() {
        
        contentView.addSubview(cusStackView)
        cusStackView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        cusStackView.addArrangedSubview(errorImageView)
        errorImageView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.width.height.equalTo(120)
        }
        
        cusStackView.addSpacing(length: 12.0)
        
        cusStackView.addArrangedSubview(textLabel)
        textLabel.snp.makeConstraints { make in
            make.leading.equalTo(24)
            make.trailing.equalTo(-24)
        }
        
        cusStackView.addSpacing(length: 28.0)
        
        cusStackView.addArrangedSubview(submitBtn)
        submitBtn.snp.makeConstraints { make in
            make.width.equalTo(250)
            make.height.equalTo(48)
            make.bottom.equalTo(0)
        }
    }
}

extension TSGeneratorErrorView {
    
//    func setErrorText(errorString:String) {
//        textLabel.text = errorString
//    }
    
    func sensitiveErrorView() {
        submitBtn.setTitle(isUploadImage ? "Reselect photos".localized : "Got it".localized, for: .normal)
        errorImageView.image = UIImage(named: "generator_sensitive_error")
        

        textLabel.text = "Your photo may contain nudity, gore or violence that does not comply with the health policy, please replace the photo and try again.".localized
        
    }
    
    func generalErrorView() {
        submitBtn.setTitle("Got it".localized, for: .normal)
        errorImageView.image = UIImage(named: "generator_failed")
        

        textLabel.text = "Sorry there was a slight problem with the image processing, please try again later.".localized
    }
    
    
    func netWorkErrorView() {
        submitBtn.setTitle("Try Again".localized, for: .normal)
        errorImageView.image = UIImage(named: "generator_network_error")
        

        textLabel.text = "No network, please check your network and try again.".localized
    }
    
    func generateTooMuchView() {
        submitBtn.setTitle("Got it".localized, for: .normal)
        errorImageView.image = UIImage(named: "generator_failed")
        
        textLabel.text = "Your photo may contain nudity, gore or violence that does not comply with the health policy, please replace the photo and try again.".localized
    }
}
