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

import UIKit
import CocoaLumberjack

class FavoritesSongsDataSourceDelegate {
	var onItemsInvalidated: (() -> ())?
}

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

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

    func itemsInitialState(completion: @escaping ([MusicListDisplayHelperItem]) -> () , completionQueue: DispatchQueue) -> () {
        let favorites = FavoritesController.sharedInstance.allFavorites
        let count = favorites.count
        var array = [MediaItemMusicListDisplayHelperItem]()
        array.reserveCapacity(count)
        for idx in 0..<count {
            let item = MediaItemMusicListDisplayHelperItem(mediaItem: favorites[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] {
		if item.state == .failed {
			return [MusicLibraryItemOptions.removeFromFavorites.toString()]
		} else {
			var options = [MusicLibraryItemOptions.playNow.toString(),
						   MusicLibraryItemOptions.playNext.toString(),
						   MusicLibraryItemOptions.addToQueue.toString()]
			
			options.append(MusicLibraryItemOptions.addToPlaylist.toString())
			options.append(MusicLibraryItemOptions.removeFromFavorites.toString())
			return options
		}
	}
}

// MARK: - MusicListDataSourceFavoritesOperationsDelegate Conformance
extension FavoritesSongsDataSourceDelegate: MusicListDataSourceFavoritesOperationsDelegate {
	func removeItemFromFavorites(atIndex index: Int,
								 completion: @escaping (Bool, Error?) -> (),
								 queue: DispatchQueue) {
		let favorites = FavoritesController.sharedInstance.allFavorites
		let item = favorites[index]
		FavoritesController.sharedInstance.removeFavorite(item: item)
		queue.async {
			completion (true, nil)
		}
	}
	
	func moveItem(from: Int,
				  to: Int,
				  completion: @escaping (Bool, Error?) -> (),
				  queue: DispatchQueue) {
		do {
			try FavoritesController.sharedInstance.moveFavorite(from: from, to: to)
			queue.async {
				completion (true, nil)
			}
		} catch let error {
			queue.async {
				completion (false, error)
			}
		}
	}
}

