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

import UIKit

class VoiceUIDefaultServiceViewController: SettingsOptionsViewController {
    // MARK: Private Properties
    private var services = [IoTVoiceUIClient] ()
}

extension VoiceUIDefaultServiceViewController {
    override func viewDidLoad() {
        source = self
        title = NSLocalizedString("DEFAULT VOICE SERVICE", comment: "Voice UI Title")

        super.viewDidLoad()

        // Accessibility
        tableView.makeAccessible(Accessibility.VoiceUIDefaultServiceScreen.servicesList)
    }

    // Next two methods are to make auto layout magic work by resizing the header height to fit the text with
    // appropriate margins.
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        sizeHeaderToFit()
    }

    func sizeHeaderToFit() {
        let headerView = tableView.tableHeaderView!

        headerView.setNeedsLayout()
        headerView.layoutIfNeeded()

        let height = headerView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize).height

        var frame = headerView.frame
        let oldHeight = frame.size.height

        if abs(oldHeight-height) > 0.01 {
            // We only update if it's changed otherwise we enter infinite loop
            frame.size.height = height
            headerView.frame = frame
            tableView.tableHeaderView = headerView
        }
    }
}

extension VoiceUIDefaultServiceViewController: SettingsOptionsSource {
    func requiredObservers() -> [SettingsOptionsRequiredObservers] {
        return [.device, .voiceUI]
    }

    func isCurrentSelection(at index: Int) -> Bool {
        guard
            let deviceID = deviceID,
            let device = playerController.device(id: deviceID),
            let defaultService = device.defaultVoiceUIClient else {
                return false
        }

        let service = services[index]
        return defaultService == service
    }

    func itemSelected(at index: Int) {
        let service = services[index]
        guard
            let deviceID = deviceID,
            let device = playerController.device(id: deviceID),
            let defaultService = device.defaultVoiceUIClient,
            defaultService != service else {
                return
        }
        device.setDefaultVoiceUIClient(service,
                                       completion: nil,
                                       completionQueue: nil)
    }

    func item(at index: Int) -> SettingsOptionsSourceItem {
        let service = services[index]
        let unlocalized =  VoiceUIHelper.unlocalizedNameForService(service)
        return SettingsOptionsSourceItem(cellTitle: NSLocalizedString(unlocalized, comment: "Service Name"),
                                         unlocalizedAccessibilityTitle: unlocalized)
    }

    func numberOfItems() -> Int {
        return services.count
    }

    func loadOptions() {
        services = [IoTVoiceUIClient] ()
        if let deviceID = deviceID,
            let device = playerController.device(id: deviceID) {
            services = device.voiceUIClients
        }
    }
}
