//
//  QSLVipResultsNoMemberAlertView.swift
//  QuickSearchLocation
//
//  Created by Destiny on 2025/7/29.
//

import UIKit

class QSLVipResultsNoMemberAlertView: 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 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 adView : UIView = {
        let adView = UIView(frame:  CGRectMake(0, 0, 188, 96.2))
        // 设置阴影
        layer.masksToBounds = false
        layer.shadowColor = UIColor(red: 7/255.0, green: 78/255.0, blue: 82/255.0, alpha: 0.16).cgColor
        layer.shadowOpacity = 1
        layer.shadowOffset = CGSize(width: 1, height: 6)
        layer.shadowRadius = 8.rpx
        layer.shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: layer.cornerRadius).cgPath
        layer.shouldRasterize = true
        layer.rasterizationScale = UIScreen.main.scale
        adView.addRadius(radius: 8.rpx)
        adView.addBorder(borderWidth: 1.rpx, borderColor:  .hexStringColor(hexString: "#034249"))
        return adView
    }()
    
    lazy var adBgImage : UIImageView = {
        let adBgImage = UIImageView()
        adBgImage.contentMode = .scaleAspectFill
        adBgImage.image = UIImage(named: "vip_comment_content_bg")
        return adBgImage
    } ()
    
    
    lazy var sunlightImage : UIImageView = {
        let sunlightImage = UIImageView()
        sunlightImage.contentMode = .scaleAspectFill
        sunlightImage.image = UIImage(named: "vip_comment_sunlight")
        return sunlightImage
    } ()
    
    lazy var adContentLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 0
        label.textAlignment = .left
        label.text("登录之后才可以发送好友申请")
        label.font(12)
        label.textColor = .hexStringColor(hexString: "#00000099" ,alpha: 0.4)
        label.changeLineSpace(space: 4)
        return label
    }()
    

    ///中间描述区域
    lazy var middleView : UIView = {
        let middleView = UIView()
        middleView.backgroundColor = .clear
        return middleView
    }()
    
    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 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 advertsingPromptView : UIView = {
        let advertsingPromptView = UIView()
        return advertsingPromptView
    }()
    
    
    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,
                     bottomContentAttring: NSAttributedString,
                     isOneBtn: Bool = false,
                     oneBtnText: String = "去登录",
                     oneBtnClosure: @escaping () -> () = {},
                     firstBtnClosure: @escaping () -> () = {},
                     secondBtnClosure: @escaping () -> () = {},
                     closeBtnClosure: @escaping () -> () = {}) {
        
        let window = QSLVipResultsNoMemberAlertView(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.adContentLabel.attributedText = bottomContentAttring
        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: 290.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 QSLVipResultsNoMemberAlertView {
    
    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()
        }
        
        contentView.addSubview(headerColorView)
        headerColorView.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(100)
        }
        
        contentView.addSubview(sunlightImage)
        sunlightImage.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(72.rpx)
            make.size.equalTo(CGSizeMake(177.rpx, 177.rpx))
        }
        
        contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(26.rpx)
        }
        
        //中间区域
        contentView.addSubview(middleView)
        middleView.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(9.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(adView)
        adView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(middleView.snp.bottom).offset(24.5.rpx)
            make.left.equalToSuperview().offset(44.rpx)
            make.right.equalToSuperview().offset(-44.rpx)
            make.height.equalTo(96.2.rpx)
            //make.size.equalTo(CGSizeMake(188.rpx, 96.2.rpx))
        }
        
        adView.addSubview(adBgImage)
        adBgImage.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
//        adView.addSubview(adTitleLabel)
//        adTitleLabel.snp.makeConstraints { make in
//            make.top.equalToSuperview().offset(10.rpx)
//            make.left.equalToSuperview().offset(12.rpx)
//            make.right.equalToSuperview()
//            make.height.equalTo(13.5.rpx)
//        }
        
//        adView.addSubview(adAccessImage)
//        adAccessImage.snp.makeConstraints { make in
//            make.right.equalToSuperview()
//            make.bottom.equalToSuperview()
//            make.size.equalTo(CGSizeMake(86.5.rpx, 43.8.rpx))
//        }
        
        adView.addSubview(adContentLabel)
        adContentLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(10.28.rpx)
            make.bottom.equalToSuperview().offset(-8.37)
            //make.right.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(-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)
        }
    }
}

