package com.police.policedatasystem.viewmodel;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.police.policedatasystem.data.activity.KeyPersonDetailActivity;
import com.police.policedatasystem.data.activity.PoliceEmergencyDetailActivity;
import com.police.policedatasystem.fragment.DataFragment;
import com.police.policedatasystem.http.CustomCallBack;
import com.police.policedatasystem.http.requestparams.GetKeyPersonListParams;
import com.police.policedatasystem.http.requestparams.GetOrgKeyPersonListParams;
import com.police.policedatasystem.http.requestparams.GetPoliceEmergencyListParams;
import com.police.policedatasystem.indexActivity;
import com.police.policedatasystem.model.DataCount;
import com.police.policedatasystem.model.KeyPerson;
import com.police.policedatasystem.model.KeyPersonDetail;
import com.police.policedatasystem.model.PoliceEmergency;
import com.police.policedatasystem.model.PoliceEmergencyDetail;
import com.police.policedatasystem.util.Constants;
import com.police.policedatasystem.util.UiUtils;

import java.util.List;

import okhttp3.Call;

public class DataViewModel {
    private final DataFragment fragment;
    public int pageNum = 1;
    public indexActivity activity;

    public DataViewModel(DataFragment fragment, indexActivity activity) {
        this.fragment = fragment;
        this.activity = activity;
    }

    public void keyPersonInit() {
        fragment.loadingShow();
        pageNum = 1;
        fragment.personList.clear();
        //加载数据
        GetKeyPersonListParams params = new GetKeyPersonListParams();
        params.setPageNum(pageNum + "");
        if (fragment.filterType == 0) {
            params.setXm(fragment.binding.etInput.getText().toString());
        } else {
            params.setSfzh(fragment.binding.etInput.getText().toString());
        }
        activity.requestClient.getKeyPerson(params, new CustomCallBack<List<KeyPerson>>() {
            @Override
            public void onError(@NonNull Call call, @NonNull Exception e) {
                UiUtils.toast(e.getMessage());
                fragment.loadingNone();
            }

            @Override
            public void onSuccess(List<KeyPerson> value) {
                fragment.loadingNone();
                if (value != null) {
                    fragment.personList.addAll(value);
                }
                fragment.adapter.notifyDataSetChanged();
            }
        });
    }

    public void orgKeyPersonInit() {
        fragment.loadingShow();
        pageNum = 1;
        fragment.personList.clear();
        //加载数据
        GetOrgKeyPersonListParams params = new GetOrgKeyPersonListParams();
        params.setPageNum(pageNum + "");
        if (fragment.filterType == 0) {
            params.setXm(fragment.binding.etInput.getText().toString());
        } else {
            params.setSfzh(fragment.binding.etInput.getText().toString());
        }
        activity.requestClient.getOrgKeyPerson(params, new CustomCallBack<List<KeyPerson>>() {
            @Override
            public void onError(@NonNull Call call, @NonNull Exception e) {
                fragment.loadingNone();
                UiUtils.toast(e.getMessage());
            }

            @Override
            public void onSuccess(List<KeyPerson> value) {
                fragment.loadingNone();
                if (value != null) {
                    fragment.personList.addAll(value);
                }
                fragment.adapter.notifyDataSetChanged();
            }
        });
    }

    public void keyPersonLoadMore() {
        fragment.loadingShow();
        //加载数据
        GetKeyPersonListParams params = new GetKeyPersonListParams();
        pageNum++;
        params.setPageNum(pageNum + "");
        activity.requestClient.getKeyPerson(params, new CustomCallBack<List<KeyPerson>>() {
            @Override
            public void onError(@NonNull Call call, @NonNull Exception e) {
                fragment.loadingNone();
                fragment.binding.refreshLayout.finishLoadMore();
                UiUtils.toast(e.getMessage());
            }

            @Override
            public void onSuccess(List<KeyPerson> value) {
                fragment.loadingNone();
                if (value == null || value.size() < Constants.PAGE_SIZE) {
                    fragment.binding.refreshLayout.setNoMoreData(true);
                }
                if (value != null) {
                    fragment.personList.addAll(value);
                }
                fragment.adapter.notifyDataSetChanged();
                fragment.binding.refreshLayout.finishLoadMore();
            }
        });
    }

