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

import UIKit

class QSLHomeAddFriendView: UIView {
    lazy var bgButton: UIButton = {
        let btn = UIButton()
        btn.addTarget(self, action: #selector(removeView), for: .touchUpInside)
        return btn
    }()
    
    lazy var contentView: UIView = {
        
        let contentViewW = QSLConst.qsl_kScreenW
        let contentViewH =  365.0.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: "home_friends_alert_bg")
        return centerImage
    }()
    
    lazy var titleLabel: UILabel = {
      
        let label = UILabel()
        label.text("查找好友定位")
        label.boldFont(30)
        label.textColor = QSLColor.textColor_333
        return label
    }()
    
    lazy var contentLabel: UILabel = {
      
        let label = UILabel()
        label.text("去添加Ta的好友开启守护！")
        label.font(12)
        label.textColor = .hexStringColor(hexString: "#61877F")
        return label
    }()
    
    lazy var oneButton: UIButton = {
      
        let btn = UIButton()
        btn.addRadius(radius: 22.rpx)
        btn.title(QSLConfig.addFriendTitle)
        btn.textColor(.white)
        btn.mediumFont(16)
        btn.gradientBackgroundColor(color1: .hexStringColor(hexString: "#15CBA1"), color2: .hexStringColor(hexString: "#1FE0BA"), width: 250.rpx, height: 44.rpx, direction: .horizontal)
        btn.addTarget(self, action: #selector(oneBtnAction), for: .touchUpInside)
        return btn
    }()
    
    lazy var hideButton: UIButton = {
        let btn = UIButton()
        btn.title("下次再说")
        btn.textColor(.init(white: 0, alpha: 0.4))
        btn.font(11)
        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 = QSLHomeAddFriendView(frame: CGRect(x: 0, y: 0, width: QSLConst.qsl_kScreenW, height: QSLConst.qsl_kScreenH))
        view.addSubview(window)
        window.oneBtnClosure = clickBtnClosure
        window.hideBtnClosure = hideBtnClosure
        
        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()
        }
        removeView1()
    }
    
    @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 QSLHomeAddFriendView {
    
    func initView() {
        
        addSubview(bgButton)
        bgButton.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: QSLConst.qsl_kScreenW, height: QSLConst.qsl_kScreenH))
            make.center.equalToSuperview()
        }
        
        addSubview(contentView)
        contentView.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: QSLConst.qsl_kScreenW, height: 365.0.rpx))
            make.center.equalToSuperview()
        }
        
        contentView.addSubview(centerImage)
        centerImage.snp.makeConstraints { make in
            make.top.bottom.left.right.equalTo(0)
        }
        
        contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(54.rpx)
            make.right.equalTo(-54.rpx)
            make.top.equalTo(22.rpx)
            make.height.equalTo(42.rpx)
        }
        
        contentView.addSubview(contentLabel)
        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(54.rpx)
            make.right.equalTo(-54.rpx)
            make.top.equalTo(titleLabel.snp.bottom)
            make.height.equalTo(14.rpx)
        }
        
        contentView.addSubview(hideButton)
        hideButton.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 60.rpx, height: 28.rpx))
            make.centerX.equalToSuperview()
            make.bottom.equalTo(-18.rpx)
        }
        
        contentView.addSubview(oneButton)
        oneButton.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 250.rpx, height: 44.rpx))
            make.centerX.equalToSuperview()
            make.bottom.equalTo(-54.rpx)
        }
    }
}
