package com.casic.dcms.vm;

import android.content.Context;

import androidx.lifecycle.MutableLiveData;

import com.casic.dcms.model.ActionResultBean;
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 CaseOperateViewModel extends BaseViewModel {
    private final Gson gson = new Gson();
    public MutableLiveData<ActionResultBean> verifyActionModel = new MutableLiveData<>();
    public MutableLiveData<ActionResultBean> checkActionModel = new MutableLiveData<>();
    public MutableLiveData<ActionResultBean> overtimeActionModel = new MutableLiveData<>();
    public MutableLiveData<ActionResultBean> shopActionModel = new MutableLiveData<>();
    public MutableLiveData<ActionResultBean> updateSuperviseActionModel = new MutableLiveData<>();
    public MutableLiveData<ActionResultBean> dispatcherHandleActionModel = new MutableLiveData<>();

    public void verifyCase(Context context, String processId, String bizId, String currState, String changeState, String remarks, String fileId, String eorc, String casetypeCode, String casetypeDetailCode, String lng, String lat, String caseLevel, String areaCode, String streetCode, String communityCode, String gridId, String fieldintro) {
        loadState.setValue(LoadState.Loading);
        Observable<ResponseBody> observable = RetrofitServiceManager.getHandleCaseResult(processId, bizId, currState, changeState, remarks, fileId, eorc, casetypeCode, casetypeDetailCode, lng, lat, caseLevel, areaCode, streetCode, communityCode, gridId, fieldintro);
        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) {
                        ActionResultBean resultBean = gson.fromJson(response, new TypeToken<ActionResultBean>() {
                        }.getType());
                        verifyActionModel.setValue(resultBean);
                        loadState.setValue(LoadState.Success);
                    } else {
                        StringKit.show(context, StringHelper.getResponseMessage(response));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void checkCase(Context context, String processId, String bizId, String currState, String changeState, String remarks, String fileId) {
        loadState.setValue(LoadState.Loading);
        Observable<ResponseBody> observable = RetrofitServiceManager.getHandleCaseResult(processId, bizId, currState, changeState, remarks, fileId);
        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) {
                        ActionResultBean resultBean = gson.fromJson(response, new TypeToken<ActionResultBean>() {
                        }.getType());
                        checkActionModel.setValue(resultBean);
                        loadState.setValue(LoadState.Success);
                    } else {
                        loadState.setValue(LoadState.Fail);
                        StringKit.show(context, StringHelper.getResponseMessage(response));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void handleOvertimeCase(Context context, String processId, String bizId, String currState, String changeState, String remarks, String fileId, String approvalResult) {
        loadState.setValue(LoadState.Loading);
        Observable<ResponseBody> observable = RetrofitServiceManager.getHandleCaseResult(processId, bizId, currState, changeState, remarks, fileId, approvalResult);
        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) {
                        ActionResultBean resultBean = gson.fromJson(response, new TypeToken<ActionResultBean>() {
                        }.getType());
                        overtimeActionModel.setValue(resultBean);
                        loadState.setValue(LoadState.Success);
                    } else {
                        loadState.setValue(LoadState.Fail);
                        StringKit.show(context, StringHelper.getResponseMessage(response));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void handleShopCase(Context context, String id, String punishType, String punishRemarks, String punishTime, String punishImage) {
        Observable<ResponseBody> observable = RetrofitServiceManager.getHandlePackageResult(id, punishType, punishRemarks, punishTime, punishImage);
        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) {
                        ActionResultBean resultBean = gson.fromJson(response, new TypeToken<ActionResultBean>() {
                        }.getType());
                        shopActionModel.setValue(resultBean);
                    } else {
                        StringKit.show(context, StringHelper.getResponseMessage(response));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void submitCase(Context context, String isNeedVerify, String isNeedRevisit, String longitude, String latitude,
                           String source, String caseLevel, String eorc, String casetypeCode,
                           String casetypeDetailCode, String areaCode, String streetCode,
                           String communityCode, String gridId, String description, String fieldintro,
                           String fileIdVerify, String audioIdVerify, String videoIdVerify,
                           int bzhours, int bzminis) {
        loadState.setValue(LoadState.Loading);
        Observable<ResponseBody> commonActionObservable = RetrofitServiceManager.submitCase(isNeedVerify, isNeedRevisit, longitude, latitude, source, caseLevel, eorc, casetypeCode, casetypeDetailCode, areaCode, streetCode, communityCode, gridId, description, fieldintro, fileIdVerify, audioIdVerify, videoIdVerify, bzhours, bzminis);
        ObserverSubscriber.addSubscribe(commonActionObservable, 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) {
                        loadState.setValue(LoadState.Fail);
                        StringKit.show(context, StringHelper.getResponseMessage(response));
                    } else {
                        loadState.setValue(LoadState.Success);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void submitShopCase(Context context, String isNeedVerify, String isNeedRevisit, String longitude, String latitude,
                               String source, String caseLevel, String areaCode, String streetCode,
                               String communityCode, String gridId, String description, String fieldintro,
                               String fileIdVerify, int bzhours, int bzminis, String shopId,
                               String shopkeeperName, String shopPhone) {
        loadState.setValue(LoadState.Loading);
        Observable<ResponseBody> commonActionObservable = RetrofitServiceManager.submitShopCase(isNeedVerify, isNeedRevisit, longitude, latitude, source, caseLevel, areaCode, streetCode, communityCode, gridId, description, fieldintro, fileIdVerify, bzhours, bzminis, shopId, shopkeeperName, shopPhone);
        ObserverSubscriber.addSubscribe(commonActionObservable, 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) {
                        loadState.setValue(LoadState.Fail);
                        StringKit.show(context, StringHelper.getResponseMessage(response));
                    } else {
                        loadState.setValue(LoadState.Success);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

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

    public void dispatcherHandleCase(Context context, String processId, String bizId, String currState, String changeState, String remarks, String approvalResult, String operationKey, String taskUserId) {
        loadState.setValue(LoadState.Loading);
        Observable<ResponseBody> observable = RetrofitServiceManager.getDispatcherHandleCaseResult(processId, bizId, currState, changeState, remarks, approvalResult, operationKey, taskUserId);
        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) {
                        ActionResultBean resultBean = gson.fromJson(response, new TypeToken<ActionResultBean>() {
                        }.getType());
                        dispatcherHandleActionModel.setValue(resultBean);
                        loadState.setValue(LoadState.Success);
                    } else {
                        loadState.setValue(LoadState.Fail);
                        StringKit.show(context, StringHelper.getResponseMessage(response));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
