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

import UIKit
import CocoaLumberjack

class MediaServerItemsViewController: MusicLibraryBaseViewController {
	var mediaServer: UPnPMediaServer?
	var objectID: String?

	// MARK : Private Properties
	private var collectionViewHelper: MusicListDisplayHelper!
	private var mediaServerItemsDelegate: MediaServerItemsDataSourceDelegate?
	private var mediaServerItemsDataSource: MusicListDataSource?
	private var items: [UPnPMediaItem]?
}

// MARK : - Overridden Methods
extension MediaServerItemsViewController {
	override func viewDidLoad() {
		super.viewDidLoad()
		
		mediaServerItemsDelegate = MediaServerItemsDataSourceDelegate(items: [])
		mediaServerItemsDataSource = MusicListDataSource(playerGroup: playerGroup!,
														 playerController: playerController,
														 delegate: mediaServerItemsDelegate!)
		
		collectionViewHelper = MusicListDisplayHelper(dataSources: [mediaServerItemsDataSource!],
													  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)
														
		})
	}
	
	override func viewWillDisappear(_ animated: Bool) {
		appSpinnerPresenter()?.dismissWaitSpinner()
	}
	
	override func viewWillAppear(_ animated: Bool) {
		super.viewWillAppear(animated)
		if items == nil {
			appSpinnerPresenter()?.presentWaitSpinner()
			UPnPManager.sharedInstance.folderContents(mediaServer: mediaServer!,
													  objectID: objectID,
													  completion: { (newItems, error) in
														appSpinnerPresenter()?.dismissWaitSpinner()
														if newItems != nil {
															self.items = newItems!
															self.mediaServerItemsDelegate?.forceReload(items: self.items!)
														} else {
															DDLogError("Could not fetch items")
														}
			},
													  completionQueue: DispatchQueue.main)
		}
	}
	
	override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
		if let itemVC = segue.destination as? MediaServerItemsViewController,
			let server = sender as? UPnPMediaServer {
			itemVC.injectDependencies(preferences: preferences, playerController: playerController)
			itemVC.playerGroup = playerGroup
			itemVC.mediaServer = server
		}
	}
}

// MARK : - Private Methods
extension MediaServerItemsViewController {
	private func didSelect(row: Int, item: MusicListDisplayHelperItem) {
		if let msItem = item as? MediaServerItemsMusicListDisplayHelperItem {
			if msItem.item.isFolder {
				let newVC = MediaServerItemsViewController.instantiateFromStoryboard(name: "MediaViews")
				newVC.injectDependencies(preferences: preferences, playerController: playerController)
				newVC.playerGroup = playerGroup
				newVC.objectID = msItem.item.objectID
				newVC.mediaServer = self.mediaServer
				newVC.title = msItem.title.uppercased()
				self.navigationController?.pushViewController(newVC, animated: true)
			} else {
				mediaServerItemsDataSource?.performOption(title: MusicLibraryOptions.playNow.toString(), at: row)
			}
		}
	}
}
