//
// © 2017-2020 Qualcomm Technologies, Inc. and/or its subsidiaries. All rights reserved.
//

import UIKit

class SplitViewController: UISplitViewController, UIViewControllerDependencies {
	// MARK: Private Properties/Types
    private struct Segues {
        static let presentLaunch = "presentLaunch"
    }
    private var didShowLoadScreen = false
}

// MARK: - Overridden Methods
extension SplitViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
		view.alpha = 0 // We hide ourselves until we are running.
        view.backgroundColor = UIColor.black
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        for controller in viewControllers {
            controller.injectDependencies(preferences: preferences, playerController: playerController)
            if let navController = controller as? UINavigationController {
                navController.topViewController?.injectDependencies(preferences: preferences, playerController: playerController)
            }
        }
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        if !didShowLoadScreen {
            didShowLoadScreen = true
            if !preferences.hasUserAcceptedTerms() {
                self.performSegue(withIdentifier: Segues.presentLaunch, sender: self)
            } else {
                appSpinnerPresenter()?.start()
            }
        }
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let destination = segue.destination as? UIViewControllerDependencies {
            destination.injectDependencies(preferences: preferences, playerController: playerController)
        }
    }

    override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
        // We seem to get unbalanced disappear/appear callbacks when presenting/dismissing modal views.
        // These callbacks are relied upon by the settings VCs to reenable observers/reload etc
        // We trigger them manually here.
        var needWorkaround = false
        if let navController = presentedViewController as? UINavigationController,
            let top = navController.topViewController {
            needWorkaround = top is ModalViewController ||
                top is ModalTableViewController
        }

        if needWorkaround {
            for controller in viewControllers {
                if let navController = controller as? UINavigationController,
                    let settingsController = navController.topViewController as? SettingsNavigationController {
                    settingsController.topViewController?.viewWillAppear(flag)
                }
            }
        }

        super.dismiss(animated: flag, completion: {
            if needWorkaround {
                for controller in self.viewControllers {
                    if let navController = controller as? UINavigationController,
                        let settingsController = navController.topViewController as? SettingsNavigationController {
                        settingsController.topViewController?.viewDidAppear(flag)
                    }
                }
            }

            if let completion = completion {
                completion()
            }
        })
    }
}
