package com.casic.dcms.vm;

import android.content.Context;

import androidx.lifecycle.MutableLiveData;

import com.casic.dcms.model.CaseResourceBean;
import com.casic.dcms.model.CaseTendencyBean;
import com.casic.dcms.model.CityBaseDataBean;
import com.casic.dcms.model.DeptHandleConditionBean;
import com.casic.dcms.model.GridAnalysisBean;
import com.casic.dcms.model.HighEventBean;
import com.casic.dcms.model.PunishRankingBean;
import com.casic.dcms.model.PunishTypeStatisticBean;
import com.casic.dcms.utils.StringHelper;
import com.casic.dcms.utils.retrofit.RetrofitServiceManager;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.pengxh.androidx.lite.base.BaseViewModel;
import com.pengxh.androidx.lite.kit.StringKit;
import com.pengxh.androidx.lite.utils.LoadState;
import com.pengxh.androidx.lite.utils.ObserverSubscriber;

import java.io.IOException;

import okhttp3.ResponseBody;
import rx.Observable;

public class DataAnalysisViewModel extends BaseViewModel {
    private final Gson gson = new Gson();
    public MutableLiveData<CityBaseDataBean> cityBaseData = new MutableLiveData<>();
    public MutableLiveData<CaseResourceBean> caseResourceData = new MutableLiveData<>();
    public MutableLiveData<HighEventBean> highEventModel = new MutableLiveData<>();
    public MutableLiveData<DeptHandleConditionBean> deptHandleConditionModel = new MutableLiveData<>();
    public MutableLiveData<GridAnalysisBean> gridAnalysisData = new MutableLiveData<>();
    public MutableLiveData<CaseTendencyBean> caseTendencyData = new MutableLiveData<>();
    public MutableLiveData<PunishTypeStatisticBean> typeStatisticData = new MutableLiveData<>();
    public MutableLiveData<PunishRankingBean> rankingData = new MutableLiveData<>();

