//
//  QSLPrivacyAlertView.swift
//  QuickSearchLocation
//
//  Created by Destiny on 2024/12/9.
//

import UIKit
import YYText

class QSLPrivacyAlertView: UIView {
    
    lazy var contentView: UIView = {
        
        let contentView = UIView(frame: CGRect(x: 0, y: 0, width: QSLConst.qsl_kScreenW - 60.rpx, height: 152.0.rpx))
        contentView.backgroundColor = .white
        contentView.addRadius(radius: 8.rpx)
        return contentView
    }()
    
    lazy var titleLabel: UILabel = {
      
        let label = UILabel()
        label.text("温馨提示")
        label.mediumFont(17)
        label.textColor = QSLColor.Color_202020
        return label
    }()
    
    lazy var contentLabel: YYLabel = {
       
        let label = YYLabel()
        label.textAlignment = .center
        label.numberOfLines = 0
        
        let attr = NSMutableAttributedString()
        
        let firstAttr = NSMutableAttributedString(string: "亲爱的用户，感谢您使用我们产品，为了更好的为您服务，我们可能向系统申请以下必要权限：定位信息权限、存储空间、电话权限等，用于应用的基本服务和功能），你有权拒绝或者撤回权限。 本软件非常重视您的隐私和个人信息，在您使用之前请仔细阅读")
        firstAttr.yy_lineSpacing = 5
        firstAttr.font(14)
        firstAttr.color(.hexStringColor(hexString: "#404040"))
        attr.append(firstAttr)
        
        let privacyHL = YYTextHighlight()
        let privacyText = NSMutableAttributedString(string: "《隐私权政策》")
        privacyText.yy_lineSpacing = 5
        privacyText.font(14)
        privacyText.color(.hexStringColor(hexString: "#2F79FF"))
        privacyText.yy_setTextHighlight(privacyHL, range: NSRange(location: 0, length: "《隐私权政策》".count))
        privacyHL.tapAction = { [weak self] containerView, text, range, rect in
            self?.privacyAction()
        }
        attr.append(privacyText)

        let andAttr = NSMutableAttributedString(string: "和")
        andAttr.yy_lineSpacing = 5
        andAttr.font(14)
        andAttr.color(.hexStringColor(hexString: "#404040"))
        attr.append(andAttr)
    
        let serviceHL = YYTextHighlight()
        let serviceText = NSMutableAttributedString(string: "《服务条款》")
        serviceText.yy_lineSpacing = 5
        serviceText.font(14)
        serviceText.color(.hexStringColor(hexString: "#2F79FF"))
        serviceText.yy_setTextHighlight(serviceHL, range: NSRange(location: 0, length: "《服务条款》".count))
        serviceHL.tapAction = { [weak self] containerView, text, range, rect in
            self?.serviceAction()
        }
        attr.append(serviceText)
        
        let remainAttr = NSMutableAttributedString(string: "全文，如您同意，请点击点击下方的“同意”按钮。")
        remainAttr.yy_lineSpacing = 5
        remainAttr.font(14)
        remainAttr.color(.hexStringColor(hexString: "#404040"))
        attr.append(remainAttr)
        
        label.attributedText = attr
        
        return label
    }()
    
    lazy var firstButton: UIButton = {
        
        let btn = UIButton()
        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.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 secondBtnClosure: (() -> ())?
    
    var serviceBlock: (() -> Void)?
    
    var privacyBlock: (() -> Void)?
    
    var isFirstClick: Bool = true
    
    class func alert(view: UIView,
    secondBtnClosure: @escaping () -> () = {}, serviceCloure: @escaping () -> Void, privacyClosure: @escaping () -> Void) {
        
        let window = QSLPrivacyAlertView(frame: CGRect(x: 0, y: 0, width: QSLConst.qsl_kScreenW, height: QSLConst.qsl_kScreenH))
        window.secondBtnClosure = secondBtnClosure
        window.serviceBlock = serviceCloure
        window.privacyBlock = privacyClosure
        view.addSubview(window)
        
        gravityInstance?.track(QSLGravityConst.privacy_show)
        
        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 firstBtnAction() {
        if isFirstClick {
            self.firstButton.title("不同意并退出")
            isFirstClick = false
            gravityInstance?.track(QSLGravityConst.privacy_disagree)
        } else {
            gravityInstance?.track(QSLGravityConst.privacy_second_disagree)
            exit(0)
        }
    }
    
    // 确认按钮点击事件
    @objc func secondBtnAction() {
        if isFirstClick {
            gravityInstance?.track(QSLGravityConst.privacy_agree)
        } else {
            gravityInstance?.track(QSLGravityConst.privacy_second_agree)
        }
        if let secondBtnClosure = self.secondBtnClosure {
            secondBtnClosure()
        }
        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()
        }
    }
    
    @objc func privacyAction() {
        
        if let privacyBlock = self.privacyBlock {
            privacyBlock()
        }
    }
    
    @objc func serviceAction() {
        
        if let serviceBlock = self.serviceBlock {
            serviceBlock()
        }
    }
}

extension QSLPrivacyAlertView {
    
    func initView() {
        
        addSubview(contentView)
        contentView.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: QSLConst.qsl_kScreenW - 60.rpx, height: 330.0.rpx))
            make.center.equalToSuperview()
        }
        
        contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(24.rpx)
        }
        
        contentView.addSubview(contentLabel)
        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(24.rpx)
            make.right.equalTo(-24.rpx)
            make.top.equalTo(55.rpx)
            make.height.equalTo(200.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)
        }
    }
}
