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

import UIKit

class StoreHelper {
	let syncQueue = OperationQueue()
	private (set) var info : Dictionary<String, Any>? = nil
	let id: Int
    let urlOpener: URL

	init (id: Int,
          urlOpener: URL) {
		self.id = id
        self.urlOpener = urlOpener
		syncQueue.maxConcurrentOperationCount = 1 // Serial Queue
		let fetchOp = StoreFetchOperation(id: id) { (result) in
			self.info = result
		}
		syncQueue.addOperation(fetchOp)
	}

	func appImage(completion: @escaping (UIImage?) -> ()) {
		let op = StoreIconOperation(helper: self,
									completion: completion)
		syncQueue.addOperation(op)
	}

    func appURL(completion: @escaping (URL?) -> ()) {
        if UIApplication.shared.canOpenURL(urlOpener) {
            completion(urlOpener)
        } else {
            let op = StoreURLOperation(helper: self,
                                       completion: completion)
            syncQueue.addOperation(op)
        }
    }

	func appTitle(completion: @escaping (String?) -> ()) {
		let op = StoreTitleOperation(helper: self,
								   completion: completion)
		syncQueue.addOperation(op)
	}
}
