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

import Foundation

class MediaServerItemsMusicListDisplayHelperItem : MusicListDisplayHelperItem {
	public private(set) var item: UPnPMediaItem
	
	init(item: UPnPMediaItem) {
		self.item = item
		super.init()
		
		self.state = .available
		self.title = item.name
		self.imageURL = self.item.artworkURL
		self.imageLoadFailureIsError = false
		if item.isFolder {
			self.accessory = .none
			self.options = []
		} else if item.isMusicTrack {
			var st = item.artist ?? ""
			let albumTitle = item.albumTitle ?? ""
			if !st.isEmpty && !albumTitle.isEmpty {
				st = st + " - "
			}
			st = st + albumTitle
			self.subtitle = st
			self.accessory = .options
			self.options = [MusicLibraryItemOptions.playNow.toString(),
							MusicLibraryItemOptions.playNext.toString(),
							MusicLibraryItemOptions.addToQueue.toString(),
							MusicLibraryItemOptions.addToPlaylist.toString()]
		} else {
			// Can't be played.
			self.subtitle = ""
			self.state = .unplayable
			self.accessory = .none
			self.options = []
		}
	}
}

// MARK: -

class MediaServerItemsDataSourceDelegate {
	var onItemsInvalidated: (() -> ())?
	private var items: [UPnPMediaItem] = []
	
	init (items: [UPnPMediaItem]) {
		self.items = items
	}
}

// MARK : - Public Methods
extension MediaServerItemsDataSourceDelegate {
	func forceReload(items: [UPnPMediaItem]) {
		self.items = items
		if let handler = onItemsInvalidated {
			handler()
		}
	}
}

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

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

        completionQueue.async {
            completion(array)
        }
    }
	
	func accessoryForItem(_ item: MusicListDisplayHelperItem, atIndex index: Int) -> MusicListDisplayHelperItemAccessory {
		return item.accessory
	}
	
	func mediaItemForItem(_ item: MusicListDisplayHelperItem, atIndex index: Int) -> IoTMediaItem? {
		if let casted = item as? MediaServerItemsMusicListDisplayHelperItem {
			return mediaItemForItem(casted)
		} else {
			return nil
		}
	}
	
	private func mediaItemForItem(_ item: MediaServerItemsMusicListDisplayHelperItem) -> IoTMediaItem? {
		let mediaItem = IoTMediaItem()
		let uPnPItem = item.item

		mediaItem.title = uPnPItem.name
		mediaItem.artist = uPnPItem.artist ?? ""
		mediaItem.album = uPnPItem.albumTitle ?? ""
		mediaItem.genre = uPnPItem.genre ?? ""
		mediaItem.duration = Int32(uPnPItem.durationInSeconds * 1000)
		mediaItem.streamURL = uPnPItem.itemURL ?? ""
		mediaItem.thumbnailURL = uPnPItem.artworkURL ?? ""
		return mediaItem
	}
	
	func optionsForItem(_ item: MusicListDisplayHelperItem, atIndex index: Int) -> [String] {
		return item.options
	}
}
