package com.casic.dcms.vm;

import android.content.Context;

import androidx.lifecycle.MutableLiveData;

import com.casic.dcms.model.ActionResultBean;
import com.casic.dcms.model.LampBoxDetailBean;
import com.casic.dcms.model.LampBoxListBean;
import com.casic.dcms.model.MapLampBoxBean;
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 LampBoxViewModel extends BaseViewModel {

    private final Gson gson = new Gson();
    public MutableLiveData<LampBoxListBean> lampBoxResult = new MutableLiveData<>(); //分页
    public MutableLiveData<ActionResultBean> controlResult = new MutableLiveData<>();
    public MutableLiveData<MapLampBoxBean> mapLampBoxResult = new MutableLiveData<>(); //不分页
    public MutableLiveData<LampBoxDetailBean> lampBoxDetailResult = new MutableLiveData<>();

    public void getLampBoxesByPage(Context context, int offset) {
        loadState.setValue(LoadState.Loading);
        Observable<ResponseBody> observable = RetrofitServiceManager.getLampBoxesByPage(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) {
                        LampBoxListBean listBean = gson.fromJson(response, new TypeToken<LampBoxListBean>() {
                        }.getType());
                        lampBoxResult.setValue(listBean);
                        loadState.setValue(LoadState.Success);
                    } else {
                        loadState.setValue(LoadState.Fail);
                        StringKit.show(context, StringHelper.getResponseMessage(response));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void controlLampBox(Context context, String lampBoxId, String controlType) {
        loadState.setValue(LoadState.Loading);
        Observable<ResponseBody> observable = RetrofitServiceManager.controlLampBox(lampBoxId, controlType);
        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());
                        controlResult.setValue(resultBean);
                        loadState.setValue(LoadState.Success);
                    } else {
                        loadState.setValue(LoadState.Fail);
                        StringKit.show(context, StringHelper.getResponseMessage(response));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

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

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