//
//  QSLRetainPopUpAlertView.swift
//  QuickSearchLocation
//
//  Created by Destiny on 2025/8/1.
//

import UIKit

class QSLRetainPopUpAlertView: UIView {
        
    lazy var contentView: UIView = {
        
        let contentViewW = QSLConst.qsl_kScreenW - 60.rpx
        let contentViewH =  152.0.rpx
        let contentView = UIView(frame: CGRect(x: 0, y: 0, width: contentViewW, height: contentViewH))
        contentView.backgroundColor = .clear
        return contentView
    }()
    
    
    lazy var closeButton: UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "vip_pay_failure_close"), for: .normal)
        btn.addTarget(self, action: #selector(closeBtnAction), for: .touchUpInside)
        return btn
    }()

    lazy var popBgImageView : UIImageView = {
        let popBgImageView = UIImageView()
        popBgImageView.contentMode = .scaleAspectFit
        popBgImageView.image = UIImage(named: "vip_pay_failure_back_ground")
        return popBgImageView
    }()
    

    ///中间描述区域
    lazy var middleView : UIView = {
        let middleView = UIView()
        middleView.backgroundColor = .clear
        return middleView
    }()
    
    lazy var oneButton: UIButton = {
      
        let btn = UIButton()
        btn.addRadius(radius: 20.rpx)
        btn.title("去登录")
        btn.textColor(.white)
        btn.mediumFont(14)
        btn.gradientBackgroundColor(color1: .hexStringColor(hexString: "#7F5737"), color2: .hexStringColor(hexString: "#562B0E"), width: 200.rpx, height: 40.rpx, direction: .horizontal)
        btn.addTarget(self, action: #selector(oneBtnAction), for: .touchUpInside)
        return btn
    }()
    
    lazy var firstButton: UIButton = {
        
        let btn = UIButton()
        btn.isHidden = true
        btn.backgroundColor = .hexStringColor(hexString: "#F8F8F8")
        btn.addRadius(radius: 20.rpx)
        btn.title("取消")
        btn.textColor(.hexStringColor(hexString: "#A7A7A7"))
        btn.mediumFont(16)
        btn.addTarget(self, action: #selector(firstBtnAction), for: .touchUpInside)
        return btn
    }()
    
    lazy var secondButton: UIButton = {
        
        let btn = UIButton()
        btn.isHidden = true
        btn.addRadius(radius: 20.rpx)
        btn.title("确认")
        btn.textColor(.white)
        btn.mediumFont(16)
        btn.gradientBackgroundColor(color1: .hexStringColor(hexString: "#15CBA1"), color2: .hexStringColor(hexString: "#1FE0BA"), width: 118.rpx, height: 40.rpx, direction: .horizontal)
        btn.addTarget(self, action: #selector(secondBtnAction), for: .touchUpInside)
        return btn
    }()
    

    var oneBtnClosure: (() -> ())?
    
    var firstBtnClosure: (() -> ())?
    
    var secondBtnClosure: (() -> ())?
    
    var closeBtnClosure: (() -> ())?
    
    
    class func alert(view: UIView,
                     isOneBtn: Bool = false,
                     oneBtnText: String = "去登录",
                     oneBtnClosure: @escaping () -> () = {},
                     firstBtnClosure: @escaping () -> () = {},
                     secondBtnClosure: @escaping () -> () = {},
                     closeBtnClosure: @escaping () -> () = {}) {
        
        let window = QSLRetainPopUpAlertView(frame: CGRect(x: 0, y: 0, width: QSLConst.qsl_kScreenW, height: QSLConst.qsl_kScreenH))

        window.oneButton.title(oneBtnText)
        window.oneButton.isHidden = !isOneBtn
        window.firstButton.isHidden = isOneBtn
        window.secondButton.isHidden = isOneBtn
        window.oneBtnClosure = oneBtnClosure
        window.firstBtnClosure = firstBtnClosure
        window.secondBtnClosure = secondBtnClosure
        window.closeBtnClosure = closeBtnClosure
        
        window.contentView.snp.remakeConstraints { make in
            make.size.equalTo(CGSize(width: QSLConst.qsl_kScreenW - 60.rpx, height: 310.rpx))
            make.center.equalToSuperview()
        }
        view.addSubview(window)
        
        UIView.animate(withDuration: 0.4, delay: 0, usingSpringWithDamping: 0.95, initialSpringVelocity: 0.05) {
            window.backgroundColor = .hexStringColor(hexString: "#000000", alpha: 0.7)
            window.contentView.isHidden = false
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // 单按钮点击事件
    @objc func oneBtnAction() {
        if let oneBtnClosure = self.oneBtnClosure {
            oneBtnClosure()
        }
        removeView()
    }
    
    // 取消按钮点击事件
    @objc func firstBtnAction() {
        if let firstBtnClosure = self.firstBtnClosure {
            firstBtnClosure()
        }
        removeView()
    }
    
    // 确认按钮点击事件
    @objc func secondBtnAction() {
        if let secondBtnClosure = self.secondBtnClosure {
            secondBtnClosure()
        }
        removeView()
    }
    
    // 关闭按钮点击事件
    @objc func closeBtnAction() {
        if let closeBtnClosure = self.closeBtnClosure {
            closeBtnClosure()
        }
        removeView()
    }
    
    // 移除
    @objc func removeView() {
        
        UIView.animate(withDuration: 0.4, delay: 0, usingSpringWithDamping: 0.95, initialSpringVelocity: 0.05) { [weak self] in
            self?.backgroundColor = UIColor.init(white: 0, alpha: 0)
            self?.contentView.isHidden = true
            self?.closeButton.isHidden = true
        } completion: { [weak self] finished in
            self?.removeFromSuperview()
        }
    }
}

extension QSLRetainPopUpAlertView {
    
    func initView() {
        
        addSubview(contentView)
        contentView.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: QSLConst.qsl_kScreenW - 60.rpx, height: 152.0.rpx))
            make.center.equalToSuperview()
        }
        
        addSubview(closeButton)
        closeButton.snp.makeConstraints { make in
            make.bottom.equalTo(contentView.snp.top).offset(30.hpx)
            make.right.equalTo(contentView.snp.right).offset(-10.rpx)
            make.size.equalTo(CGSizeMake(24.rpx, 24.rpx))
        }
        
        contentView.addSubview(popBgImageView)
        popBgImageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        contentView.addSubview(oneButton)
        oneButton.snp.makeConstraints { make in
            //make.top.equalTo(contentLabel.snp.bottom).offset(24.5.rpx)
            make.size.equalTo(CGSize(width: 150.rpx, height: 40.rpx))
            make.centerX.equalToSuperview()
            make.bottom.equalTo(-38.rpx)
        }
        
        contentView.addSubview(firstButton)
        firstButton.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 118.rpx, height: 40.rpx))
            make.left.equalTo(24.rpx)
            make.bottom.equalTo(-24.rpx)
        }
        
        contentView.addSubview(secondButton)
        secondButton.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 118.rpx, height: 40.rpx))
            make.right.equalTo(-24.rpx)
            make.bottom.equalTo(-24.rpx)
        }
    }
}
