package com.casic.dcms.vm;

import android.content.Context;

import androidx.lifecycle.MutableLiveData;

import com.casic.dcms.model.CaseCheckModel;
import com.casic.dcms.model.CaseDetailBean;
import com.casic.dcms.model.CaseHandleBean;
import com.casic.dcms.model.CaseHandledBean;
import com.casic.dcms.model.CaseSearchResultBean;
import com.casic.dcms.model.OvertimeCaseBean;
import com.casic.dcms.model.SanBaoCaseBean;
import com.casic.dcms.model.UrgentCaseBean;
import com.casic.dcms.model.VerifiedCaseModel;
import com.casic.dcms.model.VerifyCaseModel;
import com.casic.dcms.utils.LocaleConstant;
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 CaseViewModel extends BaseViewModel {
    private final Gson gson = new Gson();
    public MutableLiveData<VerifyCaseModel> caseVerifyModel = new MutableLiveData<>();
    public MutableLiveData<VerifiedCaseModel> caseVerifiedModel = new MutableLiveData<>();
    public MutableLiveData<CaseCheckModel> caseCheckModel = new MutableLiveData<>();
    public MutableLiveData<CaseHandleBean> caseHandleModel = new MutableLiveData<>();
    public MutableLiveData<CaseHandledBean> caseHandledModel = new MutableLiveData<>();
    public MutableLiveData<UrgentCaseBean> caseUrgentModel = new MutableLiveData<>();
    public MutableLiveData<OvertimeCaseBean> caseOvertimeModel = new MutableLiveData<>();
    public MutableLiveData<SanBaoCaseBean> shopCaseModel = new MutableLiveData<>();
    public MutableLiveData<CaseSearchResultBean> searchResultModel = new MutableLiveData<>();
    public MutableLiveData<CaseDetailBean> caseDetailModel = new MutableLiveData<>();

    /**
     * 未核实、已核实
     */
    public void getVerifyCaseList(Context context, int limit, int offset, boolean verified) {
        Observable<ResponseBody> observable;
        if (verified) {
            observable = RetrofitServiceManager.getVerifiedResult(limit, offset);
        } else {
            observable = RetrofitServiceManager.getVerifyResult(limit, 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 (verified) {
                            VerifiedCaseModel resultBean = gson.fromJson(response, new TypeToken<VerifiedCaseModel>() {
                            }.getType());
                            caseVerifiedModel.setValue(resultBean);
                        } else {
                            VerifyCaseModel resultBean = gson.fromJson(response, new TypeToken<VerifyCaseModel>() {
                            }.getType());
                            caseVerifyModel.setValue(resultBean);
                        }
                    } else {
                        StringKit.show(context, StringHelper.getResponseMessage(response));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void getCheckCaseList(Context context, int limit, int offset, boolean isChecked) {
        Observable<ResponseBody> observable;
        if (isChecked) {
            observable = RetrofitServiceManager.getCheckedResult(limit, offset);
        } else {
            observable = RetrofitServiceManager.getCheckResult(limit, 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) {
                        CaseCheckModel resultBean = gson.fromJson(response, new TypeToken<CaseCheckModel>() {
                        }.getType());
                        caseCheckModel.setValue(resultBean);
                    } else {
                        StringKit.show(context, StringHelper.getResponseMessage(response));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void getHandleCaseList(Context context, int offset, boolean isOnMap) {
        Observable<ResponseBody> observable;
        if (isOnMap) {
            observable = RetrofitServiceManager.handleCaseResult(1000, 1);
        } else {
            observable = RetrofitServiceManager.handleCaseResult(LocaleConstant.PAGE_LIMIT, 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) {
                        CaseHandleBean resultBean = gson.fromJson(response, new TypeToken<CaseHandleBean>() {
                        }.getType());
                        caseHandleModel.setValue(resultBean);
                    } else {
                        StringKit.show(context, StringHelper.getResponseMessage(response));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

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

    public void getUrgentCaseList(Context context, String isSupervise, int offset) {
        Observable<ResponseBody> observable = RetrofitServiceManager.getUrgentCase(isSupervise, 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) {
                        UrgentCaseBean resultBean = gson.fromJson(response, new TypeToken<UrgentCaseBean>() {
                        }.getType());
                        caseUrgentModel.setValue(resultBean);
                    } else {
                        StringKit.show(context, StringHelper.getResponseMessage(response));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void getOvertimeCaseList(Context context, String isOvertime, int offset) {
        Observable<ResponseBody> observable = RetrofitServiceManager.getOvertimeCase(isOvertime, 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) {
                        OvertimeCaseBean resultBean = gson.fromJson(response, new TypeToken<OvertimeCaseBean>() {
                        }.getType());
                        caseOvertimeModel.setValue(resultBean);
                    } else {
                        StringKit.show(context, StringHelper.getResponseMessage(response));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void getShopCaseList(Context context, String isShop, int offset, boolean isCompleted) {
        Observable<ResponseBody> observable;
        if (isCompleted) {
            observable = RetrofitServiceManager.getCompletedListResult(isShop, offset);
        } else {
            observable = RetrofitServiceManager.getPackageWorkListResult(isShop, 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) {
                        SanBaoCaseBean resultBean = gson.fromJson(response, new TypeToken<SanBaoCaseBean>() {
                        }.getType());
                        shopCaseModel.setValue(resultBean);
                    } else {
                        StringKit.show(context, StringHelper.getResponseMessage(response));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void searchCase(Context context, String eorc, String casetypeCode, String casetypeDetailCode, String caseState, String registerTimeBegin, String registerTimeEnd, String isShopCase, int offset) {
        loadState.setValue(LoadState.Loading);
        Observable<ResponseBody> observable = RetrofitServiceManager.getCaseListResult(eorc, casetypeCode, casetypeDetailCode, caseState, registerTimeBegin, registerTimeEnd, isShopCase, offset);
        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) {
                        CaseSearchResultBean resultBean = gson.fromJson(response, new TypeToken<CaseSearchResultBean>() {
                        }.getType());
                        searchResultModel.setValue(resultBean);
                        loadState.setValue(LoadState.Success);
                    } else {
                        loadState.setValue(LoadState.Fail);
                        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 getCaseDetail(Context context, String id) {
        loadState.setValue(LoadState.Loading);
        Observable<ResponseBody> observable = RetrofitServiceManager.getCaseDetailResult(id);
        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) {
                        CaseDetailBean detailBean = gson.fromJson(response, new TypeToken<CaseDetailBean>() {
                        }.getType());
                        caseDetailModel.setValue(detailBean);
                        loadState.setValue(LoadState.Success);
                    } else {
                        loadState.setValue(LoadState.Fail);
                        StringKit.show(context, StringHelper.getResponseMessage(response));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
