package org.ntlab.acanthus_client.views.main_menu_ui.home;

import android.util.Log;
import android.widget.EditText;

import org.ntlab.acanthus_client.Acanthus;
import org.ntlab.acanthus_client.entities.Animation;
import org.ntlab.acanthus_client.entities.AnimationJson;
import org.ntlab.acanthus_client.resources.HelloWorldRest;
import org.ntlab.acanthus_client.resources.accounts.LoginRest;
import org.ntlab.acanthus_client.resources.gallery.GalleryRest;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import java.io.IOException;
import java.util.Collection;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class HomeViewModel extends ViewModel {
    private EditText editEmailTextView;
    private MutableLiveData<Collection<AnimationJson>> animationMutableLiveData;
    private MutableLiveData<String> mText;
    private HomeModel homeModel;
    private Animation animation;


    public HomeViewModel() {
        mText = new MutableLiveData<>();
        homeModel= new HomeModel();
        homeModel.Gethelloworld(mText);
        animationMutableLiveData = new MutableLiveData<>();
        animation = new Animation();
    }

    public LiveData<String> getText() {
        return mText;
    }

    public LiveData<Collection<AnimationJson>> getAnimationJson(){return this.animationMutableLiveData;}



    public void setCurrentAnimation(int aid, Acanthus acanthus) throws IOException {

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/acanthus/")
                .addConverterFactory(JacksonConverterFactory.create())
                .build();
        final GalleryRest galleryRest = retrofit.create(GalleryRest.class);
        Call<Animation> call = galleryRest.getAnimationInformation(aid);
//        Response<Animation> response = call.execute();
//        animation = response.body();
//        acanthus.setCurrentAnimation(animation);
        call.enqueue(new Callback<Animation>() {
            @Override
            public void onResponse(Call<Animation> call, Response<Animation> response) {
                if(response.isSuccessful()){
                    animation = response.body();
                    acanthus.setCurrentAnimation(animation);
                    Log.d(acanthus.getCurrentAnimation().getName(), toString());
                }
            }

            @Override
            public void onFailure(Call<Animation> call, Throwable t) {
                Log.d("aa", t.toString());

            }
        });

    }



    public void updateGallery(Acanthus acanthus){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/acanthus/")
                .addConverterFactory(JacksonConverterFactory.create())
                .build();
        final GalleryRest galleryRest = retrofit.create(GalleryRest.class);

        Call<Collection<AnimationJson>> call = galleryRest.getGallery(null);

        call.enqueue(new Callback<Collection<AnimationJson>>() {
            @Override
            public void onResponse(Call<Collection<AnimationJson>> call, Response<Collection<AnimationJson>> response) {
                if (response.isSuccessful()){
                    Collection<AnimationJson> animList = response.body();
                    animationMutableLiveData.setValue(animList);
                    acanthus.setAnimationJsonList(animList);
                }
            }

            @Override
            public void onFailure(Call<Collection<AnimationJson>> call, Throwable t) {

            }
        });

    }

    public Collection<AnimationJson> getGallery(){ return animationMutableLiveData.getValue(); }

}