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

import UIKit
import CocoaLumberjack

class GenresViewController: MusicLibraryBaseViewController, MusicLibraryViewControllerDelegate {
	var selectedGenres: [MusicLibraryGenre]?
	// MARK: Private Properties/Types
	private var genresInUse: [MusicLibraryGenre]?
	private var genresDelegate: GenresDataSourceDelegate?
	private struct Segues {
		static let showAlbums = "showAlbums"
	}
	private var collectionViewHelper: MusicListDisplayHelper!
}

// MARK: - Overridden Methods
extension GenresViewController {
	override func viewDidLoad() {
        assert(playerGroup != nil)
		super.viewDidLoad()
		
		self.delegate = self
		
		genresInUse = selectedGenres ?? LocalMusicLibrary.sharedInstance.allGenres()
		
		genresDelegate = GenresDataSourceDelegate(genres: genresInUse!)
		let dataSource = MusicListDataSource(playerGroup: playerGroup!,
											 playerController: playerController,
											 delegate: genresDelegate!)
		
		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.genresInUse!.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("GENRES", comment: "Genres text")
	}
	
	override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
		guard let albumVC = segue.destination as? AlbumsViewController,
			let genre = sender as? MusicLibraryGenre else {
				return
		}
		
		let albumsForGenre = LocalMusicLibrary.sharedInstance.albumsForGenre(genre)
		albumVC.selectedAlbums = albumsForGenre
		albumVC.injectDependencies(preferences: preferences, playerController: playerController)
		albumVC.playerGroup = playerGroup
	}
}
	
// MARK: - MusicLibraryViewControllerDelegate Conformance
extension GenresViewController {
	func allMediaItems() -> [IoTMediaItem] {
		var allMediaItems = [IoTMediaItem]()
		genresInUse!.forEach { genre in
			let albumsForGenre = LocalMusicLibrary.sharedInstance.albumsForGenre(genre)
			albumsForGenre.forEach { album in
				let itemsForAlbum = LocalMusicLibrary.sharedInstance.songsForAlbum(album)
				let mediaItemsForAlbum = itemsForAlbum.compactMap { song in
					LocalMusicLibrary.sharedInstance.mediaItemForSong(song)
				}
				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
	}
}

// MARK: - Private Methods
extension GenresViewController {
	override func handleLibraryInvalidation() {
		if selectedGenres == nil {
			genresInUse = LocalMusicLibrary.sharedInstance.allGenres()
			genresDelegate?.forceReload(genres: genresInUse!)
		}
	}

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



