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.PostRequest;
import com.unis.model.bean.HomeStatsBean;
import com.unis.model.bean.MultiDepotBean;
import com.xyxsbj.mylibrary.tool.SPTool;

import org.json.JSONException;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 包名: com.unis.model
 * 类名: DepotModel
 * 作用描述:
 * 创建人: mQ
 * 创建时间: 2020/12/14 9:53
 * 更新人:
 * 更新时间: 2020/12/14 9:53
 * 更新说明:
 * 版本号: 1.0
 */
public class DepotModel {
    private DepotModel() {
    }

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

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

    /**
     * 根据库id查询多级列表
     *
     * @param callBack
     */
    public void getMultiList(final DataCallback<List<MultiDepotBean>> callBack) {
        String depotId = UserModel.get().getUser().getDepotId();
        OkGo.<String>get(UrlPath.depot_multi_list)
                .params("depotId", depotId)
                .execute(new MyStringCallback() {
                    @Override
                    protected void onSuccessResult(Response<String> response) throws JSONException {
                        String dataJson = getDataJson(response.body());
                        Type type = new TypeToken<List<MultiDepotBean>>() {
                        }.getType();
                        List<MultiDepotBean> list = new Gson().fromJson(dataJson, type);
                        callBack.onSuccess(list);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        callBack.onError(getErrorMsg(response));
                    }
                });
    }


    private MultiDepotBean mSelectDepot;

    /**
     * 保存所选的库
     */
    public void saveSelectDepot(MultiDepotBean bean) {
        SPTool.saveString(Constant.KEY_SELECT_DEPOT, new Gson().toJson(bean));
        mSelectDepot = bean;
    }

    public void removeSelectDepot() {
        SPTool.remove(Constant.KEY_SELECT_DEPOT);
        mSelectDepot = null;
    }

    /**
     * 获取所选的库
     */
    public MultiDepotBean getSelectDepot() {
        // 从内存中取
        if (mSelectDepot != null)
            return mSelectDepot;
        // 从SP中取json
        String json = SPTool.getString(Constant.KEY_SELECT_DEPOT, null);
        if (json == null)
            return null;
        // json解析
        MultiDepotBean bean = new Gson().fromJson(json, MultiDepotBean.class);
        return bean;
    }

    /**
     * 获取存储状态统计
     */
    public void getStorageStatus(Integer depotId, final DataCallback<List<Map<String, Object>>> callback) {
        Map<String, Object> map = new HashMap<>();
        map.put("depotId", depotId);
        map.put("storageStatus", "s");
        PostRequest<String> request = OkGo.post(UrlPath.home_percent);
        request.upJson(new Gson().toJson(map));
        request.execute(new MyStringCallback() {
            @Override
            protected void onSuccessResult(Response<String> response) throws JSONException {
                String json = getDataJson(response);
                List<Map<String, Object>> list =
                        new Gson().fromJson(json, new TypeToken<List<Map<String, Object>>>() {
                        }.getType());
                callback.onSuccess(list);
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                callback.onError(getErrorMsg(response));
            }
        });
    }

    /**
     * 获取统计信息
     */
    public void getStatus(String depotId, final DataCallback<HomeStatsBean> callback) {
        Map<String, Object> map = new HashMap<>();
        map.put("depotId", Integer.parseInt(depotId));
        PostRequest<String> request = OkGo.post(UrlPath.home_percent);
        request.upJson(new Gson().toJson(map));
        request.execute(new MyStringCallback() {
            @Override
            protected void onSuccessResult(Response<String> response) throws JSONException {
                String json = getDataJson(response);
                HomeStatsBean bean = new Gson().fromJson(json, HomeStatsBean.class);
                callback.onSuccess(bean);
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                callback.onError(getErrorMsg(response));
            }
        });
    }

    /**
     * 获取仓房状态统计
     */
    public void getWarehouseStatus(Integer depotId, final DataCallback<List<Map<String, Object>>> callback) {
        Map<String, Object> map = new HashMap<>();
        map.put("depotId", depotId);
        map.put("warehouseStatus", "w");
        PostRequest<String> request = OkGo.post(UrlPath.home_percent);
        request.upJson(new Gson().toJson(map));
        request.execute(new MyStringCallback() {
            @Override
            protected void onSuccessResult(Response<String> response) throws JSONException {
                String json = getDataJson(response);
                List<Map<String, Object>> list =
                        new Gson().fromJson(json, new TypeToken<List<Map<String, Object>>>() {
                        }.getType());
                callback.onSuccess(list);
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                callback.onError(getErrorMsg(response));
            }
        });
    }

    /**
     * 获取粮食品种统计
     */
    public void getFoodTypeCount(Integer depotId, final DataCallback<List<Map<String, Object>>> callback) {
        Map<String, Object> map = new HashMap<>();
        map.put("depotId", depotId);
        map.put("grainVarieties", "g");
        PostRequest<String> request = OkGo.post(UrlPath.home_percent);
        request.upJson(new Gson().toJson(map));
        request.execute(new MyStringCallback() {
            @Override
            protected void onSuccessResult(Response<String> response) throws JSONException {
                String json = getDataJson(response);
                List<Map<String, Object>> list =
                        new Gson().fromJson(json, new TypeToken<List<Map<String, Object>>>() {
                        }.getType());
                callback.onSuccess(list);
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                callback.onError(getErrorMsg(response));
            }
        });
    }


}
