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

import UIKit
import CocoaLumberjack

class QueueOperationsHelper {
	static let sharedInstance = QueueOperationsHelper()

    // MARK: Private Properties
    private let replyQueue = DispatchQueue(label: "com.qualcomm.qti.QueueOperationsHelper.replyQueue")
	private init() {}
}

// MARK: - Public Methods
extension QueueOperationsHelper {

    func allItems(playlist: IoTPlaylist,
                  completion: (([IoTMediaItem]?, Error?)->())?,
                  completionQueue: DispatchQueue?) {

        let count = playlist.count
        if count == 0 {
            if completionQueue != nil && completion != nil {
                completionQueue!.async {
                    completion!([], nil)
                }
            }
            return
        }
        playlist.getItemsIn(NSMakeRange(0, Int(count)),
                            completion: { (success, items, error) in
                                DDLogDebug("Got reply of \(items.count) items")
                                if completionQueue != nil && completion != nil {
                                    completionQueue!.async {
                                        if (success) {
                                            completion!(items, nil)
                                        } else {
                                            completion!(nil, error)
                                        }

                                    }
                                }
        }, completionQueue: replyQueue)
    }

    func remove(itemAtIndex index: Int,
                fromPlaylist playlist: IoTPlaylist,
                completion: ((Bool, Error?)->())?,
                completionQueue: DispatchQueue?) {
        playlist.removeMediaItems(in: NSMakeRange(index, 1),
                                  completion: completion,
                                  completionQueue: completionQueue)

    }

    func move(itemAtIndex from: Int,
              toIndex to: Int,
              playlist: IoTPlaylist,
              completion: ((Bool, Error?)->())?,
              completionQueue: DispatchQueue?) {
        let dest = to > from ? to+1 : to // The device copies before it deletes.
        playlist.moveMediaItems(in: NSMakeRange(from, 1),
                                position: Int32(dest),
                                completion: completion,
                                completionQueue: completionQueue)
    }

	func add(toPlaylist playlist: IoTPlaylist,
			 items: [IoTMediaItem],
             atEnd: Bool,
             playNow: Bool,
             completion: ((Bool, Error?)->())?,
             completionQueue: DispatchQueue?) {

        var insertionPoint = atEnd ? playlist.count : playlist.indexPlaying + 1;

        if insertionPoint < 0 {
            insertionPoint = 0
        }
        
        playlist.add(items,
                     at: insertionPoint,
                     play: playNow,
                     userData: "",
                     completion: completion,
                     completionQueue: completionQueue)
    }

	func add(toPlaylist playlist: IoTPlaylist,
			 item: IoTMediaItem,
			 atEnd: Bool,
			 playNow: Bool,
			 completion: ((Bool, Error?)->())?,
			 completionQueue: DispatchQueue?) {
		add(toPlaylist: playlist,
			items: [item],
			atEnd: atEnd,
			playNow: playNow,
			completion: completion,
			completionQueue: completionQueue)
	}
}
