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

import UIKit
import CocoaLumberjack

class PlaylistsMusicListDisplayHelperItem : MusicListDisplayHelperItem {
	public private(set) var userPlaylist: UserPlaylist
	
	init(playlist: UserPlaylist) {
		self.userPlaylist = playlist
		super.init()
		
		self.state = .available
		self.imageURL = playlist.items.first?.thumbnailURL
		self.title = playlist.name
		self.subtitle = String.localizedStringWithFormat("%d Songs", playlist.items.count)
		self.accessory = .none
		self.options = []
	}
}

// MARK: -

class PlaylistsDataSourceDelegate {
	private(set) var playlists: [UserPlaylist]
	var onItemsInvalidated: (() -> ())?
	
	var title: String { return NSLocalizedString("Playlists", comment: "Playlists Title") }
	
	init(playlists: [UserPlaylist]) {
		self.playlists = playlists
	}
}

// MARK: - Public Methods
extension PlaylistsDataSourceDelegate {
	func forceReload(playlists newPlaylists: [UserPlaylist]) {
		playlists = newPlaylists
		if let handler = onItemsInvalidated {
			handler()
		}
	}
}

// MARK: - MusicListDataSourceDelegate Conformance
extension PlaylistsDataSourceDelegate: MusicListDataSourceDelegate {
    func itemsInitialState(completion: @escaping ([MusicListDisplayHelperItem]) -> () , completionQueue: DispatchQueue) -> () {
        let count = playlists.count
        var array = [MusicListDisplayHelperItem]()
        array.reserveCapacity(count)
        for i in 0..<count {
            array.append(PlaylistsMusicListDisplayHelperItem(playlist: playlists[i]))
        }

        completionQueue.async {
            completion(array)
        }
    }
	
	func accessoryForItem(_ item: MusicListDisplayHelperItem, atIndex index: Int) -> MusicListDisplayHelperItemAccessory {
		return .none
	}
	
	func mediaItemForItem(_ item: MusicListDisplayHelperItem, atIndex index: Int) -> IoTMediaItem? {
		return nil
	}
	
	func optionsForItem(_ item: MusicListDisplayHelperItem, atIndex index: Int) -> [String] {
		return []
	}
}
