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

import UIKit

class SpeakerSetUpImageViewController: UIViewController {
	var onAction: ( ()->() )?
    @IBOutlet private weak var layoutImageView: UIImageView!
    @IBOutlet private weak var instructionLabel: UILabel!
    @IBOutlet private weak var upfiringButton: UIButton!

    var setUpInfo: SpeakerSetUpInfo?
	
	@IBAction private func doAction(_ sender: Any?) {
		if let handler = onAction {
			handler()
		}
	}

    @IBAction private func upfiringButtonTapped(_ button: UIButton) {
        setUpInfo?.toggleMarkUpfiringAsRequested()
        updateUpfiringButton()
    }

    func updateUpfiringButton() {
        if let setUpInfo = setUpInfo,
            setUpInfo.upfiringAvailable {
            upfiringButton.isHidden = false
            upfiringButton.isEnabled = !setUpInfo.isLastChannel
            let image = setUpInfo.upfiringRequested ? UIImage(named: "ic-checkmark-checked") :
                UIImage(named:"ic-checkmark-empty")
            upfiringButton.setImage(image, for: .normal)
            upfiringButton.setImage(image, for: .selected)
            upfiringButton.setImage(image, for: .disabled)
            upfiringButton.setImage(image, for: .highlighted)
        } else {
            upfiringButton.isHidden = true
        }
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        if let channel = setUpInfo?.currentBaseChannel {
            switch channel {
            case .subwoofer:
                instructionLabel.text = NSLocalizedString("Place your subwoofer near a wall or in a corner of your room and turn it on.",
                                                          comment: "")
                layoutImageView.image = UIImage(named: "room-diagram-sub-highlighted")
            case .leftSurround, .rightSurround:
                instructionLabel.text = NSLocalizedString("Place your surrounds where you'd like them and turn them on. Note that surrounds must be added in pairs.",
                                                          comment: "")
                layoutImageView.image = UIImage(named: "room-diagram-left-right-surrounds-highlighted")
            case .leftRearSurround, .rightRearSurround:
                instructionLabel.text = NSLocalizedString("Place your rear surrounds where you'd like them and turn them on. Note that surrounds must be added in pairs.",
                                                          comment: "")
                layoutImageView.image = UIImage(named: "room-diagram-rear-surrounds-highlighted")
            case .leftUpfiringSurround, .rightUpfiringSurround:
                instructionLabel.text = NSLocalizedString("Place your upfiring surrounds where you'd like them and turn them on. Note that surrounds must be added in pairs.",
                                                          comment: "")
                layoutImageView.image = UIImage(named: "room-diagram-front-upfiring-surrounds-highlighted")
            case .leftRearUpfiringSurround, .rightRearUpfiringSurround:
                instructionLabel.text = NSLocalizedString("Place your rear upfiring surrounds where you'd like them and turn them on. Note that surrounds must be added in pairs.",
                                                          comment: "")
                layoutImageView.image = UIImage(named: "room-diagram-rear-upfiring-surrounds-highlighted")
            default:
                break
            }

           	updateUpfiringButton()
        }
    }
}