    public void keyPersonRefresh() {
        fragment.loadingShow();
        fragment.personList.clear();
        pageNum = 1;
        //加载数据
        GetKeyPersonListParams params = new GetKeyPersonListParams();
        params.setPageNum(pageNum + "");
        activity.requestClient.getKeyPerson(params, new CustomCallBack<List<KeyPerson>>() {
            @Override
            public void onError(@NonNull Call call, @NonNull Exception e) {
                fragment.loadingNone();
                fragment.binding.refreshLayout.finishRefresh();
                UiUtils.toast(e.getMessage());
            }

            @Override
            public void onSuccess(List<KeyPerson> value) {
                fragment.loadingNone();
                if (value != null) {
                    fragment.personList.addAll(value);
                }
                fragment.adapter.notifyDataSetChanged();
                fragment.binding.refreshLayout.finishRefresh();
            }
        });
    }

    public void orgKeyPersonLoadMore() {
        fragment.loadingShow();
        //加载数据
        GetOrgKeyPersonListParams params = new GetOrgKeyPersonListParams();
        pageNum++;
        params.setPageNum(pageNum + "");
        activity.requestClient.getOrgKeyPerson(params, new CustomCallBack<List<KeyPerson>>() {
            @Override
            public void onError(@NonNull Call call, @NonNull Exception e) {
                fragment.loadingNone();
                fragment.binding.refreshLayout.finishLoadMore();
                UiUtils.toast(e.getMessage());
            }

            @Override
            public void onSuccess(List<KeyPerson> value) {
                fragment.loadingNone();
                if (value == null || value.size() < Constants.PAGE_SIZE) {
                    fragment.binding.refreshLayout.setNoMoreData(true);
                }
                if (value != null) {
                    fragment.personList.addAll(value);
                }
                fragment.adapter.notifyDataSetChanged();
                fragment.binding.refreshLayout.finishLoadMore();
            }
        });
    }

    public void orgKeyPersonRefresh() {
        fragment.loadingShow();
        fragment.personList.clear();
        pageNum = 1;
        //加载数据
        GetOrgKeyPersonListParams params = new GetOrgKeyPersonListParams();
        params.setPageNum(pageNum + "");
        activity.requestClient.getOrgKeyPerson(params, new CustomCallBack<List<KeyPerson>>() {
            @Override
            public void onError(@NonNull Call call, @NonNull Exception e) {
                fragment.loadingNone();
                fragment.binding.refreshLayout.finishRefresh();
                UiUtils.toast(e.getMessage());
            }

            @Override
            public void onSuccess(List<KeyPerson> value) {
                fragment.loadingNone();
                if (value != null) {
                    fragment.personList.addAll(value);
                }
                fragment.adapter.notifyDataSetChanged();
                fragment.binding.refreshLayout.finishRefresh();
            }
        });
    }

    public void keyPersonDetail(String sfzh) {
        fragment.loadingShow();
        activity.requestClient.keyPersonDetail(sfzh, new CustomCallBack<KeyPersonDetail>() {
            @Override
            public void onError(@NonNull Call call, @NonNull Exception e) {
                fragment.loadingNone();
                UiUtils.toast(e.getMessage());
            }

            @Override
            public void onSuccess(KeyPersonDetail value) {
                fragment.loadingNone();
                KeyPersonDetailActivity.startAction(activity, new Gson().toJson(value));
            }
        });
    }

    /**
     * 警情数据初始化
     */
    public void policeEmergencyInit() {
        fragment.loadingShow();
        pageNum = 1;
        fragment.policeList.clear();
        //加载数据
        GetPoliceEmergencyListParams params = new GetPoliceEmergencyListParams();
        params.setPageNum(pageNum + "");
        if (fragment.filterType == 0) {
            params.setJcjbh(fragment.binding.etInput.getText().toString());
        } else if (fragment.filterType == 1) {
            params.setBjjyqk(fragment.binding.etInput.getText().toString());
        } else {
            params.setBjrlxdh(fragment.binding.etInput.getText().toString());
        }
        activity.requestClient.getPoliceEmergency(params, new CustomCallBack<List<PoliceEmergency>>() {
            @Override
            public void onError(@NonNull Call call, @NonNull Exception e) {
                UiUtils.toast(e.getMessage());
                fragment.loadingNone();
            }

            @Override
            public void onSuccess(List<PoliceEmergency> value) {
                fragment.loadingNone();
                if (value != null) {
                    fragment.policeList.addAll(value);
                }
                fragment.adapter.notifyDataSetChanged();
            }
        });
    }

