package com.gh.ndownload

import com.gh.gamecenter.core.runOnUiThread
import com.lightgame.download.DownloadEntity
import com.lightgame.download.DownloadTask
import java.util.*

/**
 *  下载进度变化通知
 */
object NDataChanger : Observable() {

    val downloadingTasks: MutableMap<String, DownloadTask> = Collections.synchronizedMap(HashMap())//当前正在下载的任务队列
    val downloadEntries: MutableMap<String, DownloadEntity> = Collections.synchronizedMap(HashMap())//包含所有下载任务的任务队列

    @Synchronized
    fun notifyDataChanged(entry: DownloadEntity?) {
        runOnUiThread {
            if (entry != null) {
                if (downloadEntries[entry.url] != null) {
                    downloadEntries[entry.url] = entry
                }
                setChanged()
                notifyObservers(entry)
            }
        }
    }

    @Synchronized
    fun pauseDownloadingTask(url: String) {
        runOnUiThread {
            val downloadTask = downloadingTasks.remove(url)
            downloadTask?.pause()
        }
    }

}
