package com.unis.model;

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.DictBean;
import com.xyxsbj.mylibrary.tool.SPTool;

import org.json.JSONException;

import java.util.ArrayList;
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;
    private Map<String, String> houseMap;
    private Map<String, String> departmentMap;
    private Map<String, String> personMap;

    /**
     * 获取库字典
     */
    public void getDepotDictionary() {
        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);
            }
        });
    }

    /**
     * 根据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());
        }
        return depotMap.get(id);
    }

    /**
     * 获取仓房字典
     */
    public void getHouseDictionary() {
        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);
            }
        });
    }

    /**
     * 根据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());
        }
        return houseMap.get(id);
    }

    /**
     * 获取部门字典
     */
    public void getDepartmentDictionary() {
        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);
            }
        });
    }

    /**
     * 根据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());
        }
        return departmentMap.get(id);
    }


    /**
     * 获取人员字典
     */
    public void getPersonDictionary() {
//        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) {
                super.onError(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());
        }
        return personMap.get(id);
    }

    /**
     * 仓房状态列表
     */
    public void getHouseStatusList(DataCallback<List<DictBean>> callBack) {
        // TODO: 2020/11/27 仓房状态列表模拟数据
        String[] status = new String[]{"空仓", "半仓", "满仓"};
        List<DictBean> list = new ArrayList<>();
        for (int i = 0; i < status.length; i++) {
            list.add(new DictBean(i, status[i]));
        }
        callBack.onSuccess(list);
    }

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

    /**
     * 粮食品种列表
     */
    public void getFoodTypeList(DataCallback<List<DictBean>> callBack) {
        // TODO: 2020/11/27 粮食品种列表模拟数据
        String[] status = new String[]{"小麦", "玉米", "豆油"};
        List<DictBean> list = new ArrayList<>();
        for (int i = 0; i < status.length; i++) {
            list.add(new DictBean(i, status[i]));
        }
        callBack.onSuccess(list);
    }


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