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

import UIKit

class MediaServersMusicListDisplayHelperItem : MusicListDisplayHelperItem {
	public private(set) var server: UPnPMediaServer
	
	init(server: UPnPMediaServer) {
		self.server = server
		super.init()
		
		self.state = .available
		self.title = server.name
		self.subtitle = ""
		self.accessory = .unchecked // This removes the default artwork image
		self.options = []
	}
}

// MARK: -

class MediaServersDataSourceDelegate {
	private var servers: [UPnPMediaServer] = []
	var onItemsInvalidated: (() -> ())?
	
	init (servers: [UPnPMediaServer]) {
		self.servers = servers
	}
}

// MARK: - Public Methods
extension MediaServersDataSourceDelegate {
	func forceReload(servers: [UPnPMediaServer]) {
		self.servers = servers
		if let handler = onItemsInvalidated {
			handler()
		}
	}
}

// MARK: - MusicListDataSourceDelegate Conformance
extension MediaServersDataSourceDelegate: MusicListDataSourceDelegate {
	var title: String {
		return NSLocalizedString("Media Servers", comment: "Media Servers Title")
	}

    func itemsInitialState(completion: @escaping ([MusicListDisplayHelperItem]) -> () , completionQueue: DispatchQueue) -> () {
       let array = servers.map { return MediaServersMusicListDisplayHelperItem(server: $0) }

        completionQueue.async {
            completion(array)
        }
    }
	
	func itemsInitialState() -> [MusicListDisplayHelperItem] {
		let array = servers.map { return MediaServersMusicListDisplayHelperItem(server: $0) }
		return array
	}
	
	func accessoryForItem(_ item: MusicListDisplayHelperItem, atIndex index: Int) -> MusicListDisplayHelperItemAccessory {
		return item.accessory
	}
	
	func mediaItemForItem(_ item: MusicListDisplayHelperItem, atIndex index: Int) -> IoTMediaItem? {
		return nil
	}
	
	func optionsForItem(_ item: MusicListDisplayHelperItem, atIndex index: Int) -> [String] {
		return []
	}
}

