package com.casic.dcms.vm;

import androidx.lifecycle.MutableLiveData;

import com.casic.dcms.model.PersonStatusBean;
import com.casic.dcms.model.ProcessPersonBean;
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.vm.BaseViewModel;
import com.pengxh.androidx.lite.vm.ObserverSubscriber;

import java.io.IOException;

import okhttp3.ResponseBody;
import rx.Observable;

public class PersonViewModel extends BaseViewModel {
    private final Gson gson = new Gson();
    public MutableLiveData<PersonStatusBean> personStatusModel = new MutableLiveData<>();
    public MutableLiveData<ProcessPersonBean> processPersonModel = new MutableLiveData<>();

    public void obtainPersonByStatus(String deptid, String roleTips, String isOnLine) {
        Observable<ResponseBody> observable = RetrofitServiceManager.getOnlinePerson(deptid, roleTips, isOnLine);
        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) {
                        PersonStatusBean resultBean = gson.fromJson(response, new TypeToken<PersonStatusBean>() {
                        }.getType());
                        personStatusModel.setValue(resultBean);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void obtainSubordinatePerson() {
        Observable<ResponseBody> observable = RetrofitServiceManager.getProcessPersonResult();
        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) {
                        ProcessPersonBean resultBean = gson.fromJson(response, new TypeToken<ProcessPersonBean>() {
                        }.getType());
                        processPersonModel.setValue(resultBean);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
