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

import UIKit

class SpeakerSetupErrorViewController: 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: - Overridden Methods
extension SpeakerSetupErrorViewController {
	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)
		navigationItem.backBarButtonItem = bbi

        if let setUpInfo = setUpInfo {
            switch setUpInfo.currentBaseChannel {
            case .none:
                break
            case .subwoofer:
                label?.text = NSLocalizedString("Tap Try Again to add a subwoofer.", comment: "Error message")
                title = NSLocalizedString("SUBWOOFER NOT ADDED", comment: "Error Title")
            case .leftSurround:
                label?.text = NSLocalizedString("Surrounds must be added in left and right pairs.\n\n" +
                    "Tap Try Again to add a left surround.\n\n" +
                    "If you exit setup now, no surrounds will be added to your soundbar.", comment: "Error message")
                title = NSLocalizedString("SURROUNDS NOT ADDED", comment: "Error Title")
            case .rightSurround:
                label?.text = NSLocalizedString("Surrounds must be added in left and right pairs.\n\n" +
                    "Tap Try Again to add a right surround.\n\n" +
                    "If you exit setup now, no surrounds will be added to your soundbar.", comment: "Error message")
                title = NSLocalizedString("SURROUNDS NOT ADDED", comment: "Error Title")
            case .leftRearSurround:
                label?.text = NSLocalizedString("Surrounds must be added in left and right pairs.\n\n" +
                    "Tap Try Again to add a left rear surround.\n\n" +
                    "If you exit setup now, no surrounds will be added to your soundbar.", comment: "Error message")
                title = NSLocalizedString("SURROUNDS NOT ADDED", comment: "Error Title")
            case .rightRearSurround:
                label?.text = NSLocalizedString("Surrounds must be added in left and right pairs.\n\n" +
                    "Tap Try Again to add a right rear surround.\n\n" +
                    "If you exit setup now, no surrounds will be added to your soundbar.", comment: "Error message")
                title = NSLocalizedString("SURROUNDS NOT ADDED", comment: "Error Title")
            case .leftUpfiringSurround:
                label?.text = NSLocalizedString("Surrounds must be added in left and right pairs.\n\n" +
                    "Tap Try Again to add a left upfiring surround.\n\n" +
                    "If you exit setup now, no surrounds will be added to your soundbar.", comment: "Error message")
                title = NSLocalizedString("SURROUNDS NOT ADDED", comment: "Error Title")
            case .rightUpfiringSurround:
                label?.text = NSLocalizedString("Surrounds must be added in left and right pairs.\n\n" +
                    "Tap Try Again to add a right upfiring surround.\n\n" +
                    "If you exit setup now, no surrounds will be added to your soundbar.", comment: "Error message")
                title = NSLocalizedString("SURROUNDS NOT ADDED", comment: "Error Title")
            case .leftRearUpfiringSurround:
                label?.text = NSLocalizedString("Surrounds must be added in left and right pairs.\n\n" +
                    "Tap Try Again to add a left rear upfiring surround.\n\n" +
                    "If you exit setup now, no surrounds will be added to your soundbar.", comment: "Error message")
                title = NSLocalizedString("SURROUNDS NOT ADDED", comment: "Error Title")
            case .rightRearUpfiringSurround:
                label?.text = NSLocalizedString("Surrounds must be added in left and right pairs.\n\n" +
                    "Tap Try Again to add a right rear upfiring surround.\n\n" +
                    "If you exit setup now, no surrounds will be added to your soundbar.", comment: "Error message")
                title = NSLocalizedString("SURROUNDS NOT ADDED", comment: "Error Title")
            default:
                break
            }
        }
    }
}

// MARK: - UI Actions
extension SpeakerSetupErrorViewController {
    @IBAction private func exitTapped(_ sender: Any?) {
        if
            let setUpInfo = setUpInfo,
            let soundbar = playerController.player(id: soundbarPlayerID!) {
            let currentSpeaker = setUpInfo.currentBaseChannel
            if currentSpeaker == .rightSurround {
                var channels = [NSNumber(integerLiteral: IoTHomeTheaterChannel.leftSurround.rawValue)]
                if setUpInfo.upfiringRequested {
                    channels.append(NSNumber(integerLiteral: IoTHomeTheaterChannel.leftUpfiringSurround.rawValue))
                }
                soundbar.removeHomeTheaterChannels(channels,
                                                   completion: nil,
                                                   completionQueue: nil)
            } else if currentSpeaker == .rightRearSurround {
                var channels = [NSNumber(integerLiteral: IoTHomeTheaterChannel.leftRearSurround.rawValue)]
                if setUpInfo.upfiringRequested {
                    channels.append(NSNumber(integerLiteral: IoTHomeTheaterChannel.leftRearUpfiringSurround.rawValue))
                }
                soundbar.removeHomeTheaterChannels(channels,
                                                   completion: nil,
                                                   completionQueue: nil)
            } else if currentSpeaker == .rightUpfiringSurround {
                soundbar.removeHomeTheaterChannels([NSNumber(integerLiteral: IoTHomeTheaterChannel.leftUpfiringSurround.rawValue)],
                                                   completion: nil,
                                                   completionQueue: nil)
            } else if currentSpeaker == .rightRearUpfiringSurround {
                soundbar.removeHomeTheaterChannels([NSNumber(integerLiteral: IoTHomeTheaterChannel.leftRearUpfiringSurround.rawValue)],
                                                   completion: nil,
                                                   completionQueue: nil)
            }
        }
        navigationController?.presentingViewController?.dismiss(animated: true, completion: nil)
    }
	
	@IBAction private func tryAgainTapped(_ sender: Any?) {
		navigationController?.popViewController(animated: true)
	}
}
