//
//  BsBaseViewController.swift
//  BaiSiSMApp
//
//  Created by davidhuang on 2022/10/21.
//  Copyright © 2022 www.davidhuang.com. All rights reserved.
//

import UIKit
import GKNavigationBarSwift
import IQKeyboardManagerSwift

class YHBaseViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //背景色
        view.backgroundColor =  UIColor.pageBkgColor
        
        //vc中导航栏默认透明,需要使用到的 在各自的vc中定制
        gk_navBackgroundColor = .clear
        gk_navLineHidden = true
        gk_navBarAlpha = 0
        
        gk_backImage = UIImage(named: "back_icon")
        
        
#if DEBUG
        IQKeyboardManager.shared.isEnabled = true
#endif
//        navigationController?.interactivePopGestureRecognizer?.delegate = self
        
        if let key = getKeys(),!key.isEmpty {
            YHAnalytics.track(key)
        }
    }
    
    override var prefersStatusBarHidden: Bool {
        return self.gk_statusBarHidden
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return self.gk_statusBarStyle
    }
    
    deinit {
#if DEBUG
        printLog(#function)
#endif
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        if let key = getKeys(),!key.isEmpty {
            YHAnalytics.timeStrat(key)
        }
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        if let key = getKeys(),!key.isEmpty {
            YHAnalytics.timeEnd(key)
        }
    }
    
    func getKeys() -> String? {
        let key = String(describing: type(of: self))
        if YHAnalyticsUnit.events.keys.contains(key) {
            guard let event = YHAnalyticsUnit.events[key] else { return nil}
            if key == "YHHomeInfoDetailContainerViewController" || key == "YHH5WebViewVC" || key == "YHAIRobotChatViewController" {
                let subevent = self.title ?? ""
                let events = event as? [String: String] ?? [:]
                if events.keys.contains(subevent) {
                    guard let subEvent = events[subevent] else { return nil}
                    return subEvent
                }
            }
            
            if let value = event as? String {
                return value
            }
        }
        return nil
    }
    
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
    }
    
    func popGestureClose() {
        if let ges = self.navigationController?.interactivePopGestureRecognizer?.view?.gestureRecognizers {
            for item in ges {
                item.isEnabled = false
            }
        }
    }
    
    func popGestureOpen() {
        if let ges = self.navigationController?.interactivePopGestureRecognizer?.view?.gestureRecognizers {
            for item in ges {
                item.isEnabled = false
            }
        }
    }
    
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        if children.count == 1 {
            return false
        }else{
            return true
        }
    }
}

//extension YHBaseViewController: UIGestureRecognizerDelegate {
////     这个方法是在手势将要激活前调用
////     返回YES允许右滑手势的激活，返回NO不允许右滑手势的激活
//    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
//        return true
//    }
//}