    public void policeEmergencyRefresh() {
        fragment.loadingShow();
        pageNum = 1;
        fragment.policeList.clear();
        //加载数据
        GetPoliceEmergencyListParams params = new GetPoliceEmergencyListParams();
        params.setPageNum(pageNum + "");
        if (fragment.filterType == 0) {
            params.setJcjbh(fragment.binding.etInput.getText().toString());
        } else if (fragment.filterType == 1) {
            params.setBjjyqk(fragment.binding.etInput.getText().toString());
        } else {
            params.setBjrlxdh(fragment.binding.etInput.getText().toString());
        }
        activity.requestClient.getPoliceEmergency(params, new CustomCallBack<List<PoliceEmergency>>() {
            @Override
            public void onError(@NonNull Call call, @NonNull Exception e) {
                fragment.binding.refreshLayout.finishRefresh();
                UiUtils.toast(e.getMessage());
                fragment.loadingNone();
            }

            @Override
            public void onSuccess(List<PoliceEmergency> value) {
                fragment.binding.refreshLayout.finishRefresh();
                fragment.loadingNone();
                if (value != null) {
                    fragment.policeList.addAll(value);
                }
                fragment.adapter.notifyDataSetChanged();
            }
        });
    }

    public void policeEmergencyLoadMore() {
        fragment.loadingShow();
        //加载数据
        GetPoliceEmergencyListParams params = new GetPoliceEmergencyListParams();
        pageNum++;
        params.setPageNum(pageNum + "");
        activity.requestClient.getPoliceEmergency(params, new CustomCallBack<List<PoliceEmergency>>() {
            @Override
            public void onError(@NonNull Call call, @NonNull Exception e) {
                fragment.loadingNone();
                fragment.binding.refreshLayout.finishLoadMore();
                UiUtils.toast(e.getMessage());
            }

            @Override
            public void onSuccess(List<PoliceEmergency> value) {
                fragment.loadingNone();
                if (value == null || value.size() < Constants.PAGE_SIZE) {
                    fragment.binding.refreshLayout.setNoMoreData(true);
                }
                if (value != null) {
                    fragment.policeList.addAll(value);
                }
                fragment.adapter.notifyDataSetChanged();
                fragment.binding.refreshLayout.finishLoadMore();
            }
        });
    }

    public void policeEmergencyDetail(String jcjbh) {
        fragment.loadingShow();
        activity.requestClient.getPoliceEmergencyDetail(jcjbh, new CustomCallBack<PoliceEmergencyDetail>() {
            @Override
            public void onError(@NonNull Call call, @NonNull Exception e) {
                fragment.loadingNone();
                UiUtils.toast(e.getMessage());
            }

            @Override
            public void onSuccess(PoliceEmergencyDetail value) {
                fragment.loadingNone();
                PoliceEmergencyDetailActivity.startAction(activity, new Gson().toJson(value));
            }
        });
    }

    public void getKeyPersonMessageCount() {
        fragment.loadingShow();
        activity.requestClient.getKeyPersonMessageCount( new CustomCallBack<DataCount>() {
            @Override
            public void onError(@NonNull Call call, @NonNull Exception e) {
                fragment.loadingNone();
                UiUtils.toast(e.getMessage());
            }

            @Override
            public void onSuccess(DataCount value) {
                fragment.loadingNone();
                fragment.binding.tvKeyPersonCount.setText(value.getZdryCount());
                fragment.binding.tvPoliceEmergencyCount.setText(value.getJqCount());
                fragment.binding.tvCaseDetailsCount.setText(value.getAqCount());
                fragment.binding.tvKeyPersonTabCount.setText(value.getZdryCount());
                fragment.binding.tvAreaKeyTabCount.setText(value.getZdryDeptCount());
            }
        });
    }
}
