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

import UIKit
import CocoaLumberjack

class SongsViewController: MusicLibraryBaseViewController {
	var selectedSongs: [MusicLibrarySong]? = nil
	// MARK: Private Properties/Types
	private var songsInUse: [MusicLibrarySong]? = nil
	private var songsDelegate: SongsDataSourceDelegate?
	private var songsDataSource: MusicListDataSource?
	private var addToPlaylistCompletion: MusicListDataSource.OnAddToPlaylistCompletion?
	private var collectionViewHelper: MusicListDisplayHelper!
	private struct Segues {
		static let choosePlaylist = "choosePlaylist"
	}
}

// MARK: - Overridden Methods
extension SongsViewController {
	override func viewDidLoad() {
        assert(playerGroup != nil)
		super.viewDidLoad()
		
		self.delegate = self
		
		songsInUse = selectedSongs ?? LocalMusicLibrary.sharedInstance.allSongs()
		songsDelegate = SongsDataSourceDelegate(songs: songsInUse!)
		songsDataSource = MusicListDataSource(playerGroup: playerGroup!,
											 playerController: playerController,
											 delegate: songsDelegate!)
		songsDataSource!.onAddToPlaylistHandler = { [weak self] completion in
			guard let self = self else {
				return
			}
			
			self.addToPlaylistCompletion = completion
			self.performSegue(withIdentifier: Segues.choosePlaylist, sender: self)
		}
		
		collectionViewHelper = MusicListDisplayHelper(dataSources: [songsDataSource!],
													  viewController: self,
													  collectionView: collectionView!,
													  dragAllowed: false,
													  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)
														
		})
		
		title = NSLocalizedString("SONGS", comment: "Songs text")
	}
	
}

// MARK: - UIActions
extension SongsViewController {
	@IBAction func returnFromPlaylistChooser(segue: UIStoryboardSegue) {
		guard let chooserVC = segue.source as? PlaylistChooserTableViewController,
			let completion = addToPlaylistCompletion else {
				return
		}
		
		DDLogVerbose("Add to playlist: \(String (describing: chooserVC.chosenPlaylist?.name))")
		completion(chooserVC.chosenPlaylist)
	}
}

// MARK: - Private Methods
extension SongsViewController {
	override func handleLibraryInvalidation() {
		if selectedSongs == nil {
			songsInUse = LocalMusicLibrary.sharedInstance.allSongs()
			songsDelegate?.forceReload(songs: songsInUse!)
		}
	}

	private func didSelect(row: Int, item: MusicListDisplayHelperItem) {
		songsDataSource?.performOption(title: MusicLibraryOptions.playNow.toString(), at: row)
	}
}

// MARK: - MusicLibraryViewControllerDelegate Conformance
extension SongsViewController: MusicLibraryViewControllerDelegate {
	func allMediaItems() -> [IoTMediaItem] {
		let allMediaItems = songsInUse!.compactMap { LocalMusicLibrary.sharedInstance.mediaItemForSong($0) }
		return allMediaItems
	}
	
	func optionsForButton() -> [String] {
		let options = [MusicLibraryOptions.playNow.toString(),
					   MusicLibraryOptions.playNext.toString(),
					   MusicLibraryOptions.addToQueue.toString()]
		return options
	}
	
	func doOption(_ option: MusicLibraryOptions) -> Bool {
		return false
	}
}
