package com.casic.dcms.vm;

import androidx.lifecycle.MutableLiveData;

import com.casic.dcms.base.BaseApplication;
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.callback.OnObserverCallback;
import com.pengxh.androidx.lite.hub.StringHub;
import com.pengxh.androidx.lite.vm.BaseViewModel;
import com.pengxh.androidx.lite.vm.LoadState;
import com.pengxh.androidx.lite.vm.ObserverSubscriber;

import java.io.IOException;
import java.net.ConnectException;

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(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 OnObserverCallback() {
            @Override
            public void onCompleted() {
                loadState.setValue(LoadState.Success);
            }

            @Override
            public void onError(Throwable e) {
                loadState.setValue(LoadState.Fail);
                if (e instanceof ConnectException) {
                    StringHub.show(BaseApplication.get(), "连接失败，请检查网络或者服务器");
                } else {
                    StringHub.show(BaseApplication.get(), "服务器异常，请联系管理员");
                }
            }

            @Override
            public void onNext(ResponseBody responseBody) {
                try {
                    String response = responseBody.string();
                    int responseCode = StringHelper.separateResponseCode(response);
                    if (responseCode == 200) {
                        ActionResultBean resultBean = gson.fromJson(response, new TypeToken<ActionResultBean>() {
                        }.getType());
                        verifyActionModel.setValue(resultBean);
                    } else {
                        loadState.setValue(LoadState.Fail);
                        StringHelper.showErrorMessage(response);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void checkCase(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 OnObserverCallback() {
            @Override
            public void onCompleted() {
                loadState.setValue(LoadState.Success);
            }

            @Override
            public void onError(Throwable e) {
                loadState.setValue(LoadState.Fail);
                if (e instanceof ConnectException) {
                    StringHub.show(BaseApplication.get(), "连接失败，请检查网络或者服务器");
                } else {
                    StringHub.show(BaseApplication.get(), "服务器异常，请联系管理员");
                }
            }

            @Override
            public void onNext(ResponseBody responseBody) {
                try {
                    String response = responseBody.string();
                    int responseCode = StringHelper.separateResponseCode(response);
                    if (responseCode == 200) {
                        ActionResultBean resultBean = gson.fromJson(response, new TypeToken<ActionResultBean>() {
                        }.getType());
                        checkActionModel.setValue(resultBean);
                    } else {
                        loadState.setValue(LoadState.Fail);
                        StringHelper.showErrorMessage(response);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void handleOvertimeCase(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 OnObserverCallback() {
            @Override
            public void onCompleted() {
                loadState.setValue(LoadState.Success);
            }

            @Override
            public void onError(Throwable e) {
                loadState.setValue(LoadState.Fail);
                if (e instanceof ConnectException) {
                    StringHub.show(BaseApplication.get(), "连接失败，请检查网络或者服务器");
                } else {
                    StringHub.show(BaseApplication.get(), "服务器异常，请联系管理员");
                }
            }

            @Override
            public void onNext(ResponseBody responseBody) {
                try {
                    String response = responseBody.string();
                    int responseCode = StringHelper.separateResponseCode(response);
                    if (responseCode == 200) {
                        ActionResultBean resultBean = gson.fromJson(response, new TypeToken<ActionResultBean>() {
                        }.getType());
                        overtimeActionModel.setValue(resultBean);
                    } else {
                        loadState.setValue(LoadState.Fail);
                        StringHelper.showErrorMessage(response);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void handleShopCase(String id, String punishType, String punishRemarks, String punishTime, String punishImage) {
        Observable<ResponseBody> observable = RetrofitServiceManager.getHandlePackageResult(id, punishType, punishRemarks, punishTime, punishImage);
        ObserverSubscriber.addSubscribe(observable, new 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.separateResponseCode(response);
                    if (responseCode == 200) {
                        ActionResultBean resultBean = gson.fromJson(response, new TypeToken<ActionResultBean>() {
                        }.getType());
                        shopActionModel.setValue(resultBean);
                    } else {
                        loadState.setValue(LoadState.Fail);
                        StringHelper.showErrorMessage(response);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void submitCase(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 OnObserverCallback() {
            @Override
            public void onCompleted() {
                loadState.setValue(LoadState.Success);
            }

            @Override
            public void onError(Throwable e) {
                loadState.setValue(LoadState.Fail);
                if (e instanceof ConnectException) {
                    StringHub.show(BaseApplication.get(), "连接失败，请检查网络或者服务器");
                } else {
                    StringHub.show(BaseApplication.get(), "服务器异常，请联系管理员");
                }
            }

            @Override
            public void onNext(ResponseBody responseBody) {
                try {
                    String response = responseBody.string();
                    int responseCode = StringHelper.separateResponseCode(response);
                    if (responseCode != 200) {
                        loadState.setValue(LoadState.Fail);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void submitShopCase(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 OnObserverCallback() {
            @Override
            public void onCompleted() {
                loadState.setValue(LoadState.Success);
            }

            @Override
            public void onError(Throwable e) {
                loadState.setValue(LoadState.Fail);
                if (e instanceof ConnectException) {
                    StringHub.show(BaseApplication.get(), "连接失败，请检查网络或者服务器");
                } else {
                    StringHub.show(BaseApplication.get(), "服务器异常，请联系管理员");
                }
            }

            @Override
            public void onNext(ResponseBody responseBody) {
                try {
                    String response = responseBody.string();
                    int responseCode = StringHelper.separateResponseCode(response);
                    if (responseCode != 200) {
                        loadState.setValue(LoadState.Fail);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void updateCaseSupervise(String id, String superviseRemarks) {
        loadState.setValue(LoadState.Loading);
        Observable<ResponseBody> observable = RetrofitServiceManager.getUpdateSuperviseResult(id, superviseRemarks);
        ObserverSubscriber.addSubscribe(observable, new OnObserverCallback() {
            @Override
            public void onCompleted() {
                loadState.setValue(LoadState.Success);
            }

            @Override
            public void onError(Throwable e) {
                loadState.setValue(LoadState.Fail);
                if (e instanceof ConnectException) {
                    StringHub.show(BaseApplication.get(), "连接失败，请检查网络或者服务器");
                } else {
                    StringHub.show(BaseApplication.get(), "服务器异常，请联系管理员");
                }
            }

            @Override
            public void onNext(ResponseBody responseBody) {
                try {
                    String response = responseBody.string();
                    int responseCode = StringHelper.separateResponseCode(response);
                    if (responseCode == 200) {
                        ActionResultBean resultBean = gson.fromJson(response, new TypeToken<ActionResultBean>() {
                        }.getType());
                        updateSuperviseActionModel.setValue(resultBean);
                    } else {
                        loadState.setValue(LoadState.Fail);
                        StringHelper.showErrorMessage(response);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void dispatcherHandleCase(String processId, String bizId, String currState, String changeState, String remarks, String operationKey, String taskUserId) {
        loadState.setValue(LoadState.Loading);
        Observable<ResponseBody> observable = RetrofitServiceManager.getDispatcherHandleCaseResult(processId, bizId, currState, changeState, remarks, operationKey, taskUserId);
        ObserverSubscriber.addSubscribe(observable, new OnObserverCallback() {
            @Override
            public void onCompleted() {
                loadState.setValue(LoadState.Success);
            }

            @Override
            public void onError(Throwable e) {
                loadState.setValue(LoadState.Fail);
                if (e instanceof ConnectException) {
                    StringHub.show(BaseApplication.get(), "连接失败，请检查网络或者服务器");
                } else {
                    StringHub.show(BaseApplication.get(), "服务器异常，请联系管理员");
                }
            }

            @Override
            public void onNext(ResponseBody responseBody) {
                try {
                    String response = responseBody.string();
                    int responseCode = StringHelper.separateResponseCode(response);
                    if (responseCode == 200) {
                        ActionResultBean resultBean = gson.fromJson(response, new TypeToken<ActionResultBean>() {
                        }.getType());
                        dispatcherHandleActionModel.setValue(resultBean);
                    } else {
                        loadState.setValue(LoadState.Fail);
                        StringHelper.showErrorMessage(response);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
