package com.sheep.gamegroup.view.adapter;

import android.content.Context;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.kfzs.appstore.utils.adapter.MDLAdpater;
import com.kfzs.appstore.utils.adapter.MDLViewHolderHelper;
import com.kfzs.duanduan.utils.ApkUtils;
import com.kfzs.duanduan.utils.InstallButtonUtils;
import com.kfzs.duanduan.view.KFProgressButton;
import com.sheep.gamegroup.greendao.download.DownLoadInfo;
import com.sheep.gamegroup.util.DownloadUtil;
import com.sheep.gamegroup.view.activity.ActDownloadMgr;
import com.sheep.jiuyan.samllsheep.R;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import static com.kfzs.duanduan.utils.ApkUtils.EQUALS;
import static com.kfzs.duanduan.utils.ApkUtils.LT;


/**
 * Download task Listview 's data adapter
 * Created by HooRang on 2017/4/10.
 */
public class DownloadMgrAdapter extends MDLAdpater<DownLoadInfo> {


    /**
     * List view item button tag
     */
    public static final String PUBLIC_TAG_PREFIX_BUTTON     = "PUBLIC_TAG_PREFIX_BUTTON";

    /**
     * List view item text view tag
     */
    public static final String PUBLIC_TAG_PREFIX_TEXTVIEW   = "PUBLIC_TAG_PREFIX_TEXTVIEW";

    /**
     * List view item progressbar tag
     */
    public static final String PUBLIC_TAG_PREFIX_PROGRESS   = "PUBLIC_TAG_PREFIX_PROGRESS";


    private Map<String, InstallButtonUtils> mInstallButtonMgrMap = new HashMap<>();


    public DownloadMgrAdapter(Context ctx){
        super(ctx, R.layout.listview_item_download_mgr);
    }


    @Override
    protected void fillData(MDLViewHolderHelper helper, int position, DownLoadInfo model) {

        String suffix = model.getMDownloadUrl();
        helper.getView(R.id.download_mgr_btn_act).setTag(PUBLIC_TAG_PREFIX_BUTTON       + suffix);
        helper.getView(R.id.download_mgr_pb_progress).setTag(PUBLIC_TAG_PREFIX_PROGRESS + suffix);
        helper.getView(R.id.download_mgr_tv_progress).setTag(PUBLIC_TAG_PREFIX_TEXTVIEW + suffix);

        helper.setText(R.id.download_mgr_tv_name,model.getMGameName());
        ProgressBar pb = helper.getView(R.id.download_mgr_pb_progress);

        pb.setProgress(model.getMPercent() == null ? 0  : model.getMPercent());
        helper.setText(R.id.download_mgr_tv_progress ,
                ActDownloadMgr.strFormat(mContext , model.getMDownloadedSize() , model.getMTotalSize()));

        helper.setItemChildClickListener(R.id.download_mgr_ib_delete);
        helper.setItemChildClickListener(R.id.download_mgr_btn_act);
        helper.setItemChildClickListener(R.id.download_mgr_iv_icon);

        Glide.with(mContext)
                .load(model.getMIconUrl())
                .apply(new RequestOptions().disallowHardwareConfig())
                .into((ImageView) helper.getView(R.id.download_mgr_iv_icon));


        KFProgressButton targetBtn = helper.getView(R.id.download_mgr_btn_act);


        InstallButtonUtils installButtonUtils;
        if (mInstallButtonMgrMap.containsKey(model.getMDownloadUrl())) {
            installButtonUtils = mInstallButtonMgrMap.get(model.getMDownloadUrl());
        } else {
            installButtonUtils = new InstallButtonUtils();
            installButtonUtils.setDownloadUrl(model.getMDownloadUrl());
            mInstallButtonMgrMap.put(model.getMDownloadUrl(), installButtonUtils);
        }
        installButtonUtils.addBtnInt(targetBtn,position);
        boolean isPkgInstalled = ApkUtils.getInstance().isPkgInstalled(model.getMPackageName());

        switch (model.getMStatus()) {
            case DownloadUtil.STATUS_INIT:
            case DownloadUtil.STATUS_DELETE:
                targetBtn.updateButtonUI(KFProgressButton.STATUS.STATUS_INIT, isPkgInstalled);
                break;
            case DownloadUtil.STATUS_ING:
                targetBtn.updateButtonUI(KFProgressButton.STATUS.STATUS_INSTALLING, isPkgInstalled);
                break;
            case DownloadUtil.STATUS_PAUSE:
                targetBtn.updateButtonUI(KFProgressButton.STATUS.STATUS_PAUSED, isPkgInstalled);
                break;
            case DownloadUtil.STATUS_FAIL:
                targetBtn.updateButtonUI(KFProgressButton.STATUS.STATUS_FAIL, isPkgInstalled);
                helper.setText(R.id.download_mgr_tv_progress, R.string.download_fail_please_retry);
                break;
            case DownloadUtil.STATUS_FINISH:
                pb.setProgress(100);
                if (isPkgInstalled) {
                    int apkStatus = ApkUtils.getInstance().comparePkgVersionCode(model.getMPackageName(), model.getMVersionCode());
                    switch (apkStatus){
                        case EQUALS:
                        case LT:
                        targetBtn.setText(R.string.open);
                        helper.setText(R.id.download_mgr_tv_progress, R.string.download_completed);
                        break;
                    default:
                        targetBtn.setText(R.string.update);
                        helper.setText(R.id.download_mgr_tv_progress, "");
                        break;
                    }
                } else {

                    if (new File(model.getMApkPath()+"").exists()) {
                        targetBtn.setText(R.string.install);
                        helper.setText(R.id.download_mgr_tv_progress, R.string.download_completed);

                    } else {
                        targetBtn.setText(R.string.install);
                        helper.setText(R.id.download_mgr_tv_progress, "");
                    }

                }
                break;
        }
    }

    public Map<String, InstallButtonUtils> getmInstallButtonMgrMap() {
        return mInstallButtonMgrMap;
    }
}
