//
//  QSLBaseNavController.swift
//  QuickSearchLocation
//
//  Created by mac on 2024/4/10.
//

import UIKit

class QSLBaseNavController: UINavigationController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.setNavBarAppearence()
        self.interactivePopGestureRecognizer?.delegate = self;
    }
    
    func setNavBarAppearence() {
        
        // 设置 navbar 背景颜色
        self.navigationBar.setBackgroundImage(UIImage.image(color: .white, size: CGSize(width: QSLConst.qsl_kScreenW, height: QSLConst.qsl_kNavFrameH)), for: .any, barMetrics: .default)
        // 取消 navbar 阴影
        self.navigationBar.shadowImage = UIImage()
        
        // 设置文字颜色、大小
        var textAttr:[NSAttributedString.Key : Any] = [NSAttributedString.Key : Any]()
        textAttr[NSAttributedString.Key.foregroundColor] = UIColor.clear
        textAttr[NSAttributedString.Key.font] = UIFont.textM(17)
        self.navigationBar.titleTextAttributes = textAttr
        
        if #available(iOS 15.0, *) {
            
            let appearance = UINavigationBarAppearance()
//            appearance.backgroundEffect = UIBlurEffect(style: .regular)
            appearance.configureWithOpaqueBackground()
            appearance.titleTextAttributes = textAttr
            appearance.backgroundColor = .white
            
            appearance.shadowColor = .clear
            self.navigationBar.standardAppearance = appearance
            self.navigationBar.scrollEdgeAppearance = appearance
        }
    }
}

extension QSLBaseNavController {
    
    /// 设置当跳转到其他页面时隐藏 tabbar
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        if children.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
        }
        super.pushViewController(viewController, animated: animated)
    }
}

extension QSLBaseNavController: UIGestureRecognizerDelegate {
    
    /// 跳转到其他页面时开启侧滑
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        return children.count > 1
    }
}
