package com.police.policedatasystem.http;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.police.policedatasystem.apply.model.ApplyDept;
import com.police.policedatasystem.apply.model.ApplyPerson;
import com.police.policedatasystem.apply.model.ApplyType;
import com.police.policedatasystem.apply.model.DetailQueryResult;
import com.police.policedatasystem.data.model.DataCount;
import com.police.policedatasystem.data.model.KeyPerson;
import com.police.policedatasystem.data.model.KeyPersonDetail;
import com.police.policedatasystem.data.model.PoliceEmergency;
import com.police.policedatasystem.data.model.PoliceEmergencyDetail;
import com.police.policedatasystem.http.requestparams.GetKeyPersonDetailParams;
import com.police.policedatasystem.http.requestparams.GetKeyPersonListParams;
import com.police.policedatasystem.http.requestparams.GetKeyPersonMessageCountParams;
import com.police.policedatasystem.http.requestparams.GetOrgKeyPersonListParams;
import com.police.policedatasystem.http.requestparams.GetPoliceEmergencyDetailParams;
import com.police.policedatasystem.http.requestparams.GetPoliceEmergencyListParams;
import com.police.policedatasystem.mine.model.Apply;
import com.police.policedatasystem.mine.model.ApplyDetail;
import com.police.policedatasystem.util.Constants;

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

import okhttp3.Call;

public class RequestClient extends BaseRequestClient {

    private RequestClient() {
    }

    private static final class RequestClientHolder {
        static final RequestClient requestClient = new RequestClient();
    }

    public static RequestClient instance() {
        return RequestClientHolder.requestClient;
    }

    /**
     * 获取重点人
     *
     * @param params   参数对象
     * @param callback 回调函数
     */
    public void getKeyPerson(GetKeyPersonListParams params, CustomCallBack<List<KeyPerson>> callback) {
        params.setJybh(Constants.USER_ID);
        params.setSfhm(Constants.SFZH);
        params.setPageSize(Constants.PAGE_SIZE + "");
        request(Constants.KEY_PERSON_ID, new Gson().toJson(params), new CustomCallBack<String>() {
            @Override
            public void onError(Call call, @NonNull Exception e) {
                callback.onError(call, e);
            }

            @Override
            public void onSuccess(String value) {
                List<KeyPerson> keyPersonList = new Gson().fromJson(value, new TypeToken<List<KeyPerson>>() {
                }.getType());
                callback.onSuccess(keyPersonList);
            }
        });
    }

    /**
     * 获取组织重点人数据
     *
     * @param params 搜索关键字姓名
     */
    public void getOrgKeyPerson(GetOrgKeyPersonListParams params, CustomCallBack<List<KeyPerson>> callback) {
        params.setSfhm(Constants.SFZH);
        params.setDept(Constants.USER_ORG_ID);
        params.setPageSize(Constants.PAGE_SIZE + "");
        request(Constants.ORG_KEY_PERSON_ID, new Gson().toJson(params), new CustomCallBack<String>() {
            @Override
            public void onError(Call call, @NonNull Exception e) {
                callback.onError(call, e);
            }

            @Override
            public void onSuccess(String value) {
                List<KeyPerson> keyPersonList = new Gson().fromJson(value, new TypeToken<List<KeyPerson>>() {
                }.getType());
                callback.onSuccess(keyPersonList);
            }
        });
    }


    /**
     * 获取重点人详情数据
     *
     * @param str 身份证号
     */
    public void keyPersonDetail(String str, CustomCallBack<KeyPersonDetail> callBack) {
        GetKeyPersonDetailParams params = new GetKeyPersonDetailParams();
        params.setSfzh(str);//      params.setSfzh("220104197307276911");
        params.setSfhm(Constants.SFZH);
        request(Constants.KEY_PERSON_DETAIL_ID, new Gson().toJson(params), new CustomCallBack<String>() {
            @Override
            public void onError(Call call, @NonNull Exception e) {
                callBack.onError(call, e);
            }

            @Override
            public void onSuccess(String value) {
                KeyPersonDetail keyPersonList = new Gson().fromJson(value, new TypeToken<KeyPersonDetail>() {
                }.getType());
                callBack.onSuccess(keyPersonList);
            }
        });
    }