    public void getCityBaseData(Context context) {
        loadState.setValue(LoadState.Loading);
        Observable<ResponseBody> observable = RetrofitServiceManager.getCityBaseData();
        ObserverSubscriber.addSubscribe(observable, new ObserverSubscriber.OnObserverCallback() {
            @Override
            public void onCompleted() {
                loadState.setValue(LoadState.Success);
            }

            @Override
            public void onError(Throwable e) {
                loadState.setValue(LoadState.Fail);
            }

            @Override
            public void onNext(ResponseBody responseBody) {
                try {
                    String response = responseBody.string();
                    int responseCode = StringHelper.getResponseCode(response);
                    if (responseCode == 200) {
                        CityBaseDataBean resultBean = gson.fromJson(response, new TypeToken<CityBaseDataBean>() {
                        }.getType());
                        cityBaseData.setValue(resultBean);
                        loadState.setValue(LoadState.Success);
                    } else {
                        loadState.setValue(LoadState.Fail);
                        StringKit.show(context, StringHelper.getResponseMessage(response));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void getCaseResource(Context context, String begTime, String endTime) {
        loadState.setValue(LoadState.Loading);
        Observable<ResponseBody> observable = RetrofitServiceManager.getCaseResource(begTime, endTime);
        ObserverSubscriber.addSubscribe(observable, new ObserverSubscriber.OnObserverCallback() {
            @Override
            public void onCompleted() {
                loadState.setValue(LoadState.Success);
            }

            @Override
            public void onError(Throwable e) {
                loadState.setValue(LoadState.Fail);
            }

            @Override
            public void onNext(ResponseBody responseBody) {
                try {
                    String response = responseBody.string();
                    int responseCode = StringHelper.getResponseCode(response);
                    if (responseCode == 200) {
                        CaseResourceBean resultBean = gson.fromJson(response, new TypeToken<CaseResourceBean>() {
                        }.getType());
                        caseResourceData.setValue(resultBean);
                        loadState.setValue(LoadState.Success);
                    } else {
                        loadState.setValue(LoadState.Fail);
                        StringKit.show(context, StringHelper.getResponseMessage(response));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void getHighFrequencyCase(Context context, String begTime, String endTime) {
        loadState.setValue(LoadState.Loading);
        Observable<ResponseBody> observable = RetrofitServiceManager.getHighEvent(begTime, endTime);
        ObserverSubscriber.addSubscribe(observable, new ObserverSubscriber.OnObserverCallback() {
            @Override
            public void onCompleted() {
                loadState.setValue(LoadState.Success);
            }

            @Override
            public void onError(Throwable e) {
                loadState.setValue(LoadState.Fail);
            }

            @Override
            public void onNext(ResponseBody responseBody) {
                try {
                    String response = responseBody.string();
                    int responseCode = StringHelper.getResponseCode(response);
                    if (responseCode == 200) {
                        HighEventBean resultBean = gson.fromJson(response, new TypeToken<HighEventBean>() {
                        }.getType());
                        highEventModel.setValue(resultBean);
                        loadState.setValue(LoadState.Success);
                    } else {
                        loadState.setValue(LoadState.Fail);
                        StringKit.show(context, StringHelper.getResponseMessage(response));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void getDeptHandleCondition(Context context, String begTime, String endTime) {
        loadState.setValue(LoadState.Loading);
        Observable<ResponseBody> observable = RetrofitServiceManager.getDeptHandleCondition(begTime, endTime);
        ObserverSubscriber.addSubscribe(observable, new ObserverSubscriber.OnObserverCallback() {
            @Override
            public void onCompleted() {
                loadState.setValue(LoadState.Success);
            }

            @Override
            public void onError(Throwable e) {
                loadState.setValue(LoadState.Fail);
            }

            @Override
            public void onNext(ResponseBody responseBody) {
                try {
                    String response = responseBody.string();
                    int responseCode = StringHelper.getResponseCode(response);
                    if (responseCode == 200) {
                        DeptHandleConditionBean resultBean = gson.fromJson(response, new TypeToken<DeptHandleConditionBean>() {
                        }.getType());
                        deptHandleConditionModel.setValue(resultBean);
                        loadState.setValue(LoadState.Success);
                    } else {
                        loadState.setValue(LoadState.Fail);
                        StringKit.show(context, StringHelper.getResponseMessage(response));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void getGridAnalysis(Context context, String begTime, String endTime) {
        loadState.setValue(LoadState.Loading);
        Observable<ResponseBody> observable = RetrofitServiceManager.getGridAnalysis(begTime, endTime);
        ObserverSubscriber.addSubscribe(observable, new ObserverSubscriber.OnObserverCallback() {
            @Override
            public void onCompleted() {
                loadState.setValue(LoadState.Success);
            }

            @Override
            public void onError(Throwable e) {
                loadState.setValue(LoadState.Fail);
            }

            @Override
            public void onNext(ResponseBody responseBody) {
                try {
                    String response = responseBody.string();
                    int responseCode = StringHelper.getResponseCode(response);
                    if (responseCode == 200) {
                        GridAnalysisBean resultBean = gson.fromJson(response, new TypeToken<GridAnalysisBean>() {
                        }.getType());
                        gridAnalysisData.setValue(resultBean);
                        loadState.setValue(LoadState.Success);
                    } else {
                        loadState.setValue(LoadState.Fail);
                        StringKit.show(context, StringHelper.getResponseMessage(response));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void getCaseTendency(Context context, String begTime, String endTime) {
        loadState.setValue(LoadState.Loading);
        Observable<ResponseBody> observable = RetrofitServiceManager.getCaseTendency(begTime, endTime);
        ObserverSubscriber.addSubscribe(observable, new ObserverSubscriber.OnObserverCallback() {
            @Override
            public void onCompleted() {
                loadState.setValue(LoadState.Success);
            }

            @Override
            public void onError(Throwable e) {
                loadState.setValue(LoadState.Fail);
            }

            @Override
            public void onNext(ResponseBody responseBody) {
                try {
                    String response = responseBody.string();
                    int responseCode = StringHelper.getResponseCode(response);
                    if (responseCode == 200) {
                        CaseTendencyBean resultBean = gson.fromJson(response, new TypeToken<CaseTendencyBean>() {
                        }.getType());
                        caseTendencyData.setValue(resultBean);
                        loadState.setValue(LoadState.Success);
                    } else {
                        loadState.setValue(LoadState.Fail);
                        StringKit.show(context, StringHelper.getResponseMessage(response));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void getPunishTypeStatistic(Context context, String begTime, String endTime) {
        loadState.setValue(LoadState.Loading);
        Observable<ResponseBody> observable = RetrofitServiceManager.getPunishTypeStatistic(begTime, endTime);
        ObserverSubscriber.addSubscribe(observable, new ObserverSubscriber.OnObserverCallback() {
            @Override
            public void onCompleted() {
                loadState.setValue(LoadState.Success);
            }

            @Override
            public void onError(Throwable e) {
                loadState.setValue(LoadState.Fail);
            }

            @Override
            public void onNext(ResponseBody responseBody) {
                try {
                    String response = responseBody.string();
                    int responseCode = StringHelper.getResponseCode(response);
                    if (responseCode == 200) {
                        PunishTypeStatisticBean resultBean = gson.fromJson(response, new TypeToken<PunishTypeStatisticBean>() {
                        }.getType());
                        typeStatisticData.setValue(resultBean);
                        loadState.setValue(LoadState.Success);
                    } else {
                        loadState.setValue(LoadState.Fail);
                        StringKit.show(context, StringHelper.getResponseMessage(response));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void getPunishRanking(Context context, String begTime, String endTime) {
        Observable<ResponseBody> observable = RetrofitServiceManager.getPunishRanking(begTime, endTime);
        ObserverSubscriber.addSubscribe(observable, new ObserverSubscriber.OnObserverCallback() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(ResponseBody responseBody) {
                try {
                    String response = responseBody.string();
                    int responseCode = StringHelper.getResponseCode(response);
                    if (responseCode == 200) {
                        PunishRankingBean resultBean = gson.fromJson(response, new TypeToken<PunishRankingBean>() {
                        }.getType());
                        rankingData.setValue(resultBean);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
