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

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

import org.ntlab.acanthus_client.entities.AidJson;
import org.ntlab.acanthus_client.resources.accounts.WorkRest;

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

public class CreateViewModel extends ViewModel {

    private MutableLiveData<AidJson> aidJsonMutableLiveData;

    public CreateViewModel() {
        this.aidJsonMutableLiveData = new MutableLiveData<>();
    }

    public LiveData<AidJson> getAidJson() {
        return this.aidJsonMutableLiveData;
    }

    //新規作品作成APIの呼び出し
    public Integer startWorkCreate(Integer uid, String animationName, String token) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/acanthus/")
                .addConverterFactory(JacksonConverterFactory.create())
                .build();
        final WorkRest workRest = retrofit.create(WorkRest.class);

        final Integer[] aid = new Integer[1];

        //作品の新規作成
        Call<AidJson> call = workRest.createWork(uid, animationName, token);
        call.enqueue(new Callback<AidJson>() {
            @Override
            public void onResponse(Call<AidJson> call, Response<AidJson> response) {
                if (response.isSuccessful()) {
                    if (response.body() != null) {
                        //aid[0] = response.body().getAid();
                        aidJsonMutableLiveData.setValue(response.body());
                    }
                }
            }

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

            }
        });

        return aid[0];
    }
}