    /**
     * 获取警情列表数据
     *
     * @param params   参数
     * @param callBack 回调函数
     */
    public void getPoliceEmergency(GetPoliceEmergencyListParams params, CustomCallBack<List<PoliceEmergency>> callBack) {
        params.setSfhm(Constants.SFZH);
        params.setPageSize(Constants.PAGE_SIZE + "");
        request(Constants.POLICE_EMERGENCY_ID, new Gson().toJson(params), new CustomCallBack<String>() {
            @Override
            public void onError(Call call, @NonNull Exception e) {
                callBack.onError(call, e);
            }

            @Override
            public void onSuccess(String value) {
                List<PoliceEmergency> keyPersonList = new Gson().fromJson(value, new TypeToken<List<PoliceEmergency>>() {
                }.getType());
                callBack.onSuccess(keyPersonList);
            }
        });
    }


    /**
     * 获取重点人详情数据
     *
     * @param str 身份证号
     */
    public void getPoliceEmergencyDetail(String str, CustomCallBack<PoliceEmergencyDetail> callBack) {
        GetPoliceEmergencyDetailParams params = new GetPoliceEmergencyDetailParams();
        params.setJcjbh(str);//      params.setSfzh("220104197307276911");
        params.setSfhm(Constants.SFZH);
        request(Constants.POLICE_EMERGENCY_DETAIL_ID, new Gson().toJson(params), new CustomCallBack<String>() {
            @Override
            public void onError(Call call, @NonNull Exception e) {
                callBack.onError(call, e);
            }

            @Override
            public void onSuccess(String value) {
                PoliceEmergencyDetail keyPersonDetail = new Gson().fromJson(value, PoliceEmergencyDetail.class);
                callBack.onSuccess(keyPersonDetail);
            }
        });
    }

    /**
     * 获取重点人数据统计
     */
    public void getKeyPersonMessageCount(CustomCallBack<DataCount> callBack) {
        GetKeyPersonMessageCountParams params = new GetKeyPersonMessageCountParams();
        params.setSfhm(Constants.SFZH);
        params.setJybh(Constants.USER_ID);
        params.setDept(Constants.USER_ORG_ID);
//        params.setSfhm("220183197210264653");
//        params.setJybh("106222");
//        params.setDept("220183210000");
        request(Constants.KEY_PERSON_MESSAGE_COUNT_ID, new Gson().toJson(params), new CustomCallBack<String>() {
            @Override
            public void onError(Call call, @NonNull Exception e) {
                callBack.onError(call, e);
            }

            @Override
            public void onSuccess(String value) {
                DataCount dataCount = new Gson().fromJson(value, DataCount.class);
                callBack.onSuccess(dataCount);
            }
        });
    }

    /**
     * 获取申请支援类型
     */
    public void getApplyType(CustomCallBack<List<ApplyType>> callBack) {
        HashMap<String, String> params = new HashMap<>();
        params.put("type", "1");
        params.put("deptId", Constants.USER_ORG_ID);
        params.put("sfhm", Constants.SFZH);
        request(Constants.APPLY_TYPE_ID, new Gson().toJson(params), new CustomCallBack<String>() {
            @Override
            public void onError(Call call, @NonNull Exception e) {
                callBack.onError(call, e);
            }

            @Override
            public void onSuccess(String value) {
                List<ApplyType> applyTypes = new Gson().fromJson(value, new TypeToken<List<ApplyType>>() {
                }.getType());
                callBack.onSuccess(applyTypes);
            }
        });
    }

    /**
     * 获取申请部门
     */
    public void getApplyDept(CustomCallBack<List<ApplyDept>> callBack) {
        HashMap<String, String> params = new HashMap<>();
        params.put("type", "2");
        params.put("deptId", Constants.USER_ORG_ID);
        params.put("sfhm", Constants.SFZH);
        request(Constants.APPLY_TYPE_ID, new Gson().toJson(params), new CustomCallBack<String>() {
            @Override
            public void onError(Call call, @NonNull Exception e) {
                callBack.onError(call, e);
            }

            @Override
            public void onSuccess(String value) {
                List<ApplyDept> applyTypes = new Gson().fromJson(value, new TypeToken<List<ApplyDept>>() {
                }.getType());
                callBack.onSuccess(applyTypes);
            }
        });
    }

