package com.jachdev.consumerprotection.ui.exercise;

import android.util.Log;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jachdev.consumerprotection.AppApplication;
import com.jachdev.consumerprotection.data.AppResponse;
import com.jachdev.consumerprotection.data.DietPlan;
import com.jachdev.consumerprotection.data.Exercise;
import com.jachdev.consumerprotection.data.FirebasePredictionData;
import com.jachdev.consumerprotection.data.Health;
import com.jachdev.consumerprotection.data.LipidProfile;
import com.jachdev.consumerprotection.data.User;
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.lang.reflect.Type;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import androidx.annotation.NonNull;
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 ExerciseViewModel extends BaseViewModel {

    private static final String TAG = ExerciseViewModel.class.getSimpleName();
    private MutableLiveData<List<Health>> listMutableLiveData = new MutableLiveData<>();
    private MutableLiveData<List<DietPlan>> dietPlanData = new MutableLiveData<>();
    private MutableLiveData<List<Exercise>> exerData = new MutableLiveData<>();

    private SessionManager sessionManager;
    private AppService service;

    public ExerciseViewModel() {
        sessionManager = SessionManager.getInstance();
        service = AppApplication.getInstance().getAppService();
    }

    public LiveData<List<Health>> getHealthData() {
        return listMutableLiveData;
    }

    public void addHealth(double height, double weight) {

        Health health = new Health();
        health.setUid(sessionManager.getUser().getUid());
        health.setHeight(height);
        health.setWeight(weight);

        service.getServer().addHealth(health)
                .subscribeOn(Schedulers.io())
                .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<Health> list = Arrays.asList(response.getObjectToType(Health[].class).clone());
                                listMutableLiveData.postValue(list);
                                break;
                            default:
                                listMutableLiveData.postValue(null);
                                showMessage(response.getMessage());
                                break;
                        }
                    }

                    @Override
                    public void onError(@NotNull Throwable e) {
                        Log.d(TAG, "onError: " + e.getMessage());
                    }
                });
    }

    public void getHealth() {

        service.getServer().getHealth(sessionManager.getUser().getUid())
                .subscribeOn(Schedulers.io())
                .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<Health> list = Arrays.asList(response.getObjectToType(Health[].class).clone());
                                listMutableLiveData.postValue(list);
                                break;
                            default:
                                listMutableLiveData.postValue(null);
                                showMessage(response.getMessage());
                                break;
                        }
                    }

                    @Override
                    public void onError(@NotNull Throwable e) {
                        Log.d(TAG, "onError: " + e.getMessage());
                    }
                });
    }

    public LiveData<List<Exercise>> getExerData() {
        return exerData;
    }

    public void getExer() {

        service.getServer().getExercisePlan().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<Exercise> dietPlans = Arrays.asList(response.getObjectToType(Exercise[].class).clone());
                                exerData.postValue(dietPlans);
                                break;
                            default:
                                exerData.postValue(null);
                                showMessage(response.getMessage());
                                break;
                        }
                    }

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

                        exerData.postValue(null);
                    }
                });
    }
}