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

class PlaylistsViewController: MusicLibraryBaseViewController {
	var collectionViewHelper: MusicListDisplayHelper!
	var playlistsDelegate: PlaylistsDataSourceDelegate?
	var playlists: [UserPlaylist]?
	
	// MARK: Private Properties/Types
	struct Segues {
		static let showPlaylistSongs = "showPlaylistSongs"
	}
}

// MARK: - Overridden Methods
extension PlaylistsViewController {
	override func viewDidLoad() {
		super.viewDidLoad()
		
		title = NSLocalizedString("PLAYLISTS", comment: "Playlists text")
	}
	
	override func viewWillAppear(_ animated: Bool) {
		// Do here as when returning with pop we don't get a better notification.
		super.viewWillAppear(animated)
		
        playlists = UserPlaylistsController.sharedInstance.allUserPlaylists()
        if let pg = playerGroup,
            playlistsDelegate == nil {
            playlistsDelegate = PlaylistsDataSourceDelegate(playlists: playlists!)
            let dataSource = MusicListDataSource(playerGroup: pg,
                                                 playerController: playerController,
                                                 delegate: playlistsDelegate!)

            collectionViewHelper = MusicListDisplayHelper(dataSources: [dataSource],
                                                          viewController: self,
                                                          collectionView: collectionView!,
                                                          dragAllowed: false,
                                                          countUpdater: nil,
                                                          onItemSelected: { [weak self] indexPath, item in
                                                            guard let self = self else {
                                                                return
                                                            }
                                                            self.didSelect(row: indexPath.row, item: item)

            })
        } else {
            playlistsDelegate!.forceReload(playlists: playlists!)
        }
    }

	override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
		guard let playlistSongsVC = segue.destination as? PlaylistSongsViewController,
			let playlist = sender as? UserPlaylist else {
				return
		}
		
		playlistSongsVC.playlist = playlist
		playlistSongsVC.injectDependencies(preferences: preferences, playerController: playerController)
		playlistSongsVC.playerGroup = playerGroup
	}
}

// MARK: - UI Actions
extension PlaylistsViewController {
	@IBAction private func returnFromPlaylistSongs(segue: UIStoryboardSegue) {
		//Do not remove
	}
}

// MARK: - Private Methods
extension PlaylistsViewController {
	private func didSelect(row: Int, item: MusicListDisplayHelperItem) {
		performSegue(withIdentifier: Segues.showPlaylistSongs, sender: playlists![row])
	}
}

