package com.secspace.lib.common.vinstall;

import com.secspace.lib.common.beans.WaitInstallAppInfo;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Created by bxy on 2018/7/9.
 */

public class VInstallHelper {
    private static final String TAG = "VInstallHelper";
    private static VInstallHelper instance;
    private CopyOnWriteArrayList<VInstallListener> listeners = new CopyOnWriteArrayList<>();
    private LinkedHashMap<String, WaitInstallAppInfo> mTasks = new LinkedHashMap<>();

    public static VInstallHelper getInstance() {
        if (instance == null) {
            synchronized (VInstallHelper.class) {
                if (instance == null) {
                    instance = new VInstallHelper();
                }
            }
        }
        return instance;
    }

    private VInstallHelper() {

    }

    public void setTasks(LinkedHashMap<String, WaitInstallAppInfo> tasks) {
        this.mTasks = tasks;
    }

    public void addTask(WaitInstallAppInfo app) {
        mTasks.put(app.getPackageName(), app);
    }

    public void removeTask(String packageName) {
        mTasks.remove(packageName);
    }

    public LinkedHashMap<String, WaitInstallAppInfo> getTasks() {
        return mTasks;
    }

    public void addListener(VInstallListener installListener) {
        if (listeners == null) {
            return;
        }
        if (listeners.contains(installListener)) {
            return;
        }
        listeners.add(installListener);
    }

    public void removeListener(VInstallListener installListener) {
        if (listeners == null) {
            return;
        }
        if (!listeners.contains(installListener)) {
            return;
        }
        listeners.remove(installListener);
    }

    public void clearListeners() {
        if (listeners == null) {
            return;
        }
        listeners.clear();
    }

    public void onVApkInstallSuccess(String packageName) {
        if (listeners == null) {
            return;
        }
        for (VInstallListener listener : listeners) {
            listener.onVApkInstallSuccess(packageName);
        }
    }

    public void onApkInstallSuccess(String packageName) {
        if (listeners == null) {
            return;
        }
        for (VInstallListener listener : listeners) {
            listener.onApkInstallSuccess(packageName);
        }
    }

    public void onApkInstallFailed(String packageName) {
        if (listeners == null) {
            return;
        }
        for (VInstallListener listener : listeners) {
            listener.onVApkInstallFailed(packageName);
        }
    }
}
