//
//  QSLAddFriendSuccessAlertView.swift
//  QuickSearchLocation
//
//  Created by Destiny on 2025/9/23.
//

import UIKit

class QSLAddFriendSuccessAlertView: UIView {
    
    lazy var contentView: UIView = {
        
        let contentViewW = QSLConst.qsl_kScreenW - 60.rpx
        let contentViewH =  298.0.rpx
        let contentView = UIView(frame: CGRect(x: 0, y: 0, width: contentViewW, height: contentViewH))
        contentView.backgroundColor = .white
        contentView.addRadius(radius: 8.rpx)
        return contentView
    }()
    
    lazy var centerImage : UIImageView = {
        let centerImage = UIImageView()
        centerImage.contentMode = .scaleAspectFit
        centerImage.image = UIImage(named: "friends_alert_bg")
        return centerImage
    }()
    
    lazy var titleLabel: UILabel = {
      
        let label = UILabel()
        label.text("您已成功定位到好友！")
        label.mediumFont(20)
        label.textColor = QSLColor.textColor_333
        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 closeButton: UIButton = {
       
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "public_btn_close_AAA"), for: .normal)
        btn.addTarget(self, action: #selector(removeView), for: .touchUpInside)
        return btn
    }()
    
    var oneBtnClosure: (() -> ())?
    
    class func alert(view: UIView,
                     clickBtnClosure: @escaping () -> () = {}) {
        
        let window = QSLAddFriendSuccessAlertView(frame: CGRect(x: 0, y: 0, width: QSLConst.qsl_kScreenW, height: QSLConst.qsl_kScreenH))
        view.addSubview(window)
        window.oneBtnClosure = clickBtnClosure
        
        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 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?.removeFromSuperview()
        }
    }
}

extension QSLAddFriendSuccessAlertView {
    
    func initView() {
        
        addSubview(contentView)
        contentView.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: QSLConst.qsl_kScreenW - 60.rpx, height: 298.0.rpx))
            make.center.equalToSuperview()
        }
        
        contentView.addSubview(centerImage)
        centerImage.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 190.rpx, height: 175.rpx))
            make.centerX.equalToSuperview()
            make.top.equalTo(12.rpx)
        }
        
        contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(192.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)
        }
 
        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)
        }
    }
}
