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

import UIKit

class FavoritesSongsViewController: MusicLibraryBaseViewController {
	private struct Segues {
		static let dismiss = "dismiss"
		static let choosePlaylist = "choosePlaylist"
	}
	
	private var collectionViewHelper: MusicListDisplayHelper!
	private var favoritesDelegate: FavoritesSongsDataSourceDelegate?
	private var favoritesDataSource: MusicListDataSource?
	private var addToPlaylistCompletion: MusicListDataSource.OnAddToPlaylistCompletion?
}

// MARK: - Overridden Methods
extension FavoritesSongsViewController {
	override func viewDidLoad() {
        assert(playerGroup != nil)
		super.viewDidLoad()
		
		self.delegate = self
		setUpHelpers()
		title = NSLocalizedString("FAVORITES", comment: "Favorites title")
	}
}

// MARK: - UI Actions
extension FavoritesSongsViewController {
	@IBAction private func returnFromPlaylistChooser(segue: UIStoryboardSegue) {
		guard let chooserVC = segue.source as? PlaylistChooserTableViewController,
			let completion = addToPlaylistCompletion else {
				return
		}
		
		completion(chooserVC.chosenPlaylist)
	}
}

// MARK: - MusicLibraryViewControllerDelegate Conformance
extension FavoritesSongsViewController: MusicLibraryViewControllerDelegate {
	func allMediaItems() -> [IoTMediaItem] {
		return FavoritesController.sharedInstance.allFavorites
	}
	
	func optionsForButton() -> [String] {
		let options = [MusicLibraryOptions.playNow.toString(),
					   MusicLibraryOptions.playNext.toString(),
					   MusicLibraryOptions.addToQueue.toString(),
					   MusicLibraryOptions.clearFavorites.toString()]
		return options
	}
	
	func doOption(_ option: MusicLibraryOptions) -> Bool {
		if option == .clearFavorites {
			showDestructiveAlert(message: NSLocalizedString("Are you sure you want to clear your favorites list?\n\nNote: Songs will NOT be deleted from your device.", comment: "Alert message"),
								 destructiveOption: NSLocalizedString("Clear Favorites", comment: "Alert option"),
								 destructiveAction: {
									FavoritesController.sharedInstance.removeAll()
									self.favoritesDelegate!.forceReload()
									
			})
			return true
		} else {
			return false
		}
	}
}

// MARK: - Private Methods
extension FavoritesSongsViewController {
	private func setUpHelpers() {
		favoritesDelegate = FavoritesSongsDataSourceDelegate()
		favoritesDataSource = MusicListDataSource(playerGroup: playerGroup!,
												  playerController: playerController,
												  delegate: favoritesDelegate!)
		favoritesDataSource?.favoritesDelegate = favoritesDelegate
		favoritesDataSource!.onAddToPlaylistHandler = { [weak self] completion in
			guard let self = self else {
				return
			}
			
			self.addToPlaylistCompletion = completion
			self.performSegue(withIdentifier: Segues.choosePlaylist, sender: self)
		}
		
		collectionViewHelper = MusicListDisplayHelper(dataSources: [favoritesDataSource!],
													  viewController: self,
													  collectionView: collectionView!,
													  dragAllowed: true,
													  countUpdater: { [weak self] numberOfItems in
														guard let self = self else {
															return
														}
														
														self.updatePlayAllButtonText(count: numberOfItems)
			},
													  onItemSelected: { [weak self] indexPath, item in
														guard let self = self else {
															return
														}
														self.didSelect(row: indexPath.row, item: item)
														
		})
	}

	private func didSelect(row: Int, item: MusicListDisplayHelperItem) {
	}
}



