//
//  TSPlogUploadHintVC.swift
//  AIEmoji
//
//  Created by 100Years on 2025/4/9.
//

import PhotosUI
class TSPlogUploadHintVC: TSBaseVC {
    static var userDefaultsKey:String = "isFirstAIListAge"
    
    struct Config {
        var imageMaxBitSize:Int
        var goodImageNamed:String
        var badImageNamed:String

        var titleText:String
        var titleSubText:String
        
        var goodText:String
        var goodInfoText:String
      
        var badText:String
        var badInfoText:String
        
        init(imageMaxBitSize: Int, goodImageNamed: String, badImageNamed: String, titleText: String, titleSubText: String, goodText: String, goodInfoText: String, badText: String, badInfoText: String) {
            self.imageMaxBitSize = imageMaxBitSize
            self.goodImageNamed = goodImageNamed
            self.badImageNamed = badImageNamed
            self.titleText = titleText
            self.titleSubText = titleSubText
            self.goodText = goodText
            self.goodInfoText = goodInfoText
            self.badText = badText
            self.badInfoText = badInfoText
        }

        static var ptpConfig:Config {
            return Config(imageMaxBitSize: kUploadImageMaxBit5Size,
                          goodImageNamed: "hint_Cat_good",
                          badImageNamed: "hint_Cat_bad",
                          titleText: "".localized,
                          titleSubText: "",
                          goodText: "✅ Good Photo Examples".localized,
                          goodInfoText: "Choose an image where your pet's face is clearly visible".localized,
                          badText: "🚫 Bad Photo Examples".localized,
                          badInfoText: "Blurred face, no proper face".localized)
        }
    }
    
    var config:Config = Config.ptpConfig
    init(config: Config = Config.ptpConfig, clickUpImageHandle: ((UIImage?) -> Void)? = nil) {
        self.config = config
        self.clickUpImageHandle = clickUpImageHandle
        super.init()
    }
    
    @MainActor required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var clickUpImageHandle:((UIImage?)->Void)?
    lazy var popupContentView: UIView = {
        let popupContentView = UIView()
        popupContentView.backgroundColor = .mainBg
        kMainShort {
            popupContentView.makeCorner([.topLeft,.topRight], radius: 20)
        }
        return popupContentView
    }()
    
    lazy var photoPickerManager: TSPhotoPickerManager = {
        let photoPickerManager = TSPhotoPickerManager(viewController: self)
        return photoPickerManager
    }()
    
    lazy var submitBtn: UIButton = {
        let submitBtn = kCreateNormalSubmitBtn(title: "Upload Photo".localized) { [weak self]  in
            guard let self = self else { return }
            pickSinglePhoto()
        }
        return submitBtn
    }()
    
    override func createView() {
        addNormalNavBarView()
//        _ = setNavigationItem("", imageName: "ic-close", direction: .right, action: #selector(clickView))
        view.backgroundColor = .black.withAlphaComponent(0.7)
     
        let topView = UIView()
        topView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(clickView)))
        topView.backgroundColor = .clear
        contentView.addSubview(topView)
        contentView.addSubview(popupContentView)
   
        popupContentView.snp.makeConstraints { make in
            make.bottom.equalTo(0)
            make.leading.equalTo(0)
            make.trailing.equalTo(0)
        }
        
        topView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.leading.equalTo(0)
            make.trailing.equalTo(0)
            make.bottom.equalTo(popupContentView.snp.top).priority(.low)
        }
        
        setUpUI()
    }
    
    @objc func clickView() {
        dismiss(animated: true)
    }
    
    
    func setUpUI(){

        let goodLabel = UILabel.createLabel(text: config.goodText,font: .font(name:.PoppinsBoldItalic ,size: 16,weight: .medium),textColor: .mainText,textAlignment: .center,numberOfLines: 0)
        popupContentView.addSubview(goodLabel)
        goodLabel.snp.makeConstraints { make in
            make.top.equalTo(32)
            make.leading.equalTo(52)
            make.trailing.equalTo(-52)
        }
        
        let goodInfoLabel = UILabel.createLabel(text: config.goodInfoText,font: .font(size: 14),textColor: .mainText,textAlignment: .center,numberOfLines: 0)
        popupContentView.addSubview(goodInfoLabel)
        goodInfoLabel.snp.makeConstraints { make in
            make.top.equalTo(goodLabel.snp.bottom).offset(8)
            make.leading.equalTo(52)
            make.trailing.equalTo(-52)
        }
        
        let goodImageView = UIImageView.createImageView(imageName: config.goodImageNamed,autoMirrored: false)
        popupContentView.addSubview(goodImageView)
        goodImageView.snp.makeConstraints { make in
            make.top.equalTo(goodInfoLabel.snp.bottom).offset(12)
            make.leading.equalTo(0)
            make.trailing.equalTo(0)
            make.height.equalTo(80)
        }
        
        let badLabel = UILabel.createLabel(text: config.badText,font: .font(name:.PoppinsBoldItalic ,size: 16,weight: .medium),textColor: .mainText,textAlignment: .center,numberOfLines: 0)
        popupContentView.addSubview(badLabel)
        badLabel.snp.makeConstraints { make in
            make.top.equalTo(goodImageView.snp.bottom).offset(24)
            make.leading.equalTo(52)
            make.trailing.equalTo(-52)
        }
        
        let badInfoLabel = UILabel.createLabel(text: config.badInfoText,font: .font(size: 14),textColor: .mainText,textAlignment: .center,numberOfLines: 0)
        popupContentView.addSubview(badInfoLabel)
        badInfoLabel.snp.makeConstraints { make in
            make.top.equalTo(badLabel.snp.bottom).offset(8)
            make.leading.equalTo(52)
            make.trailing.equalTo(-52)
        }

        let badImageView = UIImageView.createImageView(imageName: config.badImageNamed,autoMirrored: false)
        popupContentView.addSubview(badImageView)
        badImageView.snp.makeConstraints { make in
            make.top.equalTo(badInfoLabel.snp.bottom).offset(12)
            make.leading.equalTo(0)
            make.trailing.equalTo(0)
            make.height.equalTo(80)
        }
        
        popupContentView.addSubview(submitBtn)
        submitBtn.snp.makeConstraints { make in
            make.top.equalTo(badImageView.snp.bottom).offset(25)
            make.centerX.equalToSuperview()
            make.width.equalTo(343*kDesignScale)
            make.height.equalTo(56)
            make.bottom.equalTo(-16-k_Height_safeAreaInsetsBottom())
        }
    }

    static var isShowUploadImageHint:Bool{
        get {
            return UserDefaults.standard.string(forKey: userDefaultsKey) == nil
        }
        
        set {
//            UserDefaults.standard.set(newValue ? nil : "1", forKey: userDefaultsKey)
//            UserDefaults.standard.synchronize()
        }
    }
    
    func pickSinglePhoto()  {
        photoPickerManager.pickCustomSinglePhoto() { [weak self] image, errorString in
            guard let self = self else { return }
            if let errorString = errorString {
                TSToastShared.showToast(text: errorString)
            }else{
                self.clickUpImageHandle?(image)
            }
        }
        
  
    }
    
    func dismissPageVC(){
        self.view.isHidden = true
        self.photoPickerManager.dismissPageVC()
        self.dismiss(animated: true)
    }
}
