//
//  BaseViewController.swift
//  Scanner
//
//  Created by FastKey on 2021/7/1.
//

import UIKit
import RxSwift
class LLQ_BaseViewController: UIViewController {
    var showNavigationBar: Bool = true
    var customizeBackItem: Bool = false
    var allowGestureBack = true
    var viewIsAppearing: Bool = false
    var viewDidLoaded = false
    var disposeBg = DisposeBag()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.hidesBottomBarWhenPushed = true
        self.customizeNavigationBarBackItem()
        self.edgesForExtendedLayout = .init(rawValue: 0)
        
        self.viewDidLoaded = true
        self.sp_set()
        
    }
    
    public func sp_set(){
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        self.navigationController?.setNavigationBarHidden(!self.showNavigationBar, animated: true)
        viewIsAppearing = true

        if allowGestureBack {
            if customizeBackItem {
                navigationController?.interactivePopGestureRecognizer?.isEnabled = true
                navigationController?.interactivePopGestureRecognizer?.delegate = self as? UIGestureRecognizerDelegate
            } else {
                navigationController?.interactivePopGestureRecognizer?.isEnabled = false
            }
        } else {
            navigationController?.interactivePopGestureRecognizer?.isEnabled = false
        }
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        viewIsAppearing = false
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .default
    }
    
    func setNavigationBarLucent(){
        let naviBGView = self.navigationController?.navigationBar.subviews[0]
        naviBGView?.alpha = 0
        naviBGView?.subviews[0].alpha = 0
        if #available(iOS 11.0, *) {
            naviBGView?.subviews[1].alpha = 0
        }
    }
    
    func resetNavigationBar(){
        let naviBGView = self.navigationController?.navigationBar.subviews[0]
        naviBGView?.alpha = 1
        naviBGView?.subviews[0].alpha = 1
        if #available(iOS 11.0, *) {
            naviBGView?.subviews[1].alpha = 1
        }
    }
}

extension LLQ_BaseViewController {
    private func customizeNavigationBarBackItem() {

        let image = UIImage.init(named: "icon_common_back")
        navigationItem.leftBarButtonItem = UIBarButtonItem(image: image, style: .plain, target: self, action: #selector(popViewController))
    }
    @objc func popViewController() {
        navigationController?.popViewController(animated: true)
    }
    
}
