package com.kfzs.duanduan.utils;

import com.kfzs.duanduan.view.KFProgressButton;
import com.sheep.jiuyan.samllsheep.R;

import java.util.HashMap;
import java.util.Set;

/**
 * @ Created by Dlg
 * @ <p>TiTle:  InstallButtonUtils</p>
 * @ <p>Description: 安装按钮的集合处理类，集中处理，就是同URL的下载按钮集合</p>
 * @ date:  2017/6/16 14:44
 * @ QQ:    315096953
 */
public class InstallButtonUtils {

    private int downloadedPgrs;
    private String position;
    private String downloadUrl;
    private String packageName;
    private KFProgressButton.STATUS status;
    private HashMap<String, KFProgressButton> btns = new HashMap<>();
    public static final int mIntTag = R.id.view_tag_down_btn;


    /**
     * @param position 这个值暂时是没有用的，以前的遗留物
     * @param url
     * @param view
     * @param tag      相同URL的按钮，tag来区分不同按钮，在listview里一般用position，防止重复加载很多无用btn
     * @return
     */
    public static InstallButtonUtils init(String position, String url, String packageName, KFProgressButton view, String tag) {
        return init(position, url, packageName).addBtn(view, tag);
    }

    private static InstallButtonUtils init(String position, String url, String packageName) {
        InstallButtonUtils logicMgr = new InstallButtonUtils();
        logicMgr.setStatus(KFProgressButton.STATUS.STATUS_INIT);
        logicMgr.setPosition(position);
        logicMgr.setDownloadUrl(url);
        logicMgr.setPackageName(packageName);
        return logicMgr;
    }


    /**
     * @param view
     * @param tag  要设置为不同的值，否则tag和url都一样时，就会先移除以前的btn
     *             不然在listview里会生成很多不同的button
     * @return
     */
    public InstallButtonUtils addBtn(KFProgressButton view, String tag) {
        btns.remove(tag);
        btns.put(tag, view);
        return this;
    }

    public InstallButtonUtils addBtnInt(KFProgressButton view, int tag) {
        addBtn(view, tag + "");
        return this;
    }

    public String getPosition() {
        return position;
    }

    public void setPosition(String position) {
        this.position = position;
    }

    public String getDownloadUrl() {
        return downloadUrl;
    }

    public void setDownloadUrl(String downloadUrl) {
        this.downloadUrl = downloadUrl;
    }

    public String getPackageName() {
        return packageName;
    }

    public void setPackageName(String packageName) {
        this.packageName = packageName;
    }

    public KFProgressButton.STATUS getStatus() {
        return status;
    }

    public void setStatus(KFProgressButton.STATUS status) {
        boolean isPkgInstalled = ApkUtils.getInstance().isPkgInstalled(packageName);
        this.status = status;
        Set<String> sets = btns.keySet();
        for (String set : sets) {
            if (downloadUrl.equals(btns.get(set).getTag(mIntTag))) {
                btns.get(set).updateButtonUI(status, isPkgInstalled);
            }
            //else 如果已经被别的复用，暂时不用删除吧。
        }
    }

    public int getDownloadedPgrs() {
        return downloadedPgrs;
    }

    public void setDownloadedPgrs(int downloadedPgrs) {
        this.downloadedPgrs = downloadedPgrs;
        Set<String> sets = btns.keySet();
        for (String set : sets) {
            if (downloadUrl.equals(btns.get(set).getTag(mIntTag))) {
                btns.get(set).updateProgress(downloadedPgrs);
            }
        }
    }
}
