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

import UIKit

class SpeakerSetUpSuccessViewController: UIViewController, SpeakerSetUpSuccessOrFailure {
	// MARK: Public Properties
	var bailOutViewController: UIViewController?
	var soundbarPlayerID: String?
    var soundbarDeviceID: String?
	var setUpInfo: SpeakerSetUpInfo?
	
	// MARK: Outlets
	@IBOutlet private weak var label: UILabel?
	
	// MARK: Private Properties
	private let storyboardName = "SetUpSpeaker"
	private let chooseSpeakerVCID = "SpeakerSetUpChooseSpeakerViewController"
}

// MARK: - Overridden Methods
extension SpeakerSetUpSuccessViewController {
	override func viewDidLoad() {
		super.viewDidLoad()
		navigationItem.setHidesBackButton(true, animated: false)
		
		// No title for back button to centre next title
		let bbi = UIBarButtonItem(title: " ", style: .plain, target: nil, action: nil)
		self.navigationItem.backBarButtonItem = bbi
		
		if
            let soundbar = playerController.player(id: soundbarPlayerID!),
            let speakerType = setUpInfo?.currentBaseChannel {
			let soundbarName = soundbar.displayName
			var additionName = ""
			
			switch speakerType {
            case .none:
                break
			case .subwoofer:
				additionName = NSLocalizedString("subwoofer", comment: "Subwoofer name")
			case .leftSurround:
				additionName = NSLocalizedString("left surround", comment: "Left Surround name")
			case .rightSurround:
				additionName = NSLocalizedString("right surround", comment: "Right Surround name")
            case .leftRearSurround:
                additionName = NSLocalizedString("left rear surround", comment: "Left Rear Surround name")
            case .rightRearSurround:
                additionName = NSLocalizedString("right rear surround", comment: "Right Rear Surround name")
            case .leftUpfiringSurround:
                additionName = NSLocalizedString("left upfiring surround", comment: "Left Upfiring Surround name")
            case .rightUpfiringSurround:
                additionName = NSLocalizedString("right upfiring surround", comment: "Right Upfiring Surround name")
            case .leftRearUpfiringSurround:
                additionName = NSLocalizedString("left rear upfiring surround", comment: "Left Rear Upfiring Surround name")
            case .rightRearUpfiringSurround:
                additionName = NSLocalizedString("right rear upfiring surround", comment: "Right Rear Upfiring Surround name")
            default:
                break
			}
			
			let message = NSString.localizedStringWithFormat("Your %@ has been added to your %@ soundbar.", additionName, soundbarName) as String
			
			label?.text = message
		}
	}
}

// MARK: - UI Actions
extension SpeakerSetUpSuccessViewController {
    private func doNextSurroundInPair() {
        // We go around again for the right speaker
        let sb = UIStoryboard(name: storyboardName, bundle: nil)
        let vc = sb.instantiateViewController(withIdentifier: chooseSpeakerVCID) as! SpeakerSetUpChooseSpeakerViewController
        vc.bailOutViewController = bailOutViewController
        setUpInfo?.moveToNextChannel()
        vc.setUpInfo = setUpInfo
        vc.soundbarPlayerID = soundbarPlayerID
        vc.soundbarDeviceID = soundbarDeviceID
        vc.injectDependencies(preferences: preferences, playerController: playerController)
        show(vc, sender: self)
    }

	@IBAction private func nextTapped() {
        if setUpInfo?.isLastChannel ?? true {
			navigationController?.presentingViewController?.dismiss(animated: true, completion: nil)
        } else {
            doNextSurroundInPair()
        }
	}
}
