package com.jachdev.consumerprotection.ui.classifier;

import android.util.Log;

import com.jachdev.consumerprotection.AppApplication;
import com.jachdev.consumerprotection.data.AppResponse;
import com.jachdev.consumerprotection.data.BotMessage;
import com.jachdev.consumerprotection.data.BotResponse;
import com.jachdev.consumerprotection.data.DietPlan;
import com.jachdev.consumerprotection.data.LipidProfile;
import com.jachdev.consumerprotection.data.Nutrition;
import com.jachdev.consumerprotection.data.NutritionResponse;
import com.jachdev.consumerprotection.network.AppService;
import com.jachdev.consumerprotection.util.BaseViewModel;
import com.jachdev.consumerprotection.util.SessionManager;

import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.List;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import io.reactivex.SingleObserver;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class ClassifierViewModel extends BaseViewModel {


    private static final String TAG = ClassifierViewModel.class.getSimpleName();
    private AppService calService;
    private AppService appService;

    private MutableLiveData<List<Nutrition>> data = new MutableLiveData<>();
    private MutableLiveData<List<DietPlan>> dietPlanData = new MutableLiveData<>();

    public ClassifierViewModel() {
        appService = AppApplication.getInstance().getAppService();
        calService = AppApplication.getInstance().getCalService();
    }

    public LiveData<List<Nutrition>> getNutritionData() {
        return data;
    }

    public void getNutrition(String message) {

        //Send message to bot
        calService.getCalServer().getNutrition(
                message
        ).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SingleObserver<NutritionResponse>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onSuccess(NutritionResponse nutritionResponse) {

                        data.postValue(nutritionResponse.getItems());
                    }

                    @Override
                    public void onError(Throwable e) {

                        data.postValue(null);
                    }
                });
    }

    public LiveData<List<DietPlan>> getDietPlanData() {
        return dietPlanData;
    }

    public void getDietPlan() {

        appService.getServer().getDietPlan().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SingleObserver<AppResponse>() {
                    @Override
                    public void onSubscribe(@NotNull Disposable d) {
                        Log.d(TAG, "onSubscribe: ");
                    }

                    @Override
                    public void onSuccess(@NotNull AppResponse response) {
                        switch (response.getCode()){
                            case 0:
                                List<DietPlan> dietPlans = Arrays.asList(response.getObjectToType(DietPlan[].class).clone());
                                dietPlanData.postValue(dietPlans);
                                break;
                            default:
                                dietPlanData.postValue(null);
                                showMessage(response.getMessage());
                                break;
                        }
                    }

                    @Override
                    public void onError(@NotNull Throwable e) {

                        dietPlanData.postValue(null);
                    }
                });
    }
}