//
//  NXInteractiveView.swift
//  bubbleLayer_swift
//
//  Created by liuming on 2020/8/23.
//  Copyright © 2020 liuming. All rights reserved.
//

import UIKit
public class NXInteractiveView: UIView {
    public var tapGestureRecognizer: UITapGestureRecognizer?
    public var longPressGestureRecognizer: UILongPressGestureRecognizer?
    // 点击回调
    public var tapGestureHander: (() -> Void)?
    // 长按回调
    public var longPressGestureHander: (() -> Void)?

    override public init(frame: CGRect) {
        super.init(frame: frame)
        addTagGestureRecognizer()
        addLongGestureRecogizer()
    }

    required init?(coder _: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // 单机方法
    private func addTagGestureRecognizer() {
        let tap = UITapGestureRecognizer(target: self, action: #selector(tapGestureRecognizerHandler(sender:)))
        tap.numberOfTouchesRequired = 1
        tap.numberOfTapsRequired = 1
        addGestureRecognizer(tap)
        tapGestureRecognizer = tap
    }

    @objc
    public func tapGestureRecognizerHandler(sender _: UITapGestureRecognizer) {
        print("---- tapGestureRecognizerHandler -----")
        if tapGestureHander != nil {
            tapGestureHander!()
        }
    }

    // 长按事件
    private func addLongGestureRecogizer() {
        let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longGestureRecognizerHandler(sender:)))
        longPressGestureRecognizer.numberOfTapsRequired = 1
        longPressGestureRecognizer.numberOfTouchesRequired = 1
        addGestureRecognizer(longPressGestureRecognizer)
        self.longPressGestureRecognizer = longPressGestureRecognizer
    }

    @objc
    public func longGestureRecognizerHandler(sender _: UILongPressGestureRecognizer) {
        print("---- longGestureRecognizerHandler -----")
        if longPressGestureHander != nil {
            longPressGestureHander!()
        }
    }

    public func removeTapGestureRecognizer() {
        if let tap = tapGestureRecognizer {
            removeGestureRecognizer(tap)
        }
    }

    public func removeLongPressGestureRecognizer() {
        if let long = longPressGestureRecognizer {
            removeGestureRecognizer(long)
        }
    }
}
