package com.kfzs.duanduan.event;

import android.content.Context;

import com.kfzs.duanduan.bean.DownloadStatus;
import com.kfzs.duanduan.services.DownloadTaskService;
import com.kfzs.duanduan.datashare.provider.download.DownLoadInfo;
import com.kfzs.duanduan.utils.dlg.HelperUtils;

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 DownloadTaskService downloadTaskService;
    private String mTag;

    private static OnDownCallback onDownCallback;

    public static EventDownloadHandler newInstance(Context context, OnDownCallback callback) {
        mContext = context;
        eventDownloadHandler = new EventDownloadHandler();
        downloadTaskService = new DownloadTaskService(mContext);
        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 DownloadTaskService.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 DownloadTaskService.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 DownloadTaskService.STATUS_FAIL:
//                downloadTaskService.setDownloadTaskStatus(event.getDownloadUrl(),
//                        DownloadTaskService.STATUS_FAIL);
                onDownCallback.downloadFail(event.getDownloadUrl());
                Double totalSize = event.getFileTotalSize();
                if (totalSize > HelperUtils.getFreeSpace()) {
                    DownLoadInfo downloadTask = downloadTaskService.getDownloadTaskByUrl(event.getDownloadUrl());
                    EventBus.getDefault().post(BigEvent.get().setEventTypes(EventTypes.STORAGE_LOW)
                            .setData(downloadTask.getMGameName()));
                }
                break;
            case DownloadTaskService.STATUS_PAUSE:
//                downloadTaskService.setDownloadTaskStatus(event.getDownloadUrl(),
//                        DownloadTaskService.STATUS_PAUSE);
                onDownCallback.downloadPause(event.getDownloadUrl());
                break;
            case DownloadTaskService.STATUS_DELETE:
//                downloadTaskService.setDownloadTaskStatus(event.getDownloadUrl(),
//                        DownloadTaskService.STATUS_DELETE);
                onDownCallback.downloadDelete(event.getDownloadUrl());
                break;
            case DownloadTaskService.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);

    }

}
