//
//  UIView+Extension.swift
//  Lanu
//
//  Created by OneeChan on 2025/11/11.
//

import Foundation
import UIKit


extension UIView {
    var navigationController: UINavigationController? {
        let vc = viewController
        if let nav = vc as? UINavigationController {
            return nav
        }
        return vc?.navigationController
    }
    
    var viewController: UIViewController? {
        var responder: UIResponder? = self
        while responder != nil {
            if let viewController = responder as? UIViewController {
                return viewController
            }
            responder = responder?.next
        }
        if let window = self as? UIWindow {
            return window.rootViewController
        } else if let window {
            return window.rootViewController
        }
        return nil
    }
    
    static var appKeyWindow: UIWindow? {
        let scenes = UIApplication.shared.connectedScenes
        if scenes.isEmpty { return nil }
        
        var activeScene = scenes.filter { $0.activationState == .foregroundActive }
        if activeScene.isEmpty {
            activeScene.insert(scenes.first!)
        }
        
        let windowScenes = scenes.compactMap { $0 as? UIWindowScene }
        if windowScenes.isEmpty { return nil }
        
        return windowScenes.first?.windows.filter { $0.isKeyWindow }.first
    }
    
    static var statusBarHeight: CGFloat = {
        guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
        else {
            return 0
        }
        return windowScene.statusBarManager?.statusBarFrame.height ?? 0
    }()
    
    static var navigationBarHeight: CGFloat = {
        guard let navigationController = UIView.appKeyWindow?.rootViewController as? UINavigationController else {
            return 44
        }
        return navigationController.navigationBar.bounds.height
    }()
    
    var safeBottomInset: CGFloat {
        Self.appKeyWindow?.safeAreaInsets.bottom ?? 0
    }
    
    var commonBottomInset: CGFloat {
        -safeBottomInset - 10
    }
}

// MARK: 点击响应
extension UIView {
    private class BlockTapGestureRecognizer: UITapGestureRecognizer {
        private var actionBlock: (() -> Void)?
        
        init(action block: (() -> Void)?) {
            super.init(target: nil, action: nil)
            self.actionBlock = block
            // 设置目标和动作
            addTarget(self, action: #selector(handleTap(_:)))
        }
        
        @objc private func handleTap(_ gesture: BlockTapGestureRecognizer) {
            actionBlock?()
        }
    }
    
    func onTap(_ block: @escaping () -> Void) {
        let tap = BlockTapGestureRecognizer(action: block)
        addGestureRecognizer(tap)
        isUserInteractionEnabled = true
    }
    
    private class BlockLongPressGestureRecognizer: UILongPressGestureRecognizer {
        private var actionBlock: (() -> Void)?
        
        init(action block: (() -> Void)?) {
            super.init(target: nil, action: nil)
            self.actionBlock = block
            // 设置目标和动作
            addTarget(self, action: #selector(handleTap(_:)))
        }
        
        @objc private func handleTap(_ gesture: BlockTapGestureRecognizer) {
            actionBlock?()
        }
    }
    
    func onLongPress(_ block: @escaping () -> Void) {
        let tap = BlockLongPressGestureRecognizer(action: block)
        addGestureRecognizer(tap)
    }
}