    /**
     * 获取申请支援审批人
     */
    public void getApplyPerson(String deptId, CustomCallBack<List<ApplyPerson>> callBack) {
        HashMap<String, String> params = new HashMap<>();
        params.put("type", "3");
        params.put("deptId", deptId);
        params.put("sfhm", Constants.SFZH);
        request(Constants.APPLY_TYPE_ID, new Gson().toJson(params), new CustomCallBack<String>() {
            @Override
            public void onError(Call call, @NonNull Exception e) {
                callBack.onError(call, e);
            }

            @Override
            public void onSuccess(String value) {
                List<ApplyPerson> applyTypes = new Gson().fromJson(value, new TypeToken<List<ApplyPerson>>() {
                }.getType());
                callBack.onSuccess(applyTypes);
            }
        });
    }

    /**
     * 获取申请支援-消息来源
     */
    public void getApplyXXLY(CustomCallBack<List<ApplyType>> callBack) {
        HashMap<String, String> params = new HashMap<>();
        params.put("type", "4");
        params.put("deptId", Constants.USER_ORG_ID);
        params.put("sfhm", Constants.SFZH);
        request(Constants.APPLY_TYPE_ID, new Gson().toJson(params), new CustomCallBack<String>() {
            @Override
            public void onError(Call call, @NonNull Exception e) {
                callBack.onError(call, e);
            }

            @Override
            public void onSuccess(String value) {
                List<ApplyType> applyTypes = new Gson().fromJson(value, new TypeToken<List<ApplyType>>() {
                }.getType());
                callBack.onSuccess(applyTypes);
            }
        });
    }

    /**
     * 获取申请支援-警务类型
     */
    public void getApplyJWLX(CustomCallBack<List<ApplyType>> callBack) {
        HashMap<String, String> params = new HashMap<>();
        params.put("type", "5");
        params.put("deptId", Constants.USER_ORG_ID);
        params.put("sfhm", Constants.SFZH);
        request(Constants.APPLY_TYPE_ID, new Gson().toJson(params), new CustomCallBack<String>() {
            @Override
            public void onError(Call call, @NonNull Exception e) {
                callBack.onError(call, e);
            }

            @Override
            public void onSuccess(String value) {
                List<ApplyType> applyTypes = new Gson().fromJson(value, new TypeToken<List<ApplyType>>() {
                }.getType());
                callBack.onSuccess(applyTypes);
            }
        });
    }

    /**
     * 获取我的赋能-办理状态
     */
    public void getMineBLZT(CustomCallBack<List<ApplyType>> callBack) {
        HashMap<String, String> params = new HashMap<>();
        params.put("type", "6");
        params.put("deptId", Constants.USER_ORG_ID);
        params.put("sfhm", Constants.SFZH);
        request(Constants.APPLY_TYPE_ID, new Gson().toJson(params), new CustomCallBack<String>() {
            @Override
            public void onError(Call call, @NonNull Exception e) {
                callBack.onError(call, e);
            }

            @Override
            public void onSuccess(String value) {
                List<ApplyType> applyTypes = new Gson().fromJson(value, new TypeToken<List<ApplyType>>() {
                }.getType());
                callBack.onSuccess(applyTypes);
            }
        });
    }


    /**
     * 获取我的赋能-信息来源
     */
    public void getMineXXLY(CustomCallBack<List<ApplyType>> callBack) {
        HashMap<String, String> params = new HashMap<>();
        params.put("type", "7");
        params.put("deptId", Constants.USER_ORG_ID);
        params.put("sfhm", Constants.SFZH);
        request(Constants.APPLY_TYPE_ID, new Gson().toJson(params), new CustomCallBack<String>() {
            @Override
            public void onError(Call call, @NonNull Exception e) {
                callBack.onError(call, e);
            }

            @Override
            public void onSuccess(String value) {
                List<ApplyType> applyTypes = new Gson().fromJson(value, new TypeToken<List<ApplyType>>() {
                }.getType());
                callBack.onSuccess(applyTypes);
            }
        });
    }

