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

import UIKit
import CocoaLumberjack

class ArtistsViewController: MusicLibraryBaseViewController {
	var selectedArtists: [MusicLibraryArtist]?
	// MARK: Private Properties/Types
	private var artistsInUse: [MusicLibraryArtist]?
	private var artistsDelegate: ArtistsDataSourceDelegate?
	private struct Segues {
		static let showAlbums = "showAlbums"
	}
	private var collectionViewHelper: MusicListDisplayHelper!
}

// MARK: - Overridden Methods
extension ArtistsViewController {
	override func viewDidLoad() {
        assert(playerGroup != nil)
		super.viewDidLoad()
		
		self.delegate = self
		
		artistsInUse = selectedArtists ?? LocalMusicLibrary.sharedInstance.allArtists()
		
		artistsDelegate = ArtistsDataSourceDelegate(artists: artistsInUse!)
		let dataSource = MusicListDataSource(playerGroup: playerGroup!,
											 playerController: playerController,
											 delegate: artistsDelegate!)
		
		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.artistsInUse!.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("ARTISTS", comment: "Artists text")
	}
	
	override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
		guard let albumVC = segue.destination as? AlbumsViewController,
			let artist = sender as? MusicLibraryArtist else {
				return
		}
		
		let albumsForArtist = LocalMusicLibrary.sharedInstance.albumsForArtist(artist)
		albumVC.selectedAlbums = albumsForArtist
		albumVC.injectDependencies(preferences: preferences, playerController: playerController)
		albumVC.playerGroup = playerGroup
	}
}
	
// MARK: - MusicLibraryViewControllerDelegate Conformance
extension ArtistsViewController: MusicLibraryViewControllerDelegate {
	func allMediaItems() -> [IoTMediaItem] {
		var allMediaItems = [IoTMediaItem]()
		artistsInUse!.forEach { artist in
			let albumsForArtist = LocalMusicLibrary.sharedInstance.albumsForArtist(artist)
			albumsForArtist.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 ArtistsViewController {
	override func handleLibraryInvalidation() {
		if selectedArtists == nil {
			artistsInUse = LocalMusicLibrary.sharedInstance.allArtists()
			artistsDelegate?.forceReload(artists: artistsInUse!)
		}
	}

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


