//
//  QSLFriendRemarkAlertView.swift
//  QuickSearchLocation
//
//  Created by Destiny on 2024/12/5.
//

import UIKit

class QSLFriendRemarkAlertView: UIView {
    
    lazy var contentView: UIView = {
        
        let contentView = UIView(frame: CGRect(x: 0, y: 0, width: QSLConst.qsl_kScreenW - 60.rpx, height: 203.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 remarkView: UIView = {
      
        let view = UIView()
        view.backgroundColor = QSLColor.backGroundColor
        view.addRadius(radius: 4.rpx)
        view.addBorder(borderWidth: 1, borderColor: .hexStringColor(hexString: "#F2F2F2"))
        return view
    }()
    
    lazy var numText: UILabel = {
        
        let label = UILabel()
        label.text("0/11")
        label.font(13)
        label.textColor = .hexStringColor(hexString: "#A7A7A7")
        return label
    }()
    
    lazy var remarkTextField: UITextField = {
      
        let textField = UITextField()
        textField.maxTextNumber = 11
        textField.delegate = self
        textField.textColor = QSLColor.Color_202020
        textField.font = UIFont.textF(15)
        textField.placeholder = "请输入备注"
        textField.setPlaceholderAttribute(font: UIFont.textF(16), color: UIColor.hexStringColor(hexString: "#A7A7A7"))
        return textField
    }()
    
    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
    }()
    
    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 secondBtnClosure: ((String) -> ())?
    
    class func alert(view: UIView,
                     secondBtnClosure: @escaping (String) -> ()) {
        
        let window = QSLFriendRemarkAlertView(frame: CGRect(x: 0, y: 0, width: QSLConst.qsl_kScreenW, height: QSLConst.qsl_kScreenH))
        window.secondBtnClosure = secondBtnClosure
        view.addSubview(window)
        
        gravityInstance?.track(QSLGravityConst.friend_remark_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)
        
        initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 取消按钮点击事件
    @objc func firstBtnAction() {
        
        gravityInstance?.track(QSLGravityConst.friend_remark_cancel)
        removeView()
    }
    
    // 确认按钮点击事件
    @objc func secondBtnAction() {
        
        gravityInstance?.track(QSLGravityConst.friend_remark_click)
        
        guard let text = self.remarkTextField.text, text.count > 0 else {
            UIApplication.keyWindow?.toast(text: "请输入名称")
            return
        }
        
        if let secondBtnClosure = self.secondBtnClosure {
            secondBtnClosure(text)
        }
        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 QSLFriendRemarkAlertView: UITextFieldDelegate {
    
    func textFieldDidChangeSelection(_ textField: UITextField) {
        
        let numCount = textField.text?.count
        self.numText.text = "\(numCount ?? 0)/11"
    }
}

extension QSLFriendRemarkAlertView {
    
    func initUI() {
        
        addSubview(contentView)
        contentView.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: QSLConst.qsl_kScreenW - 60.rpx, height: 203.0.rpx))
            make.center.equalToSuperview()
        }
        
        contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(24.rpx)
        }
        
        contentView.addSubview(remarkView)
        remarkView.snp.makeConstraints { make in
            make.left.equalTo(29.rpx)
            make.right.equalTo(-19.rpx)
            make.top.equalTo(65.rpx)
            make.height.equalTo(46.rpx)
        }
        
        remarkView.addSubview(numText)
        numText.snp.makeConstraints { make in
            make.right.equalTo(-12.rpx)
            make.width.equalTo(32.rpx)
            make.centerY.equalToSuperview()
        }
        
        remarkView.addSubview(remarkTextField)
        remarkTextField.snp.makeConstraints { make in
            make.left.equalTo(12.rpx)
            make.top.bottom.equalTo(0)
            make.right.equalTo(numText.snp.left).offset(-12.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)
        }
    }
}
