//
//  UIViewController+Extension.swift
//  QuickSearchLocation
//
//  Created by mac on 2024/4/10.
//

import UIKit

// MARK: - 一、基本的扩展
public extension UIViewController {
    
    // MARK: 1.1、pop回上一个界面
    /// pop回上一个界面
    func popToPreviousVC() {
        guard let nav = self.navigationController else { return }
        if let index = nav.viewControllers.firstIndex(of: self), index > 0 {
            let vc = nav.viewControllers[index - 1]
            nav.popToViewController(vc, animated: true)
        }
    }
    
    // MARK: 1.2、获取push进来的 VC
    /// 获取push进来的 VC
    /// - Returns: push进来的 VC
    func getPreviousNavVC() -> UIViewController? {
        guard let nav = self.navigationController else { return nil }
        if nav.viewControllers.count <= 1 {
            return nil
        }
        if let index = nav.viewControllers.firstIndex(of: self), index > 0 {
            let vc = nav.viewControllers[index - 1]
            return vc
        }
        return nil
    }
    
    // MARK: 1.3、获取顶部控制器(类方法)
    /// 获取顶部控制器
    /// - Returns: VC
    static func topViewController() -> UIViewController? {
        guard let window = UIApplication.shared.windows.filter({$0.isKeyWindow}).first, let rootVC = window.rootViewController  else {
            return nil
        }
        return top(rootVC: rootVC)
    }
    
    // MARK: 1.4、获取顶部控制器(实例方法)
    /// 获取顶部控制器
    /// - Returns: VC
    func topViewController() -> UIViewController? {
        guard let window = UIApplication.shared.windows.filter({$0.isKeyWindow}).first, let rootVC = window.rootViewController  else {
            return nil
        }
        return Self.top(rootVC: rootVC)
    }
    
    private static func top(rootVC: UIViewController?) -> UIViewController? {
        if let presentedVC = rootVC?.presentedViewController {
            return top(rootVC: presentedVC)
        }
        if let nav = rootVC as? UINavigationController,
            let lastVC = nav.viewControllers.last {
            return top(rootVC: lastVC)
        }
        if let tab = rootVC as? UITabBarController,
            let selectedVC = tab.selectedViewController {
            return top(rootVC: selectedVC)
        }
        return rootVC
    }
    
    // MARK: 1.5、是否正在展示
    /// 是否正在展示
    var isCurrentVC: Bool {
        return isViewLoaded == true && (view!.window != nil)
    }
    
    // MARK: 1.6、关闭当前的控制器
    /// 关闭当前的控制器
    func closeCurrentVC() {
        guard let nav = self.navigationController else {
            dismiss(animated: true, completion: nil)
            return
        }
        if nav.viewControllers.count > 1 {
            nav.popViewController(animated: true)
        } else if let _ = nav.presentingViewController {
            nav.dismiss(animated: true, completion: nil)
        }
    }
    
    //MARK: 1.7、dismiss到某个vc
    /// dismiss到某个vc
    /// - Parameters:
    ///   - vc: vc
    ///   - animated: 是否需要动画
    @discardableResult
    func dismiss(vc: AnyClass, animated: Bool) -> Bool {
        guard let targetPresentingVC = findPresentingViewController(fromVc: self, toVc: vc) else { return false }
        targetPresentingVC.dismiss(animated: animated)
        return true
    }
    
    //MARK: 寻找到对应的presentingViewController
    /// 寻找到对应的presentingViewController
    /// - Parameters:
    ///   - fromVc: 当前vc
    ///   - toVc: 目标vc
    /// - Returns: 目标presentingViewController
    private func findPresentingViewController(fromVc: UIViewController, toVc: AnyClass) -> UIViewController? {
        // 判断是否存在对应的presentingViewController
        guard let vc = fromVc.presentingViewController else { return nil }
        // 判断是否是目标vc
        if (vc.isMember(of: toVc)) {
            // 寻找到对应的presentingViewController
            return vc
        } else {
            return findPresentingViewController(fromVc: vc, toVc: toVc)
        }
    }
    
    func pushVC(vc: UIViewController ) {
        
        if vc.isKind(of: UIViewController.self) == false {
            return
        }
        
        if self.navigationController == nil {
            return
        }
        
        if vc.hidesBottomBarWhenPushed == false {
            vc.hidesBottomBarWhenPushed = true
        }
        
        self.navigationController?.pushViewController(vc, animated: true)
    }
}
