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

import org.ntlab.acanthus_client.entities.AidJson;
import org.ntlab.acanthus_client.entities.Animation;
import org.ntlab.acanthus_client.entities.WorkJson;
import org.ntlab.acanthus_client.resources.accounts.WorkRest;
import org.ntlab.acanthus_client.resources.gallery.GalleryRest;
import org.ntlab.acanthus_client.resources.gallery.InvitesRest;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;

public class EditViewModel extends ViewModel {

    private MutableLiveData<WorkJson> workJsonMutableLiveData;
    private MutableLiveData<AidJson> aidJsonMutableLiveData;

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

    //getter
    public LiveData<WorkJson> getWorkJson() {
        return this.workJsonMutableLiveData;
    }

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

    //新規作品作成APIの呼び出し
    public void startWorkEdit(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);

        //作品の新規作成
        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) {
                        aidJsonMutableLiveData.setValue(response.body());
                    }
                }
            }

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

            }
        });

    }

    //ユーザーの作成した作品の表示APIの呼び出し
    public void getWorkList(Integer uid, 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);

        //制作作品の表示
        Call<WorkJson> call = workRest.getWork(uid, token);
        call.enqueue(new Callback<WorkJson>() {
            @Override
            public void onResponse(Call<WorkJson> call, Response<WorkJson> response) {
                if (response.isSuccessful()) {
                    if (response.body() != null) {
                        workJsonMutableLiveData.setValue(response.body());
                    }
                }
            }

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

            }
        });
    }

//    //aidから特定の作品を取得するAPIの呼び出し
//    public Animation getAnimationInfo(Integer aid) {
//        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);
//
//        final Animation[] animation = {null};
//
//        //特定の作品の表示
//        Call<Animation> call = galleryRest.getAnimationInformation(aid);
//        call.enqueue(new Callback<Animation>() {
//            @Override
//            public void onResponse(Call<Animation> call, Response<Animation> response) {
//                if (response.isSuccessful()) {
//                    if (response.body() != null) {
//                        animation[0] = response.body();
//                    }
//                }
//            }
//
//            @Override
//            public void onFailure(Call<Animation> call, Throwable t) {
//
//            }
//        });
//
//        return animation[0];
//    }
//
//    //ユーザーの作品へ他ユーザーを招待するAPIの呼び出し
//    public void invitesEditor(Integer aid, String ownerUid, String invitedUid, String ownerToken) {
//        Retrofit retrofit = new Retrofit.Builder()
//                .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/acanthus/")
//                .addConverterFactory(JacksonConverterFactory.create())
//                .build();
//        final InvitesRest invitesRest = retrofit.create(InvitesRest.class);
//
//        //作品への招待
//        Call<String> call = invitesRest.addInvite(aid, ownerUid, invitedUid, ownerToken);
//        call.enqueue(new Callback<String>() {
//            @Override
//            public void onResponse(Call<String> call, Response<String> response) {
//                if (response.isSuccessful() && response.body() != null) {
//                }
//            }
//
//            @Override
//            public void onFailure(Call<String> call, Throwable t) {
//
//            }
//        });
//    }

}