//
//  TSBottomAlertVC.swift
//  AIEmoji
//
//  Created by 100Years on 2025/1/17.
//

class TSBottomAlertVC: TSBaseVC {
    
    lazy var topBgView: UIView = {
        let topBgView = UIView(frame: CGRectMake(0, 0, k_ScreenWidth, k_ScreenHeight))
        topBgView.backgroundColor = .black.withAlphaComponent(0.5)
        topBgView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard)))
        return topBgView
    }()
    
    var bottomViewH:CGFloat = 340{
        didSet{
            bottomView.frame = CGRectMake(0, k_ScreenHeight-bottomViewH, k_ScreenWidth, bottomViewH)
        }
    }
    
    lazy var bgImageView: UIImageView = {
        let bgImageView = UIImageView()
        bgImageView.image = UIImage(named: "bottom_alert_bg")?.resizableImage(capInsets:UIEdgeInsets(top:40, left: 30, bottom: 30, right: 170))
        bgImageView.contentMode = .scaleToFill
        return bgImageView
    }()
    
    lazy var bottomView: UIView = {
        let bottomView = UIView(frame: CGRectMake(0, k_ScreenHeight-bottomViewH, k_ScreenWidth, bottomViewH))
        bottomView.addSubview(bgImageView)
        bgImageView.snp.makeConstraints { make in
            make.bottom.top.leading.right.equalTo(0)
        }
        return bottomView
    }()
    

    lazy var xBtn: UIButton = {
        let xBtn = TSUIExpandedTouchButton()
        xBtn.setUpButton(image: UIImage(named: "x")) { [weak self]  in
//        let xBtn = UIButton.createButton(image: UIImage(named: "x")) { [weak self]  in
            guard let self = self else { return }
            clickXBtn()
        }
        return xBtn
    }()
    
    lazy var againBtn: UIButton = {
        let againBtn = UIButton.createButton(title:"Try Again".localized,backgroundColor:.themeColor,font: UIFont.font(size: 14,weight: .medium),titleColor:.white,corner: 24)
        { [weak self]  in
            guard let self = self else { return }
            clickAgainBtn()
        }
        return againBtn
    }()
    
    lazy var saveBtn: UIButton = {
        let saveBtn = UIButton.createButton(title:"Save".localized,backgroundColor:.themeColor,font: UIFont.font(size: 14,weight: .medium),titleColor:.white,corner: 24)
        { [weak self]  in
            guard let self = self else { return }
            clickSubmitBtn()
        }
        kDelayMainShort {
            saveBtn.addGradientBg(colors: ["#E961F6".uiColor.cgColor,"#7E57F4".uiColor.cgColor])
        }
        return saveBtn
    }()
    
    
    lazy var leftBtn: UIButton = {
        let leftBtn = UIButton.createButton(title:"Copy".localized,backgroundColor: .fromHex("#FFFFFF", alpha: 0.4),font: UIFont.font(size: 14,weight: .medium),titleColor:.white,corner: 24)
        { [weak self]  in
            guard let self = self else { return }
            clickLeftBtn()
        }
        leftBtn.isHidden = true
        return leftBtn
    }()
    
    lazy var rightBtn: UIButton = {
        
        let rightBtn = UIButton.createButton(title:"Save".localized,backgroundColor:.themeColor,font: UIFont.font(size: 14,weight: .medium),titleColor:.white,corner: 24)
        { [weak self]  in
            guard let self = self else { return }
            clickRightBtn()
        }
        
        kDelayMainShort {
            rightBtn.addGradientBg(colors: ["#E961F6".uiColor.cgColor,"#7E57F4".uiColor.cgColor])
        }
        rightBtn.isHidden = true
        return rightBtn
    }()
    

    lazy var regenerateBtn: UIButton = {
        let regenerateBtn = UIButton.createButton(
            title: "regenerate",
            image: UIImage(named: "refresh_gary"),
            backgroundColor:.white.withAlphaComponent(0.1),
            font: .font(size: 12),
            titleColor: .white.withAlphaComponent(0.6),
            corner: 8.0)
        { [weak self]  in
            guard let self = self else { return }
            clickAgainBtn()
        }
        regenerateBtn.contentEdgeInsets = UIEdgeInsets(top: 4, left: 7, bottom: 4, right: 7)
        regenerateBtn.imageEdgeInsets = UIEdgeInsets(top: 0, left: -4, bottom: 0, right: 0)
        regenerateBtn.isHidden = true
        return regenerateBtn
    }()
    
    //############################## 失败 View ##############################
    
    lazy var failLabel: UILabel = {
        let failLabel = UILabel.createLabel(text: "Generation Failed".localized,font: .font(size: 14.0),textColor: .white.withAlphaComponent(0.6),textAlignment: .center)
        return failLabel
    }()
    
    lazy var failView: UIView = {
        let failView = UIView()
        let failImageView = UIImageView.createImageView(imageName: "generation_failed")
    
        failView.addSubview(failImageView)
        failImageView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.centerX.equalToSuperview()
            make.width.equalTo(88)
            make.height.equalTo(88)
        }
        
        failView.addSubview(failLabel)
        failLabel.snp.makeConstraints { make in
            make.top.equalTo(failImageView.snp.bottom).offset(16)
            make.leading.equalTo(10)
            make.trailing.equalTo(-10)
            make.width.greaterThanOrEqualTo(88)
            make.height.equalTo(14)
            make.bottom.equalTo(0)
        }
        failView.isHidden = true
        return failView
    }()
    
    //############################## 结果 View ##############################
    lazy var resultIconImageView: UIImageView = {
        let resultIconImageView = UIImageView.createImageView(imageName: "")
        return resultIconImageView
    }()
    
    lazy var successTopView: UIView = {
        let resultView = UIView()

        resultView.addSubview(resultIconImageView)
        resultIconImageView.snp.makeConstraints { make in
            make.top.equalTo(-15)
            make.trailing.equalTo(-28)
            make.width.equalTo(88)
            make.height.equalTo(88)
        }
        
        let successLabel = UILabel.createLabel(text: "Successfully Generated".localized,font: .font(name: .PoppinsBlackItalic, size: 18.0),textColor: .themeColor,textAlignment: .left)
        resultView.addSubview(successLabel)
        successLabel.snp.makeConstraints { make in
            make.top.equalTo(30)
            make.leading.equalTo(20)
            make.trailing.equalTo(resultIconImageView.snp.leading).offset(-20)
            make.height.equalTo(18)
        }
        kDelayMainShort {
            successLabel.applyGradient(colors: [UIColor.white,"#D999FF".uiColor])
        }
        resultView.isHidden = true
        return resultView
    }()
    
    //############################## 自定义 View ##############################
    lazy var diyView: UIView = {
        let diyView = UIView()
 
        diyView.addSubview(failView)
        failView.snp.makeConstraints { make in
            make.centerY.equalToSuperview().offset(-40)
            make.centerX.equalToSuperview()
        }
        
        diyView.addSubview(successTopView)
        successTopView.snp.makeConstraints { make in
            make.leading.top.trailing.equalTo(0)
            make.height.equalTo(73)
        }
        

        return diyView
    }()
    
    //点击空白,关闭页面
    var isClickTheBlankClosePage = true
    
    override func createView() {
        view.backgroundColor = .clear
        setNavBarViewHidden(true)

        contentView.addSubview(topBgView)
        contentView.addSubview(bottomView)
    
        let safeBottom = k_Height_safeAreaInsetsBottom()
        //重试
        bottomView.addSubview(againBtn)
        againBtn.snp.makeConstraints { make in
            make.bottom.equalTo(-16-safeBottom)
            make.centerX.equalToSuperview()
            make.width.equalTo(228)
            make.height.equalTo(48)
        }
        
        //保存
        bottomView.addSubview(saveBtn)
        saveBtn.snp.makeConstraints { make in
            make.bottom.equalTo(-16-safeBottom)
            make.centerX.equalToSuperview()
            make.width.equalTo(228)
            make.height.equalTo(48)
        }
        
        
        //中心点
        let centerView = UIView()
        bottomView.addSubview(centerView)
        centerView.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        bottomView.addSubview(leftBtn)
        leftBtn.snp.makeConstraints { make in
            make.bottom.equalTo(-16-safeBottom)
            make.trailing.equalTo(centerView.snp.leading).offset(-8)
            make.width.equalTo(163)
            make.height.equalTo(48)
        }
        
        bottomView.addSubview(rightBtn)
        rightBtn.snp.makeConstraints { make in
            make.bottom.equalTo(-16-safeBottom)
            make.leading.equalTo(centerView.snp.trailing).offset(8)
            make.width.equalTo(163)
            make.height.equalTo(48)
        }
        
        bottomView.addSubview(diyView)
        diyView.snp.makeConstraints { make in
            make.bottom.equalTo(saveBtn.snp.top).offset(-10)
            make.top.left.right.equalTo(0)
        }
        
        bottomView.addSubview(xBtn)
        xBtn.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.trailing.equalTo(-6)
            make.width.equalTo(24)
            make.height.equalTo(24)
        }
        

    }
    
    func closePage() {
        self.dismiss(animated: true, completion: nil)
    }

    @objc func clickAgainBtn(){
        
    }
    
    @objc func clickSubmitBtn(){
        
    }
    
    @objc func clickLeftBtn(){
        
    }
    
    @objc func clickRightBtn(){
        
    }
    
    @objc func clickXBtn(){
        closePage()
    }
    
    @objc private func dismissKeyboard() {
        if isClickTheBlankClosePage {
            closePage()
        }
    }
}
