package com.kfzs.duanduan.utils.dlg;

import android.content.Context;
import android.util.Log;

import com.kfzs.duanduan.datashare.provider.download.DownLoadInfo;
import com.kfzs.duanduan.event.EventDownloadHandler;
import com.kfzs.duanduan.proto.GameOuterClass;
import com.kfzs.duanduan.services.DownloadTaskService;
import com.kfzs.duanduan.utils.ApkUtils;
import com.kfzs.duanduan.utils.GameStatusScanner;
import com.kfzs.duanduan.utils.InstallButtonUtils;
import com.kfzs.duanduan.view.KFProgressButton;

import java.util.Map;

/**
 * @ Created by Dlg
 * @ <p>TiTle:  DownBtnUtils</p>
 * @ <p>Description:    下载按钮的各种通用状态监听类</p>
 * @ date:  2017/6/16 14:37
 * @ QQ:    315096953
 */

public class DownBtnUtils {
    private static final String TAG = DownBtnUtils.class.getName();

    /**
     * 安装按钮的标准状态监听
     *
     * @param installButtonMgrMap
     * @return
     */
    public static EventDownloadHandler.OnDownCallback
    installBtnStatus(final Map<String, InstallButtonUtils> installButtonMgrMap) {
        return new EventDownloadHandler.OnDownCallback() {
            @Override
            public void downloading(String downloadUrl, Integer progress) {
                if (installButtonMgrMap.get(downloadUrl) != null) {
                    installButtonMgrMap.get(downloadUrl).setStatus(KFProgressButton.STATUS.STATUS_PROGRESSING);
                }
            }

            @Override
            public void downloadFinish(String downloadUrl) {
                if (installButtonMgrMap.get(downloadUrl) != null) {
                    installButtonMgrMap.get(downloadUrl).setStatus(KFProgressButton.STATUS.STATUS_FINISH);
                }
            }

            @Override
            public void downloadPause(String downloadUrl) {
                if (installButtonMgrMap.get(downloadUrl) != null) {
                    installButtonMgrMap.get(downloadUrl).setStatus(KFProgressButton.STATUS.STATUS_PAUSED);
                }
            }

            @Override
            public void downloadFail(String downloadUrl) {
                if (installButtonMgrMap.get(downloadUrl) != null) {
                    installButtonMgrMap.get(downloadUrl).setStatus(KFProgressButton.STATUS.STATUS_FAIL);
                }
            }

            @Override
            public void downloadDelete(String downloadUrl) {
                if (installButtonMgrMap.get(downloadUrl) != null) {
                    installButtonMgrMap.get(downloadUrl).setStatus(KFProgressButton.STATUS.STATUS_DELETE);
                }
            }

            @Override
            public void downloadInit(String downloadUrl) {
                if (installButtonMgrMap.get(downloadUrl) != null) {
                    installButtonMgrMap.get(downloadUrl).setStatus(KFProgressButton.STATUS.STATUS_INIT);
                }
            }
        };
    }


    /**
     * 加下载按钮点击事件的监听
     * 再三警告，一定要统一在这里加，这里会设置Tag防止复用错位
     *
     * @param context
     * @param btn              KFProgressButton
     * @param installButtonMgr
     * @param model
     * @param tag              可以随便写，就是记录哪里的按钮而已
     */
    private static void initBtnListener(Context context, KFProgressButton btn,
                                        final Map<String, InstallButtonUtils> installButtonMgr,
                                        GameOuterClass.Game model, String tag) {
        GameStatusScanner mScanner = new GameStatusScanner(context, btn, tag) {
            @Override
            public void onButtonClickReturnListener(String downloadUrl, KFProgressButton.STATUS status) {
                if (installButtonMgr != null && installButtonMgr.containsKey(downloadUrl)) {
                    installButtonMgr.get(downloadUrl).setStatus(status);
                }
            }
        };
        btn.setTag(InstallButtonUtils.mIntTag, model.getDownloadUrl());//加Tag，防止复用出问题
        mScanner.compare(model);
        mScanner.addListener();
    }


    /**
     * 添加下载按钮的监听
     *
     * @param target           KFProgressButton
     * @param context
     * @param installButtonMgr
     * @param model            Game_modle
     * @param tag              一般记录是哪个页面的下载按钮，可以任意，最好是不要重复
     */
    public static void addDownloadBtnListener(KFProgressButton target,
                                              Context context,
                                              Map<String, InstallButtonUtils> installButtonMgr,
                                              GameOuterClass.Game model, String tag) {
        if (!model.getDownloadUrl().toLowerCase().startsWith("http")) {
            Log.e(TAG, "推荐页游戏下载地址错误：" + model.getGameName() + "#" + model.getDownloadUrl());
            return;
        }
        boolean isPkgInstalled = ApkUtils.getInstance().isPkgInstalled(model.getPackageName());
        if (installButtonMgr.containsKey(model.getDownloadUrl())) {
            installButtonMgr.get(model.getDownloadUrl()).addBtn(target, tag);
            target.updateButtonUI(installButtonMgr.get(model.getDownloadUrl()).getStatus(), isPkgInstalled);
        } else {
            KFProgressButton.STATUS status = KFProgressButton.STATUS.STATUS_INIT;
            DownLoadInfo downloadInfo = new DownloadTaskService(context).getDownloadTaskByUrl(model.getDownloadUrl());
            if(downloadInfo != null && downloadInfo.getMStatus() != null){
                switch (downloadInfo.getMStatus()){
                    case DownloadTaskService.STATUS_ING:
                        status = KFProgressButton.STATUS.STATUS_PROGRESSING;
                        break;
                    case DownloadTaskService.STATUS_PAUSE:
                        status = KFProgressButton.STATUS.STATUS_PAUSED;
                        break;
                    case DownloadTaskService.STATUS_FAIL:
                        status = KFProgressButton.STATUS.STATUS_FAIL;
                        break;
                    case DownloadTaskService.STATUS_FINISH:
                        status = KFProgressButton.STATUS.STATUS_FINISH;
                        break;
                    case DownloadTaskService.STATUS_DELETE:
                        status = KFProgressButton.STATUS.STATUS_DELETE;
                        break;
                    case DownloadTaskService.STATUS_INIT:
                    default:
                        status = KFProgressButton.STATUS.STATUS_INIT;
                        break;
                }
            }
            installButtonMgr.put(model.getDownloadUrl(), InstallButtonUtils
                    .init(String.valueOf(tag), model.getDownloadUrl(), model.getPackageName(), target, tag));
            target.updateButtonUI(status, isPkgInstalled);
        }
        DownBtnUtils.initBtnListener(context, target, installButtonMgr, model, TAG);
    }
}
