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.lzy.okgo.request.PostRequest;
import com.unis.model.bean.HouseBean;
import com.unis.model.bean.HouseListBean;
import com.xyxsbj.mylibrary.tool.LogTool;
import com.xyxsbj.mylibrary.tool.SPTool;

import org.json.JSONException;

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

/**
 * 包名: com.unis.model
 * 类名: UserModel
 * 作用描述:
 * 创建人: mQ
 * 创建时间: 2020/11/25 15:55
 * 更新人:
 * 更新时间: 2020/11/25 15:55
 * 更新说明:
 * 版本号: 1.0
 */
public class HouseModel {
    private HouseModel() {
    }

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

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

    Map<String, List<HouseListBean>> mListMap = new HashMap<>();

    /**
     * 获取仓房列表
     */
    public void getHouseList(final String depotId, final DataCallback<List<HouseListBean>> callBack) {
        LogTool.d("获取仓 库ID:" + depotId);
        List<HouseListBean> list = mListMap.get(depotId);
        if (list != null) {
            callBack.onSuccess(list);
            return;
        }
        OkGo.<String>get(UrlPath.house_list)
                .params("depotId", depotId)
                .execute(new MyStringCallback() {
                    @Override
                    protected void onSuccessResult(Response<String> response) throws JSONException {
                        String dataJson = getDataJson(response);
                        List<HouseListBean> houseList =
                                new Gson().fromJson(dataJson, new TypeToken<List<HouseListBean>>() {
                                }.getType());
                        mListMap.put(depotId, houseList);
                        callBack.onSuccess(houseList);
                    }

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

    /**
     * 存储选择的仓房
     */
    public void setSelectHouse(HouseListBean bean) {
        String houseJson = new Gson().toJson(bean);
        SPTool.saveString(Constant.KEY_SELECT_HOUSE, houseJson);
    }

    public void removeSelectHouse() {
        SPTool.remove(Constant.KEY_SELECT_HOUSE);
    }

    /**
     * 获取选择的仓房
     */
    public HouseListBean getSelectHouse() {
        String houseJson = SPTool.getString(Constant.KEY_SELECT_HOUSE, null);
        if (houseJson == null)
            return null;
        return new Gson().fromJson(houseJson, HouseListBean.class);
    }


    /**
     * 根据仓房id获取仓房信息
     */
    public void getHouseDetail(String id, final DataCallback<HouseBean> callback) {
        GetRequest<String> request =
                OkGo.get(UrlPath.house_detail + "/" + id);
        request.execute(new MyStringCallback() {
            @Override
            protected void onSuccessResult(Response<String> response) throws JSONException {
                String json = getDataJson(response);
                HouseBean bean = new Gson().fromJson(json, HouseBean.class);
                callback.onSuccess(bean);
            }

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

    /**
     * 修改仓房信息
     */
    public void update(HouseBean house, final DataCallback<String> callback) {
        PostRequest<String> request = OkGo.post(UrlPath.house_update);
        request.upJson(new Gson().toJson(house));
        request.execute(new MyStringCallback() {
            @Override
            protected void onSuccessResult(Response<String> response) throws JSONException {
                callback.onSuccess(null);
            }

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


}