    /**
     * 申请赋能-提交申请
     */
    public void addApply(HashMap<String, String> params, CustomCallBack<Object> callBack) {
        params.put("sfhm", Constants.SFZH);
        request(Constants.APPLY_COMMIT_ID, new Gson().toJson(params), new CustomCallBack<String>() {
            @Override
            public void onError(Call call, @NonNull Exception e) {
                callBack.onError(call, e);
            }

            @Override
            public void onSuccess(String value) {
                callBack.onSuccess(null);
            }
        });
    }

    /**
     * 我的赋能-查询我申请的赋能{
     * "sfhm": "220104199208240015",
     * "sqsj": "",
     * "sy": "",
     * "ly": "",
     * "spzt": "",
     * "fnbh": ""
     * }
     */
    public void getApplyList(HashMap<String, String> params, CustomCallBack<List<Apply>> callBack) {
        params.put("sfhm", Constants.SFZH);
        request(Constants.MINE_LIST_ID, new Gson().toJson(params), new CustomCallBack<String>() {
            @Override
            public void onError(Call call, @NonNull Exception e) {
                callBack.onError(call, e);
            }

            @Override
            public void onSuccess(String value) {
                List<Apply> applyTypes = new Gson().fromJson(value, new TypeToken<List<Apply>>() {
                }.getType());
                callBack.onSuccess(applyTypes);
            }
        });
    }

    /**
     * 我的赋能详情
     */
    public void getApplyDetail(String fnbh, CustomCallBack<ApplyDetail> callBack) {
        HashMap<String, String> params = new HashMap<>();
        params.put("sfhm", Constants.SFZH);
        params.put("fnbh", fnbh);
        params.put("deptId", Constants.USER_ORG_ID);
        request(Constants.MINE_DETAIL_ID, new Gson().toJson(params), new CustomCallBack<String>() {
            @Override
            public void onError(Call call, @NonNull Exception e) {
                callBack.onError(call, e);
            }

            @Override
            public void onSuccess(String value) {
                ApplyDetail applyDetail = new Gson().fromJson(value, ApplyDetail.class);
                callBack.onSuccess(applyDetail);
            }
        });
    }

    /**
     * 警情详情跳转到我的赋能详情
     */
    public void jumpApplyDetail(String jqbh, CustomCallBack<List<Apply>> callBack) {
        HashMap<String, String> params = new HashMap<>();
        params.put("sfhm", Constants.SFZH);
        params.put("sfzh", Constants.SFZH);
        params.put("jqbh", jqbh);
        params.put("deptId", Constants.USER_ORG_ID);
        request(Constants.MINE_DETAIL_ID, new Gson().toJson(params), new CustomCallBack<String>() {
            @Override
            public void onError(Call call, @NonNull Exception e) {
                callBack.onError(call, e);
            }

            @Override
            public void onSuccess(String value) {
                List<Apply> applyTypes = new Gson().fromJson(value, new TypeToken<List<Apply>>() {
                }.getType());
                callBack.onSuccess(applyTypes);
            }
        });
    }

    /**
     * 我的赋能详情
     */
    public void policeEmergencyDetailQuery(int type, String content, CustomCallBack<DetailQueryResult> callBack) {
        HashMap<String, String> params = new HashMap<>();
        params.put("sfhm", Constants.SFZH);
        params.put("type", type + "");
        params.put("condi", content);
        request(Constants.SELF_QUERY_ID, new Gson().toJson(params), new CustomCallBack<String>() {
            @Override
            public void onError(Call call, @NonNull Exception e) {
                callBack.onError(call, e);
            }

            @Override
            public void onSuccess(String value) {
                DetailQueryResult applyDetail = new Gson().fromJson(value, DetailQueryResult.class);
                callBack.onSuccess(applyDetail);
            }
        });
    }
}
