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

import UIKit

class LaunchViewController: UIViewController, UIViewControllerDependencies {
    @IBOutlet private weak var pleaseAcceptLabel: UILabel?
    @IBOutlet private weak var viewButton: UIButton?
    @IBOutlet private weak var acceptButton: UIButton?
}

// MARK: - Overridden Methods
extension LaunchViewController {
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        acceptButton?.makeAccessible(Accessibility.LaunchScreen.acceptButton)
        viewButton?.makeAccessible(Accessibility.LaunchScreen.viewTermsButton)
        hideButtonsAsRequired()
    }
	
	override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
		if segue.identifier == "showTerms" {
			guard let navController = segue.destination as? UINavigationController else {
				print ("Not nav controller but: \(segue.destination)")
				return
			}
			guard let textVC = navController.viewControllers.last as? TextViewController else {
				print ("Not TextViewController")
				return
			}
			textVC.contents = .termsOfService
			textVC.title = NSLocalizedString("TERMS OF SERVICE", comment: "Terms of Service screen title")
		}
	}
}

// MARK: - UI Actions
extension LaunchViewController {
    @IBAction private func acceptTapped(_ sender: Any?) {
        preferences.userDidAcceptTerms()
        presentingViewController?.dismiss(animated: true,
                                          completion: {
                                            appSpinnerPresenter()?.start()
        })
    }
}

// MARK: - Private Methods
extension LaunchViewController {
	private func hideButtonsAsRequired() {
		let termsAccepted = preferences.hasUserAcceptedTerms()
		
		pleaseAcceptLabel?.isHidden = termsAccepted
		viewButton?.isHidden = termsAccepted
		acceptButton?.isHidden = termsAccepted
	}
}
