package com.unis.model;

import android.graphics.Color;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.GetRequest;
import com.unis.model.bean.EnumBean;
import com.xyxsbj.mylibrary.tool.SPTool;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 包名: com.unis.model
 * 类名: BasicModel
 * 作用描述: 基础信息Model
 * 创建人: mQ
 * 创建时间: 2020/11/27 15:17
 * 更新人:
 * 更新时间: 2020/11/27 15:17
 * 更新说明:
 * 版本号: 1.0
 */
public class BasicModel {

    private BasicModel() {
    }

    private static class SingleHolder {
        private static final BasicModel INSTANCE = new BasicModel();
    }

    public static BasicModel get() {
        return SingleHolder.INSTANCE;
    }


    private Map<String, String> depotMap;

    /**
     * 获取库字典
     */
    public void getDepotDic() {
        final GetRequest<String> request = OkGo.get(UrlPath.depot_dictionary);
        request.execute(new MyStringCallback() {
            @Override
            protected void onSuccessResult(Response<String> response) throws JSONException {
                String json = getDataJson(response.body());
                depotMap = new Gson().fromJson(json, new TypeToken<Map<String, String>>() {
                }.getType());
                SPTool.saveString(Constant.KEY_DEPOT_MAP, json);
            }

            @Override
            public void onError(Response<String> response) {
            }
        });
    }

    /**
     * 根据id获取库名
     */
    public String getDepotName(String id) {
        if (depotMap == null) {
            String json = SPTool.getString(Constant.KEY_DEPOT_MAP, "");
            depotMap = new Gson().fromJson(json, new TypeToken<Map<String, String>>() {
            }.getType());
        }
        if (depotMap != null)
            return depotMap.get(id);
        return "";
    }


    private Map<String, String> houseMap;

    /**
     * 获取仓房字典
     */
    public void getHouseDic() {
        final GetRequest<String> request = OkGo.get(UrlPath.house_dictionary);
        request.execute(new MyStringCallback() {
            @Override
            protected void onSuccessResult(Response<String> response) throws JSONException {
                String json = getDataJson(response.body());
                houseMap = new Gson().fromJson(json, new TypeToken<Map<String, String>>() {
                }.getType());
                SPTool.saveString(Constant.KEY_HOUSE_MAP, json);
            }

            @Override
            public void onError(Response<String> response) {
            }
        });
    }

    /**
     * 根据id获取仓房名称
     */
    public String getHouseName(String id) {
        if (houseMap == null) {
            String json = SPTool.getString(Constant.KEY_HOUSE_MAP, "");
            houseMap = new Gson().fromJson(json, new TypeToken<Map<String, String>>() {
            }.getType());
        }
        if (houseMap != null)
            return houseMap.get(id);
        return "";
    }

    private Map<String, String> departmentMap;

    /**
     * 获取部门字典
     */
    public void getDepartmentDic() {
        final GetRequest<String> request = OkGo.get(UrlPath.department_dictionary);
        request.execute(new MyStringCallback() {
            @Override
            protected void onSuccessResult(Response<String> response) throws JSONException {
                String json = getDataJson(response.body());
                departmentMap = new Gson().fromJson(json, new TypeToken<Map<String, String>>() {
                }.getType());
                SPTool.saveString(Constant.KEY_DEPARTMENT_MAP, json);
            }

            @Override
            public void onError(Response<String> response) {
            }
        });
    }

    /**
     * 根据id获取部门名称
     */
    public String getDepartmentName(String id) {
        if (departmentMap == null) {
            String json = SPTool.getString(Constant.KEY_DEPARTMENT_MAP, "");
            departmentMap = new Gson().fromJson(json, new TypeToken<Map<String, String>>() {
            }.getType());
        }
        if (departmentMap != null)
            return departmentMap.get(id);
        return "";
    }

    private Map<String, String> personMap;

    /**
     * 获取人员字典
     */
    public void getPersonDic() {
//        String depotId = UserModel.get().getUser().getDepotId();
        GetRequest<String> request = OkGo.get(UrlPath.person_dictionary);
//        request.params("depotId", depotId);
        request.execute(new MyStringCallback() {
            @Override
            protected void onSuccessResult(Response<String> response) throws JSONException {
                String json = getDataJson(response);
                Gson gson = new Gson();
                personMap = gson.fromJson(json,
                        new TypeToken<Map<String, String>>() {
                        }.getType());
                SPTool.saveString(Constant.KEY_PERSON_MAP, json);
            }

            @Override
            public void onError(Response<String> response) {
            }
        });
    }

    /**
     * 根据人员id 获取人员名称
     */
    public String getPersonName(String id) {
        if (personMap == null) {
            String json = SPTool.getString(Constant.KEY_PERSON_MAP, "");
            personMap = new Gson().fromJson(json, new TypeToken<Map<String, String>>() {
            }.getType());
        }
        if (personMap != null)
            return personMap.get(id);
        else
            return "";
    }


    private Map<String, List<EnumBean>> enumList;
    private Map<String, String> enumAll;

