//
//  QSLTrialTipsAlertView.swift
//  QuickSearchLocation
//
//  Created by Destiny on 2025/10/31.
//

import UIKit

class QSLTrialTipsAlertView: UIView {
    
    lazy var contentView: UIView = {
        
        let contentViewW = 318.rpx
        let contentViewH =  436.rpx
        let contentView = UIView(frame: CGRect(x: 0, y: 0, width: contentViewW, height: contentViewH))
        return contentView
    }()
    
    lazy var centerImage : UIImageView = {
        let centerImage = UIImageView()
        centerImage.contentMode = .scaleAspectFit
        centerImage.image = UIImage(named: "vip_trial_tips")
        return centerImage
    }()
    
    lazy var contentLabel: UILabel = {
        let label = UILabel()
        label.text("您已好友守护了Ta   次，\n并且有   条重要轨迹即将消失。")
        label.font(12)
        label.numberOfLines = 2
        label.textColor = .hexStringColor(hexString: "#B6795B")
        return label
    }()
    
    lazy var oneButton: UIButton = {
        let btn = UIButton()
        btn.addTarget(self, action: #selector(oneBtnAction), for: .touchUpInside)
        return btn
    }()
    
    lazy var hideButton: UIButton = {
        let btn = UIButton()
        btn.image(UIImage(named: "vip_pay_failure_close"))
        btn.addTarget(self, action: #selector(removeView), for: .touchUpInside)
        return btn
    }()
    
    var oneBtnClosure: (() -> ())?
    var hideBtnClosure: (() -> ())?
    
    class func alert(view: UIView,
                     clickBtnClosure: @escaping () -> () = {},
                     hideBtnClosure: @escaping () -> () = {}) {
        
        let window = QSLTrialTipsAlertView(frame: CGRect(x: 0, y: 0, width: QSLConst.qsl_kScreenW, height: QSLConst.qsl_kScreenH))
        view.addSubview(window)
        window.oneBtnClosure = clickBtnClosure
        window.hideBtnClosure = hideBtnClosure
        
        QSEventHandle.eventPush(eventName: QSLGravityConst.trial_exposure_end_popup)
        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()

        let attr = NSMutableAttributedString()
        let firstAttr = NSMutableAttributedString(string: "您已好友守护了Ta")
        firstAttr.yy_font = UIFont.textM(12)
        firstAttr.yy_color = UIColor.hexStringColor(hexString: "#B6795B")
        attr.append(firstAttr)
        
        let randomAddCount1 = Int.random(in: 1...10)
        let randomAddCount2 = Int.random(in: 1...5)
        let firstText = "\(randomAddCount1)"
        let firstAttr1 = NSMutableAttributedString(string: firstText)
        firstAttr1.yy_font = UIFont.textM(15)
        firstAttr1.yy_color = .hexStringColor(hexString: "#69280E")
        attr.append(firstAttr1)

        let secondAttr = NSMutableAttributedString(string: "次，\n并且有")
        secondAttr.yy_font = UIFont.textB(12)
        secondAttr.yy_color = UIColor.hexStringColor(hexString: "#B6795B")
        attr.append(secondAttr)
        
        let secondText = "\(randomAddCount2)"
        let secondAttr1 = NSMutableAttributedString(string: secondText)
        secondAttr1.yy_font = UIFont.textM(15)
        secondAttr1.yy_color = .hexStringColor(hexString: "#69280E")
        attr.append(secondAttr1)
        
        let thirdAttr = NSMutableAttributedString(string: "条重要轨迹即将消失。")
        thirdAttr.yy_font = UIFont.textM(12)
        thirdAttr.yy_color = UIColor.hexStringColor(hexString: "#B6795B")
        attr.append(thirdAttr)
        
        contentLabel.attributedText = attr
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // 单按钮点击事件
    @objc func oneBtnAction() {
        if let oneBtnClosure = self.oneBtnClosure {
            oneBtnClosure()
        }
        removeView1()
        QSEventHandle.eventPush(eventName: QSLGravityConst.trial_click_end_popup)
    }
    
    @objc func removeView1() {
        
        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?.centerImage.isHidden = true
        } completion: { [weak self] finished in
            self?.removeFromSuperview()
        }
    }
    
    // 移除
    @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?.centerImage.isHidden = true
        } completion: { [weak self] finished in
            self?.hideBtnClosure?()
            self?.removeFromSuperview()
        }
    }
}

extension QSLTrialTipsAlertView {
    
    func initView() {

        addSubview(contentView)
        contentView.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 318.rpx, height: 436.rpx))
            make.center.equalToSuperview()
        }
        
        contentView.addSubview(centerImage)
        centerImage.snp.makeConstraints { make in
            make.top.bottom.left.right.equalTo(0)
        }
        
        contentView.addSubview(contentLabel)
        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(15.rpx)
            make.right.equalTo(-15.rpx)
            make.top.equalTo(68.rpx)
            make.height.equalTo(40.rpx)
        }
        
        addSubview(hideButton)
        hideButton.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 28.rpx, height: 28.rpx))
            make.right.equalTo(-24.rpx)
            make.top.equalTo(contentView.snp.top).offset(-30.rpx)
        }
        
        contentView.addSubview(oneButton)
        oneButton.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 220.rpx, height: 44.rpx))
            make.centerX.equalToSuperview()
            make.bottom.equalTo(-20.rpx)
        }
    }
}
