package com.casic.dcms.vm;

import android.content.Context;

import androidx.lifecycle.MutableLiveData;

import com.casic.dcms.model.dept.AdjustCaseModel;
import com.casic.dcms.model.dept.AuthorizeCaseModel;
import com.casic.dcms.model.dept.DelayedCaseModel;
import com.casic.dcms.model.dept.HandleCaseModel;
import com.casic.dcms.model.dept.HandledCaseModel;
import com.casic.dcms.model.dept.PostCaseModel;
import com.casic.dcms.model.dept.TodoCaseModel;
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.ObserverSubscriber;

import java.io.IOException;

import okhttp3.ResponseBody;
import rx.Observable;

public class DeptCaseViewModel extends BaseViewModel {
    private final Gson gson = new Gson();
    public MutableLiveData<TodoCaseModel> deptTodoCaseListModel = new MutableLiveData<>();
    public MutableLiveData<HandleCaseModel> deptHandleCaseListModel = new MutableLiveData<>();
    public MutableLiveData<HandledCaseModel> deptHandledCaseListModel = new MutableLiveData<>();
    public MutableLiveData<AdjustCaseModel> deptAdjustCaseListModel = new MutableLiveData<>();
    public MutableLiveData<AuthorizeCaseModel> deptAuthorizeCaseListModel = new MutableLiveData<>();
    public MutableLiveData<DelayedCaseModel> deptDelayedCaseListModel = new MutableLiveData<>();
    public MutableLiveData<PostCaseModel> deptPostBeanCaseListModel = new MutableLiveData<>();

    public void getDeptStatusCaseList(Context context, int offset, String pageTag) {
        Observable<ResponseBody> observable;
        if (pageTag.equals("todo")) {
            //
            observable = RetrofitServiceManager.getWorkListResult(offset);
        } else if (pageTag.equals("process")) {
            //process
            observable = RetrofitServiceManager.getProcessListResult(offset);
        } else {
            //handled
            observable = RetrofitServiceManager.getCompletedListResult(offset);
        }
        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) {
                        if (pageTag.equals("todo")) {
                            TodoCaseModel resultBean = gson.fromJson(response, new TypeToken<TodoCaseModel>() {
                            }.getType());
                            deptTodoCaseListModel.setValue(resultBean);
                        } else if (pageTag.equals("process")) {
                            HandleCaseModel resultBean = gson.fromJson(response, new TypeToken<HandleCaseModel>() {
                            }.getType());
                            deptHandleCaseListModel.setValue(resultBean);
                        } else {
                            HandledCaseModel resultBean = gson.fromJson(response, new TypeToken<HandledCaseModel>() {
                            }.getType());
                            deptHandledCaseListModel.setValue(resultBean);
                        }
                    } else {
                        StringKit.show(context, StringHelper.getResponseMessage(response));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void getDeptStatusCaseList(Context context, int offset, boolean isAdjust) {
        Observable<ResponseBody> observable;
        if (isAdjust) {
            observable = RetrofitServiceManager.getCaseAdjustResult(offset);
        } else {
            observable = RetrofitServiceManager.getCaseAuthorizeResult(offset);
        }
        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) {
                        if (isAdjust) {
                            AdjustCaseModel resultBean = gson.fromJson(response, new TypeToken<AdjustCaseModel>() {
                            }.getType());
                            deptAdjustCaseListModel.setValue(resultBean);
                        } else {
                            AuthorizeCaseModel resultBean = gson.fromJson(response, new TypeToken<AuthorizeCaseModel>() {
                            }.getType());
                            deptAuthorizeCaseListModel.setValue(resultBean);
                        }
                    } else {
                        StringKit.show(context, StringHelper.getResponseMessage(response));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void getDelayedOrPostCaseList(Context context, int offset, boolean isDelayed) {
        Observable<ResponseBody> observable;
        if (isDelayed) {
            observable = RetrofitServiceManager.getDelayedListResult(offset);
        } else {
            observable = RetrofitServiceManager.getPostponeListResult(offset);
        }
        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) {
                        if (isDelayed) {
                            DelayedCaseModel resultBean = gson.fromJson(response, new TypeToken<DelayedCaseModel>() {
                            }.getType());
                            deptDelayedCaseListModel.setValue(resultBean);
                        } else {
                            PostCaseModel resultBean = gson.fromJson(response, new TypeToken<PostCaseModel>() {
                            }.getType());
                            deptPostBeanCaseListModel.setValue(resultBean);
                        }
                    } else {
                        StringKit.show(context, StringHelper.getResponseMessage(response));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
