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

import UIKit
import CocoaLumberjack

class PlaylistSongsDataSourceDelegate {
	var onItemsInvalidated: (() -> ())?
	
	private let playlist: UserPlaylist
	init(playlist: UserPlaylist) {
		self.playlist = playlist
	}
}

// MARK: - Public Methods
extension PlaylistSongsDataSourceDelegate {
	func forceReload() {
		if let handler = onItemsInvalidated {
			handler()
		}
	}
}

// MARK: - MusicListDataSourceDelegate Conformance
extension PlaylistSongsDataSourceDelegate: MusicListDataSourceDelegate {
	var title: String {
		return NSLocalizedString("Playlist Songs", comment: "Playlist Songs Title")
	}

    func itemsInitialState(completion: @escaping ([MusicListDisplayHelperItem]) -> () , completionQueue: DispatchQueue) -> () {
        let count = playlist.items.count
        var array = [MusicListDisplayHelperItem]()
        array.reserveCapacity(count)
        for idx in 0..<count {
            let item = MediaItemMusicListDisplayHelperItem(mediaItem: playlist.items[idx],
                                                           accessory: .options,
                                                           options: [])
            array.append(item)
        }

        completionQueue.async {
            completion(array)
        }
    }

	func accessoryForItem(_ item: MusicListDisplayHelperItem, atIndex index: Int) -> MusicListDisplayHelperItemAccessory {
		return .options
	}
	
	func mediaItemForItem(_ item: MusicListDisplayHelperItem, atIndex index: Int) -> IoTMediaItem? {
		guard let mlItem = item as? MediaItemMusicListDisplayHelperItem else {
			return nil
		}
		return mlItem.mediaItem
	}
	
	func optionsForItem(_ item: MusicListDisplayHelperItem, atIndex index: Int) -> [String] {
		guard let mlItem = item as? MediaItemMusicListDisplayHelperItem else {
			return []
		}
		
		if mlItem.state == .failed {
			return [MusicLibraryItemOptions.removeFromPlaylist.toString()]
		} else {
			var options = [MusicLibraryItemOptions.playNow.toString(),
						   MusicLibraryItemOptions.playNext.toString(),
						   MusicLibraryItemOptions.addToQueue.toString()]
			
			
			if !FavoritesController.sharedInstance.isFavorite(item: mlItem.mediaItem) {
				options.append(MusicLibraryItemOptions.addToFavorites.toString())
			}
			
			options.append(MusicLibraryItemOptions.addToPlaylist.toString())
			options.append(MusicLibraryItemOptions.removeFromPlaylist.toString())
			return options
		}
	}
}

// MARK: - MusicListDataSourceUserPlaylistOperationsDelegate Conformance
extension PlaylistSongsDataSourceDelegate: MusicListDataSourceUserPlaylistOperationsDelegate {
	func removeItemFromUserPlaylist(atIndex index: Int,
									completion: @escaping (Bool, Error?) -> (),
									queue: DispatchQueue) {
		var pItems = playlist.items
		pItems.remove(at: index)
		playlist.items = pItems
		UserPlaylistsController.sharedInstance.updateUserPlaylist(playlist)
		queue.async {
			completion (true, nil)
		}
	}
}


