//
//  LLQ_RootViewController.swift
//  Scanner
//
//  Created by FastKey on 2021/7/13.
//

import UIKit
import Cartography
import PromiseKit
class LLQ_RootViewController: LLQ_BaseViewController {
    private lazy  var contentController : LLQ_BaseNavigationController = {
        let homeVc = UIStoryboard.main().initController(id: "LLQ_HomeController") as! LLQ_HomeController
        let nav = LLQ_BaseNavigationController.init(rootViewController: homeVc)
        return nav
    }()
    let launchVc = UIStoryboard.main().initController(id: "LLQ_LaunchViewController") as! LLQ_LaunchViewController
    var enterBackgroundDate: Date = Date()
    var loadDate = Date()
    var initializeShowHomePage = false
    var iapBinding = false
    
    var addedLaunchVc = false
    
    func addLanchViewController(){
        if self.addedLaunchVc {
            return
        }
        self.addedLaunchVc = true
        self.addChild(self.launchVc)
        self.view.addSubview(self.launchVc.view)
        constrain(self.launchVc.view, self.view) { launch, view in
            launch.edges == view.edges
        }
        self.launchVc.addAnimation()

    }
    
    func removeLaunchViewController(){
        
        self.launchVc.view.removeFromSuperview()
        self.launchVc.removeFromParent()
        self.addedLaunchVc = false
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.showNavigationBar = false
        self.loadDate = Date()
        self.initContentController()
        self.addLanchViewController()
        

        self.launchVc.view.tag = 9990

        DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
           
            self.removeLaunchViewController()
            
        }
    }
    

    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    
    private func initContentController(){
        self.addChild(self.contentController)
        self.view.insertSubview(self.contentController.view, at: 0)
    }
    

    
    private func showHomePage(){
//        LLQ_ScannerLog("[Launch] [Hot] show home Page")
        if Date().timeIntervalSince(self.loadDate) < 2 {
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
                self.showHomePage()
            }
            return
        }
        
        self.removeLaunchViewController()
        
        if let vc = self.contentController.viewControllers[0] as? LLQ_HomeController,  !self.initializeShowHomePage{
            self.initializeShowHomePage = true
            vc.initializeShowHomePage()
        }
    }

    
    override var childForStatusBarStyle: UIViewController? {
        return self.contentController
        
    }
}

extension LLQ_RootViewController {

    static var now : LLQ_RootViewController {
        let nav = UIApplication.shared.keyWindow?.rootViewController as! LLQ_BaseNavigationController
        
        return  nav.viewControllers.first as! LLQ_RootViewController
    }
    
    func topPresentedVC(for viewController: UIViewController = LLQ_RootViewController.now) -> UIViewController {

        // default is self
        let vc = viewController
        if let presented = vc.presentedViewController {
            return topPresentedVC(for: presented)
        }
        return vc
    }
}

