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

import UIKit

enum SpeakerSetUpUpfiringChannels{
    case unavailable
    case available([IoTHomeTheaterChannel])
    case requested([IoTHomeTheaterChannel])
}

class SpeakerSetUpInfo {
    private let baseChannels: [IoTHomeTheaterChannel]
    private var upfiringChannels: SpeakerSetUpUpfiringChannels
    private var currentIndex = 0

    var upfiringAvailable: Bool {
        switch upfiringChannels {
        case .unavailable:
            return false
        case .available(_):
            return true
        case .requested(_):
            return true
        }
    }

    var upfiringRequested: Bool {
        switch upfiringChannels {
        case .unavailable:
            return false
        case .available(_):
            return false
        case .requested(_):
            return true
        }
    }

    var isLastChannel: Bool {
        return currentIndex == baseChannels.count - 1
    }

    var currentBaseChannel: IoTHomeTheaterChannel {
        return baseChannels[currentIndex]
    }

    var currentUpfiringChannel: IoTHomeTheaterChannel? {
        switch upfiringChannels {
        case .unavailable:
            return nil
        case .available(let channels):
            return channels[currentIndex]
        case .requested(let channels):
            return channels[currentIndex]
        }
    }

    init(baseChannels: [IoTHomeTheaterChannel],
         upfiringChannels: [IoTHomeTheaterChannel]?) {
        assert(baseChannels.count > 0)

        self.baseChannels = baseChannels
        if let upfiringChannels = upfiringChannels {
            assert(baseChannels.count == upfiringChannels.count)
            self.upfiringChannels = .available(upfiringChannels)
        } else {
            self.upfiringChannels = .unavailable
        }
    }

    func moveToNextChannel() {
        if currentIndex >= baseChannels.count - 1 {
            fatalError()
        }
        currentIndex = currentIndex + 1
    }

    func toggleMarkUpfiringAsRequested() {
        switch upfiringChannels {
        case .unavailable:
            assertionFailure()
        case .available(let channels):
            upfiringChannels = .requested(channels)
        case .requested(let channels):
            upfiringChannels = .available(channels)
        }
    }

    func markUpfiringAsUnavailable() {
        upfiringChannels = .unavailable
    }
}

