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

import UIKit

class SpeakerSetUpPageViewController: UIPageViewController {
	// MARK: Public Properties
    var setUpInfo: SpeakerSetUpInfo?
	var soundbarPlayerID: String?
    var soundbarDeviceID: String?
	
	// MARK: Private Properties/Types
	private struct Segues {
		static let showSpeakers = "showSpeakers"
	}
	
	private let storyboardName = "SetUpSpeaker"
	private let ethernetViewStoryboardName = "Ethernet"
	private let roomLayoutViewStoryboardName = "SpeakerSetUpImageViewController"
	private var bailOutViewController: UIViewController?
}

// MARK: - Overriden Methods
extension SpeakerSetUpPageViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        prepareModalNavigationBar()
    }

	override func viewWillAppear(_ animated: Bool) {
		super.viewWillAppear(animated)
		
		// No title for back button to centre next title
		let bbi = UIBarButtonItem(title: " ", style: .plain, target: nil, action: nil)
		navigationItem.backBarButtonItem = bbi
		
		let vcs = navigationController?.viewControllers ?? []
		if vcs.count > 1 {
			bailOutViewController = vcs[vcs.count - 2]
		} else {
			bailOutViewController = navigationController?.topViewController
		}

        if let channel = setUpInfo?.currentBaseChannel {
            switch channel {
            case .subwoofer:
                title = NSLocalizedString("ADD SUBWOOFER", comment: "Choose speaker text")
            case .leftSurround, .rightSurround:
                title = NSLocalizedString("ADD SURROUNDS", comment: "Choose speaker text")
            case .leftRearSurround, .rightRearSurround:
                title = NSLocalizedString("ADD REAR SURROUNDS", comment: "Choose speaker text")
            case .leftUpfiringSurround, .rightUpfiringSurround:
                title = NSLocalizedString("ADD UPFIRING SURROUNDS", comment: "Choose speaker text")
            case .leftRearUpfiringSurround, .rightRearUpfiringSurround:
                title = NSLocalizedString("ADD REAR UPFIRING SURROUNDS", comment: "Choose speaker text")
            default:
                break
            }
        }
		showAppropriateVC()
	}
	
	override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
		if let dest = segue.destination as? SpeakerSetUpChooseSpeakerViewController {
			dest.injectDependencies(preferences: preferences, playerController: playerController)
			dest.bailOutViewController = bailOutViewController
			dest.soundbarPlayerID = soundbarPlayerID
            dest.soundbarDeviceID = soundbarDeviceID
			dest.setUpInfo = setUpInfo
		}
	}
}

// MARK: - Private Methods
extension SpeakerSetUpPageViewController {
    private func showAppropriateVC() {
        let sb = UIStoryboard(name: storyboardName, bundle: nil)
        if let player = playerController.player(id: soundbarPlayerID!),
            player.isSoundBar,
            let _ = playerController.device(player: player) {
            let roomLayoutVC = sb.instantiateViewController(withIdentifier: roomLayoutViewStoryboardName) as! SpeakerSetUpImageViewController
            
            roomLayoutVC.onAction = { [weak self] in
                guard let self = self else { return }
                self.showNext()
            }
            roomLayoutVC.setUpInfo = setUpInfo
            setViewControllers([roomLayoutVC],
                               direction: .forward,
                               animated: false,
                               completion: nil)
        }
    }

	private func showNext() {
		performSegue(withIdentifier: Segues.showSpeakers, sender: self)
	}
}
