package com.casic.dcms.vm;

import android.content.Context;

import androidx.lifecycle.MutableLiveData;

import com.casic.dcms.model.ProcessPersonBean;
import com.casic.dcms.model.WorkerBean;
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 WorkerViewModel extends BaseViewModel {
    private final Gson gson = new Gson();
    public MutableLiveData<WorkerBean> workerModel = new MutableLiveData<>();
    public MutableLiveData<ProcessPersonBean> processPersonModel = new MutableLiveData<>();

    public void getWorkers(Context context, String deptid, String roleTips, String isOnLine) {
        loadState.setValue(LoadState.Loading);
        Observable<ResponseBody> observable = RetrofitServiceManager.getOnlinePerson(deptid, roleTips, isOnLine);
        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) {
                        loadState.setValue(LoadState.Success);
                        WorkerBean resultBean = gson.fromJson(response, new TypeToken<WorkerBean>() {
                        }.getType());
                        workerModel.setValue(resultBean);
                    } else {
                        loadState.setValue(LoadState.Fail);
                        StringKit.show(context, StringHelper.getResponseMessage(response));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

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