package com.casic.dcms.vm;

import android.content.Context;

import androidx.lifecycle.MutableLiveData;

import com.casic.dcms.model.CaseLargeTypeBean;
import com.casic.dcms.model.CaseProcessBean;
import com.casic.dcms.model.CaseSmallTypeBean;
import com.casic.dcms.model.CaseStatusBean;
import com.casic.dcms.model.ComSentenceBean;
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 CommonDataViewModel extends BaseViewModel {
    private final Gson gson = new Gson();
    public MutableLiveData<CaseLargeTypeBean> largeTypeModel = new MutableLiveData<>();
    public MutableLiveData<CaseSmallTypeBean> smallTypeModel = new MutableLiveData<>();
    public MutableLiveData<CaseStatusBean> caseStatusModel = new MutableLiveData<>();
    public MutableLiveData<ComSentenceBean> sentenceModel = new MutableLiveData<>();
    public MutableLiveData<CaseProcessBean> caseProcessModel = new MutableLiveData<>();

    public void getLargeType(Context context, String eorc) {
        Observable<ResponseBody> observable = RetrofitServiceManager.getCaseLargeType(eorc);
        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) {
                        CaseLargeTypeBean resultBean = gson.fromJson(response, new TypeToken<CaseLargeTypeBean>() {
                        }.getType());
                        largeTypeModel.setValue(resultBean);
                    } else {
                        StringKit.show(context, StringHelper.getResponseMessage(response));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void getSmallType(Context context, String eorc, String typeId) {
        Observable<ResponseBody> observable = RetrofitServiceManager.getCaseSmallType(eorc, typeId);
        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) {
                        CaseSmallTypeBean resultBean = gson.fromJson(response, new TypeToken<CaseSmallTypeBean>() {
                        }.getType());
                        smallTypeModel.setValue(resultBean);
                    } else {
                        StringKit.show(context, StringHelper.getResponseMessage(response));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void getCaseStatus(Context context) {
        Observable<ResponseBody> observable = RetrofitServiceManager.getCaseStatus();
        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) {
                        CaseStatusBean resultBean = gson.fromJson(response, new TypeToken<CaseStatusBean>() {
                        }.getType());
                        caseStatusModel.setValue(resultBean);
                    } else {
                        StringKit.show(context, StringHelper.getResponseMessage(response));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void getCommonSentence(Context context) {
        Observable<ResponseBody> observable = RetrofitServiceManager.getCommonSentenceResult();
        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) {
                        ComSentenceBean resultBean = gson.fromJson(response, new TypeToken<ComSentenceBean>() {
                        }.getType());
                        sentenceModel.setValue(resultBean);
                    } else {
                        StringKit.show(context, StringHelper.getResponseMessage(response));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void getCaseProcess(Context context, String processId) {
        loadState.setValue(LoadState.Loading);
        Observable<ResponseBody> observable = RetrofitServiceManager.getCaseProcessResult(processId);
        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) {
                        CaseProcessBean resultBean = gson.fromJson(response, new TypeToken<CaseProcessBean>() {
                        }.getType());
                        caseProcessModel.setValue(resultBean);
                        loadState.setValue(LoadState.Success);
                    } else {
                        loadState.setValue(LoadState.Fail);
                        StringKit.show(context, StringHelper.getResponseMessage(response));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
