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

import UIKit
import CocoaLumberjack

class AlbumsViewController: MusicLibraryBaseViewController {
	var selectedAlbums: [MusicLibraryAlbum]?
	// MARK: Private Properties/Types
	private var albumsInUse: [MusicLibraryAlbum]?
	private var albumsDelegate: AlbumsDataSourceDelegate?
	private struct Segues {
		static let showSongs = "showSongs"
	}
	private var collectionViewHelper: MusicListDisplayHelper!
}

// MARK: - Overridden Methods
extension AlbumsViewController {
	override func viewDidLoad() {
        assert(playerGroup != nil)
		super.viewDidLoad()
		
		self.delegate = self
		
		albumsInUse = selectedAlbums ?? LocalMusicLibrary.sharedInstance.allAlbums()
		
		albumsDelegate = AlbumsDataSourceDelegate(albums: albumsInUse!)
		let dataSource = MusicListDataSource(playerGroup: playerGroup!,
											 playerController: playerController,
											 delegate: albumsDelegate!)
		
		collectionViewHelper = MusicListDisplayHelper(dataSources: [dataSource],
													  viewController: self,
													  collectionView: collectionView!,
													  dragAllowed: false,
													  countUpdater: { [weak self] numberOfItems in
														guard let self = self else {
															return
														}
														
														// The item we're given is the number of albums, not songs.
														var numberOfSongs = 0
														self.albumsInUse!.forEach {
															numberOfSongs = numberOfSongs + $0.numberOfSongs
														}
														
														self.updatePlayAllButtonText(count: numberOfSongs)
			},
													  onItemSelected: { [weak self] indexPath, item in
														guard let self = self else {
															return
														}
														self.didSelect(row: indexPath.row, item: item)
														
		})
		
		title = NSLocalizedString("ALBUMS", comment: "Albums text")
	}
	
	override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
		guard let songVC = segue.destination as? SongsViewController,
			let album = sender as? MusicLibraryAlbum else {
				return
		}
		
		let itemsForAlbum = LocalMusicLibrary.sharedInstance.songsForAlbum(album)
		songVC.selectedSongs = itemsForAlbum
		songVC.injectDependencies(preferences: preferences, playerController: playerController)
		songVC.playerGroup = playerGroup
	}
}

// MARK: - Private Methods
extension AlbumsViewController {
	override func handleLibraryInvalidation() {
		if selectedAlbums == nil {
			albumsInUse = LocalMusicLibrary.sharedInstance.allAlbums()
			albumsDelegate?.forceReload(albums: albumsInUse!)
		}
	}

	private func didSelect(row: Int, item: MusicListDisplayHelperItem) {
		performSegue(withIdentifier: Segues.showSongs, sender: albumsInUse![row])
	}
}

// MARK: - MusicLibraryViewControllerDelegate Conformance
extension AlbumsViewController: MusicLibraryViewControllerDelegate {
	func allMediaItems() -> [IoTMediaItem] {
		var allMediaItems = [IoTMediaItem]()
		albumsInUse!.forEach {
			let itemsForAlbum = LocalMusicLibrary.sharedInstance.songsForAlbum($0)
			let mediaItemsForAlbum = itemsForAlbum.compactMap { LocalMusicLibrary.sharedInstance.mediaItemForSong($0) }
			allMediaItems.append(contentsOf:mediaItemsForAlbum)
		}
		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
	}
}

