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

import UIKit

class PlaylistChooserTableViewCell: UITableViewCell {
	@IBOutlet weak var label: UILabel?
}

class PlaylistChooserTableViewController: ModalTableViewController {
	public var chosenPlaylist: UserPlaylist?
	
	// MARK: Private Properties/Types
	private struct Segues {
		static let dismiss = "dismiss"
	}
	
	private var playlists = UserPlaylistsController.sharedInstance.allUserPlaylists()
	private var playlistHelper: NewUserPlaylistHelper?
}

// MARK: - Overridden Methods
extension PlaylistChooserTableViewController {
    override func viewDidLoad() {
		title = NSLocalizedString("CHOOSE PLAYLIST", comment: "Choose playlist text")
        super.viewDidLoad()
		
		playlistHelper = NewUserPlaylistHelper(viewController: self,
											   mediaItemProvider: { (completion) in
												completion([])
		})
		
		let rightButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addPlaylist(_ :)))
		rightButton.accessibilityLabel = "Add"
		navigationItem.rightBarButtonItem = rightButton

		// Accessibility
		tableView?.accessibilityLabel = "Playlist List"
    }
	
	override func didTapModalClose(_ sender: AnyObject?) {
		chosenPlaylist = nil
		performSegue(withIdentifier: Segues.dismiss, sender: self) // Unwind
	}
}

// MARK: - UI Actions
extension PlaylistChooserTableViewController {
	@IBAction private func addPlaylist(_ sender: AnyObject?) {
		playlistHelper?.saveAsPlaylist(completion: { [weak self] playlistOpt in
			guard let playlist = playlistOpt,
				let self = self else {
				return
			}
			
			self.playlists = UserPlaylistsController.sharedInstance.allUserPlaylists()
			if let index = self.playlists.firstIndex(where: { $0.id == playlist.id }) {
				let rows = [ IndexPath(row: index, section: 0) ]
				self.tableView.insertRows(at: rows, with: .fade)
			} else {
				self.tableView.reloadData()
			}
		})
	}
}

// MARK: - Overridden TableView Methods
extension PlaylistChooserTableViewController {
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return playlists.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "PlaylistChooserTableViewCell", for: indexPath) as! PlaylistChooserTableViewCell

		let playlist = playlists[indexPath.row]
		cell.label?.text = playlist.name
		cell.accessibilityLabel = "Playlist " + playlist.name
        return cell
    }
	
	override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
		return CGFloat.leastNormalMagnitude // if we return zero it gets ignored
	}
	
	override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
		return CGFloat.leastNormalMagnitude // if we return zero it gets ignored
	}
	
	override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
		tableView.deselectRow(at: indexPath, animated: true)
		chosenPlaylist = playlists[indexPath.row]
		performSegue(withIdentifier: Segues.dismiss, sender: self) // Unwind
	}
}
