//
//  QSLAlertView.swift
//  QuickSearchLocation
//
//  Created by Destiny on 2024/12/4.
//

import UIKit

class QSLAlertView: 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: UILabel = {
        
        let label = UILabel()
        label.numberOfLines = 0
        label.text("登录之后才可以发送好友申请")
        label.font(14)
        label.textColor = .hexStringColor(hexString: "#404040")
        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,
                     content: String,
                     isOneBtn: Bool = false,
                     contentTextAlignment: NSTextAlignment = .center,
                     oneBtnText: String = "去登录",
                     oneBtnClosure: @escaping () -> () = {},
                     firstBtnClosure: @escaping () -> () = {},
                     secondBtnClosure: @escaping () -> () = {},
                     closeBtnClosure: @escaping () -> () = {}) {
        
        let window = QSLAlertView(frame: CGRect(x: 0, y: 0, width: QSLConst.qsl_kScreenW, height: QSLConst.qsl_kScreenH))
        window.titleLabel.text = title
        window.contentLabel.text = content
        window.contentLabel.changeLineSpace(space: 4)
        window.contentLabel.textAlignment = contentTextAlignment
        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 + 92.0.rpx))
            make.center.equalToSuperview()
        }
        view.addSubview(window)
        
        gravityInstance?.track(QSLGravityConst.friend_delete_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 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 QSLAlertView {
    
    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(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(65.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)
        }
        
        
    }
}
