//
//  QSLVipAlertView.swift
//  QuickSearchLocation
//
//  Created by Destiny on 2025/7/28.
//

import UIKit

class QSLVipAlertView: 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 = .white
        contentView.addRadius(radius: 8.rpx)
        contentView.addBorder(borderWidth: 3.rpx, borderColor:  .hexStringColor(hexString: "#FFFFFF"))
        return contentView
    }()
    
    lazy var centerImage : UIImageView = {
        let centerImage = UIImageView()
        centerImage.contentMode = .scaleAspectFit
        centerImage.image = UIImage(named: "vip_comment_give_thumb")
        return centerImage
    }()
    
    //渐变色
    lazy var headerColorView: UIView = {
        let headerColorViewW = QSLConst.qsl_kScreenW - 60.rpx
        let headerColorView = UIView(frame: CGRect(x: 0, y: 0, width: headerColorViewW, height: 100))
        headerColorView.layer.masksToBounds = true

        // 创建渐变图层
        let gradientLayer = CAGradientLayer()
        gradientLayer.frame = headerColorView.bounds

        // 设置渐变颜色 (#9EFFEC 到白色)
        gradientLayer.colors = [
            UIColor(red: 0.62, green: 1.0, blue: 0.93, alpha: 1.0).cgColor, // #9EFFEC
            UIColor.white.cgColor
        ]

        // 设置渐变方向 (180deg 表示从上到下)
        gradientLayer.startPoint = CGPoint(x: 0.5, y: 0.0)  // 顶部中点
        gradientLayer.endPoint = CGPoint(x: 0.5, y: 1.0)    // 底部中点

        // 添加渐变图层到视图
        headerColorView.layer.insertSublayer(gradientLayer, at: 0)
        return headerColorView

    }()
    
    lazy var titleLabel: UILabel = {
      
        let label = UILabel()
        label.text("给我们一个好评呗~")
        label.mediumFont(20)
        label.textColor = QSLColor.textColor_333
        return label
    }()
    
    ///中间描述区域
    lazy var middleView : UIView = {
        let middleView = UIView()
        middleView.backgroundColor = .white
        return middleView
    }()
    
    lazy var leftMiddleView : UIView = {
        let leftMiddleView = UIView(frame: CGRectMake(0, 0, 14.rpx, 1.rpx))
        leftMiddleView.gradientBackgroundColor(color1: .hexStringColor(hexString: "#AAAAAA",alpha: 0.0), color2: .hexStringColor(hexString: "#AAAAAA"), width: 14.rpx, height: 1.rpx, direction: .horizontal)
        return leftMiddleView
    }()
    
    lazy var rithMiddleView : UIView = {
        let rithMiddleView = UIView(frame: CGRectMake(0, 0, 14, 1))
        rithMiddleView.gradientBackgroundColor(color1: .hexStringColor(hexString: "#AAAAAA"), color2: .hexStringColor(hexString: "#AAAAAA",alpha: 0.0), width: 14.rpx, height: 1.rpx, direction: .horizontal)
        return rithMiddleView
    }()
    
    lazy var contentLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 0
        label.textAlignment = .center
        label.text("登录之后才可以发送好友申请")
        label.font(12)
        label.textColor = .hexStringColor(hexString: "#00000099" ,alpha: 0.4)
        label.changeLineSpace(space: 4)
        return label
    }()
    
    lazy var oneButton: UIButton = {
      
        let btn = UIButton()
        btn.addRadius(radius: 20.rpx)
        btn.title("去登录")
        btn.textColor(.white)
        btn.mediumFont(16)
        btn.gradientBackgroundColor(color1: .hexStringColor(hexString: "#15CBA1"), color2: .hexStringColor(hexString: "#1FE0BA"), width: 150.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
    }()
    
    lazy var closeButton: UIButton = {
       
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "public_btn_close_AAA"), for: .normal)
        btn.addTarget(self, action: #selector(closeBtnAction), for: .touchUpInside)
        return btn
    }()
    
    var oneBtnClosure: (() -> ())?
    
    var firstBtnClosure: (() -> ())?
    
    var secondBtnClosure: (() -> ())?
    
    var closeBtnClosure: (() -> ())?
    
    
    class func alert(view: UIView,
                     title: String,
                     titleAttring: NSAttributedString,
                     content: String,
                     isOneBtn: Bool = false,
                     oneBtnText: String = "去登录",
                     oneBtnClosure: @escaping () -> () = {},
                     firstBtnClosure: @escaping () -> () = {},
                     secondBtnClosure: @escaping () -> () = {},
                     closeBtnClosure: @escaping () -> () = {}) {
        
        let window = QSLVipAlertView(frame: CGRect(x: 0, y: 0, width: QSLConst.qsl_kScreenW, height: QSLConst.qsl_kScreenH))
        if title.count > 0 {
            window.titleLabel.text = title
        } else {
            window.titleLabel.attributedText = titleAttring
        }
        window.contentLabel.text = content
        window.contentLabel.changeLineSpace(space: 4)
        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
        
        let contentHeight = content.heightAccording(width: QSLConst.qsl_kScreenW - 108.rpx, font: UIFont.textF(15), lineSpacing: 4)
        window.contentView.snp.remakeConstraints { make in
            make.size.equalTo(CGSize(width: QSLConst.qsl_kScreenW - 60.rpx, height: 65.rpx + contentHeight + 129.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
        } completion: { [weak self] finished in
            self?.removeFromSuperview()
        }
    }
}

extension QSLVipAlertView {
    
    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(centerImage)
        centerImage.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(contentView.snp.top).offset(-40.rpx)
            make.size.equalTo(CGSizeMake(164.6.rpx, 114.rpx))
        }
        
        
        contentView.addSubview(headerColorView)
        headerColorView.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(100)
        }
        
        contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(68.rpx)
        }
        
        //中间区域
        contentView.addSubview(middleView)
        middleView.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(16.5.rpx)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(21.rpx)
        }
        
        middleView.addSubview(contentLabel)
        contentLabel.snp.makeConstraints { make in

            make.center.equalToSuperview()
            make.height.equalTo(21.rpx)
        }
        
        middleView.addSubview(leftMiddleView)
        leftMiddleView.snp.makeConstraints { make in
            make.right.equalTo(contentLabel.snp.left).offset(-6.rpx)
            make.centerY.equalToSuperview()
            make.size.equalTo(CGSizeMake(14.rpx, 1.rpx))
        }
        
        middleView.addSubview(rithMiddleView)
        rithMiddleView.snp.makeConstraints { make in
            make.left.equalTo(contentLabel.snp.right).offset(6.rpx)
            make.centerY.equalToSuperview()
            make.size.equalTo(CGSizeMake(14.rpx, 1.rpx))
        }
        
        contentView.addSubview(oneButton)
        oneButton.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 150.rpx, height: 40.rpx))
            make.centerX.equalToSuperview()
            make.bottom.equalTo(-24.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)
        }
        
        contentView.addSubview(closeButton)
        closeButton.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 24.rpx, height: 24.rpx))
            make.top.equalTo(12.rpx)
            make.right.equalTo(-12.rpx)
        }
        
        
    }
}
