//
//  CLBaseViewController.swift

//
//  Created by Leon on 2025/7/12.
//

import SnapKit
import UIKit

class CLBaseViewController: UIViewController {
    // MARK: - Configs

    class var shouldPresentThisVc: Bool {
        return false
    }

    lazy var navigationView: NavigationView = {
        let view = NavigationView()
        view.backButton.isHidden = true
        return view
    }()

    override var title: String? {
        didSet {
            navigationView.title = title
        }
    }

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }

    override var shouldAutorotate: Bool {
        return false
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }
    
    var isDisplaying: Bool = false

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .c.cbd
        modalPresentationStyle = .fullScreen
        addNavigationView()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if type(of: self).shouldPresentThisVc {
            navigationView.setupBackButtonCloseIcon()
            navigationView.backButton.isHidden = false
            navigationView.backButton.removeTarget(nil, action: nil, for: .touchUpInside)
            navigationView.backButton.addTarget(self, action: #selector(tapNaviClose), for: .touchUpInside)
        } else if presentingViewController != nil && navigationController?.viewControllers.count == 1{
            navigationView.setupBackButtonCloseIcon()
        }
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        isDisplaying = true
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        isDisplaying = false
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
    }

    func disabledFullScreenPan() {
        if let navc: CLNavigationController = navigationController as? CLNavigationController {
            navc.disabledFullScreenPan()
        }
        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
    }

    /// 开启全屏
    func enabledFullScreenPan() {
        if let navc: CLNavigationController = navigationController as? CLNavigationController {
            navc.enabledFullScreenPan()
        }
        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
    }

    @objc func tapNaviClose() {
        dismiss(animated: true)
    }

    deinit {
        print("♻️ \(classForCoder)")

        continueDialogIfHaveWhenDealloc()
    }

    private func continueDialogIfHaveWhenDealloc() {
        guard let subControllers = navigationController?.viewControllers else { return }
        let popControllers = subControllers.filter { $0 is BaseMaskPopDialogController } as! [BaseMaskPopDialogController]

        if !popControllers.isEmpty {
            for per in popControllers {
                if !DialogPopUpManager.shared.toBeHandleMaskPopControllers.contains(per) {
                    DialogPopUpManager.shared.toBeHandleMaskPopControllers.append(per)
                }
            }
            DialogPopUpManager.shared.delayToContinueDialog()
        }
    }
}

extension CLBaseViewController {
    public func addNavigationView() {
        view.addSubview(navigationView)
        navigationView.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.leading.equalToSuperview()
            make.trailing.equalToSuperview()
            make.height.equalTo(UIWindow.navBarTotalHeight)
        }
    }

    public func hiddenBackButton(of isHidden: Bool) {
        navigationView.backButton.isHidden = isHidden
    }

    public func close(dismissFirst:Bool = false, completion:(()->Void)? = nil) {
        if let vcs = navigationController?.viewControllers, vcs.count > 1 {
            if presentingViewController != nil && dismissFirst{
                dismiss(animated: true, completion: completion)
            }else{
                navigationController?.popViewController(animated: true)
            }
        } else {
            dismiss(animated: true, completion: completion)
        }
    }
    
    public func presentNaviRootVc(vc: UIViewController, animated: Bool = true){
//        if let clBaseVc = vc as? CLBaseViewController{ // 转移到viewWillAppear中
//            clBaseVc.navigationView.setupBackButtonCloseIcon()
//        }
        
        let navc = CLNavigationController(rootViewController: vc)
        navc.modalPresentationStyle = .fullScreen
        present(navc, animated: animated)
    }
    
}

class CLViewController<Container: UIView>: CLBaseViewController {
    var container: Container { view as! Container }

    override func loadView() {
        super.loadView()
        if view is Container {
            return
        }
        
        view = Container()
        if let realContainer = view as? CLContainer {
            realContainer.navigationView = navigationView
        }
//        if Container.self is CLContainer.Type{
//            view = CLContainer(navigationView: navigationView)
//        }else{
//            view = Container()
//        }
    }
}
