package com.sheep.gamegroup.event;

import android.content.Context;

import com.sheep.gamegroup.greendao.download.DownLoadInfo;
import com.sheep.gamegroup.model.entity.DownloadStatus;
import com.sheep.gamegroup.util.DownloadUtil;
import com.sheep.jiuyan.samllsheep.utils.G;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by HooRang on 2017/3/6.
 */
public class EventDownloadHandler {
    private static final String TAG = EventDownloadHandler.class.getName();

    private static EventDownloadHandler eventDownloadHandler;
    private static Context mContext;
    private static DownloadUtil downloadTaskService;
    private String mTag;

    private static OnDownCallback onDownCallback;

    public static EventDownloadHandler newInstance(Context context, OnDownCallback callback) {
        mContext = context;
        eventDownloadHandler = new EventDownloadHandler();
        downloadTaskService = new DownloadUtil();
        onDownCallback = callback;
        return eventDownloadHandler;
    }

    public String getmTag() {
        return mTag;
    }

    public EventDownloadHandler setmTag(String mTag) {
        this.mTag = mTag;
        return this;
    }

    /**
     * 下载的状态监听
     *
     * @param event
     */
    public void handlDownloadResult(DownloadStatus event) {
        switch (event.getStatus()) {
            case DownloadUtil.STATUS_ING:
//                downloadTaskService.setDownloadTaskPercent(event.getDownloadPgrs(), event.getFileDownloadedSize(),
//                        event.getFileTotalSize(), event.getDownloadId(), event.getDownloadUrl());
//                downloadTaskService.setDownloadTaskStatus(event.getDownloadUrl(), DownloadTaskService.STATUS_ING);
                onDownCallback.downloading(event.getDownloadUrl(), event.getDownloadPgrs());
                break;
            case DownloadUtil.STATUS_FINISH:
//                downloadTaskService.setDownloadTaskFinish(event.getDownloadUrl(), event.getApkPath());
//                downloadTaskService.setDownloadTaskStatus(event.getDownloadUrl(), DownloadTaskService.STATUS_FINISH);
                onDownCallback.downloadFinish(event.getDownloadUrl());
//                if (new File(event.getApkPath()).exists()) {
//                    ApkUtils.installApk(mContext, event.getApkPath());
//                }
                break;
            case DownloadUtil.STATUS_FAIL:
//                downloadTaskService.setDownloadTaskStatus(event.getDownloadUrl(),
//                        DownloadTaskService.STATUS_FAIL);
                onDownCallback.downloadFail(event.getDownloadUrl());
                Double totalSize = event.getFileTotalSize();
                if (totalSize > G.getFreeSpace()) {
                    DownLoadInfo downloadTask = downloadTaskService.getDownloadTaskByUrl(event.getDownloadUrl());
                    EventBus.getDefault().post(BigEvent.get().setEventTypes(EventTypes.STORAGE_LOW)
                            .setData(downloadTask.getMGameName()));
                }
                break;
            case DownloadUtil.STATUS_PAUSE:
//                downloadTaskService.setDownloadTaskStatus(event.getDownloadUrl(),
//                        DownloadTaskService.STATUS_PAUSE);
                onDownCallback.downloadPause(event.getDownloadUrl());
                break;
            case DownloadUtil.STATUS_DELETE:
//                downloadTaskService.setDownloadTaskStatus(event.getDownloadUrl(),
//                        DownloadTaskService.STATUS_DELETE);
                onDownCallback.downloadDelete(event.getDownloadUrl());
                break;
            case DownloadUtil.STATUS_INIT:
            default:
                onDownCallback.downloadInit(event.getDownloadUrl());
                break;
        }
    }


    public interface OnDownCallback {
        void downloading(String downloadUrl, Integer progress);

        void downloadFinish(String downloadUrl);

        void downloadPause(String downloadUrl);

        void downloadFail(String downloadUrl);

        void downloadDelete(String downloadUrl);

        void downloadInit(String downloadUrl);

    }

}