    /**
     * 获取枚举字典
     */
    public void getEnumDic() {

        GetRequest<String> request = OkGo.get(UrlPath.enum_all);
        request.execute(new MyStringCallback() {
            @Override
            protected void onSuccessResult(Response<String> response) throws JSONException {
                Gson gson = new Gson();
                String json = getDataJson(response);

                JSONObject object = new JSONObject(json);
                JSONObject parentMap = object.optJSONObject("parentMap");
                if (parentMap != null) {
                    enumList = gson.fromJson(parentMap.toString(), new TypeToken<Map<String, List<EnumBean>>>() {
                    }.getType());
                    SPTool.saveString(Constant.KEY_ENUM_LIST, parentMap.toString());
                }

                JSONObject enumMap = object.optJSONObject("enumMap");
                if (enumMap != null) {
                    enumAll = gson.fromJson(enumMap.toString(), new TypeToken<Map<String, String>>() {
                    }.getType());
                    SPTool.saveString(Constant.KEY_ENUM_ALL, enumMap.toString());
                }

            }

            @Override
            public void onError(Response<String> response) {
            }
        });
    }

    /**
     * 根据id查询字典名称
     *
     * @param id
     */
    public String getEnumName(String id) {
        if (enumAll == null) {
            String json = SPTool.getString(Constant.KEY_ENUM_ALL, "");
            enumAll = new Gson().fromJson(json, new TypeToken<Map<String, String>>() {
            }.getType());
        }
        if (enumAll != null)
            return enumAll.get(id);
        else
            return "";
    }

    /**
     * 根据id查询字典列表
     *
     * @param id
     */
    public List<EnumBean> getEnumList(String id) {
        if (enumList == null) {
            String json = SPTool.getString(Constant.KEY_ENUM_LIST, "");
            enumList = new Gson().fromJson(json, new TypeToken<Map<String, List<EnumBean>>>() {
            }.getType());
        }
        if (enumList != null)
            return enumList.get(id);
        else
            return new ArrayList<>();
    }

    /**
     * 仓房状态列表
     */
    public void getHouseStatusList(DataCallback<List<EnumBean>> callBack) {
//        String[] status = new String[]{"空仓", "半仓", "满仓"};
//        List<DictBean> list = new ArrayList<>();
//        for (int i = 0; i < status.length; i++) {
//            list.add(new DictBean(i, status[i]));
//        }

        List<EnumBean> enumList = getEnumList("3");
        callBack.onSuccess(enumList);
    }

    /**
     * 储存状态列表
     */
    public void getStorageList(DataCallback<List<EnumBean>> callBack) {
//        String[] status = new String[]{"出库", "入库", "通风", "熏蒸", "内环流控温"};
//        List<DictBean> list = new ArrayList<>();
//        for (int i = 0; i < status.length; i++) {
//            list.add(new DictBean(i, status[i]));
//        }
        List<EnumBean> enumList = getEnumList("4");
        callBack.onSuccess(enumList);
    }

    /**
     * 粮食品种列表
     */
    public void getFoodTypeList(DataCallback<List<EnumBean>> callBack) {
//        String[] status = new String[]{"小麦", "玉米", "豆油"};
//        List<DictBean> list = new ArrayList<>();
//        for (int i = 0; i < status.length; i++) {
//            list.add(new DictBean(i, status[i]));
//        }
        List<EnumBean> enumList = getEnumList("2");
        callBack.onSuccess(enumList);
    }


    /**
     * 获取记录类型名称 如 tf=机械通风
     */
    public String getWorkTypeName(String type) {
        switch (type) {
            case "tf":
                return "机械通风申请";
        }
        return type;
    }

    /**
     * 待办类型
     */
    Map<String, String> todoTypeMap = new HashMap<String, String>() {{
        put("code0", "保管员每周粮情检查");
        put("code1", "安全员每天安全检查");
        put("code2", "仓储科长每2周本库检查");
        put("code3", "业务科长每2周分库检查");
        put("code4", "监管科长每2周租仓、委托点检查");
        put("code5", "分管副总每月粮情检查");
        put("code6", "分管副总每月质量检查");
        put("code7", "分管副总每月分库检查");
        put("code8", "总经理每季检查粮情");
        put("code9", "安全员每天租仓管理/委托管理");
        put("code10", "保管员每天租仓管理/委托管理");
    }};

    public String getTodoType(String code) {
        return todoTypeMap.get(code);
    }


    /**
     * 饼图的色值与名称对应表
     */
    private Map<String, String> mColorMap;

    public Integer getColor(String name) {
        if (mColorMap == null) {
            mColorMap = new HashMap<String, String>() {{
                // 仓房状态
                put("熏蒸", "#FAE704");
                put("内环流控温", "#B00B9D");
                put("静态储存", "#0CFE87");
                put("出库", "#FFA900");
                put("入库", "#4D09FF");
                put("通风", "#1CE0E7");

                // 存储状态
                put("满仓", "#0B8A47");
                put("空仓", "#95D374");
                put("半仓", "#FFF53B");
            }};
        }
        String color = mColorMap.get(name);
        if (color == null)
            color = "#0A7E41";
        return Color.parseColor(color);
    }
}
