package org.ntlab.acanthus_client.views.main_menu_ui.mypage.model.Invited;

import androidx.lifecycle.MutableLiveData;

import org.ntlab.acanthus_client.Acanthus;
import org.ntlab.acanthus_client.entities.Work;
import org.ntlab.acanthus_client.resources.accounts.InvitedRest;
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;
import retrofit2.converter.scalars.ScalarsConverterFactory;

//-----------------------------------------------------------------
//　招待されているかの問い合わせAPI
public class InvitedConnectionModel {

    private Retrofit retrofit;
    private Acanthus acanthus;
    //-----------------------------------------------------------------
    private Boolean isInvited;

    //-----------------------------------------------------------------
    // setter
    private void setIsInvited(Boolean isInvited) {
        this.isInvited = isInvited;
    }

    //-----------------------------------------------------------------
    //-----------------------------------------------------------------
    //
    public InvitedConnectionModel(Acanthus acanthus) {
        init(acanthus);
    }

    //-----------------------------------------------------------------
    // 招待確認
    public void checkInvited(MutableLiveData<Boolean> mIsInvited) {
        final InvitedRest invitedRest = retrofit.create(InvitedRest.class);
        int dummyId = 2;

        Call<Boolean> call = invitedRest.checkAccountInvited(dummyId, acanthus.getAid());
        call.enqueue(new Callback<Boolean>() {
            @Override
            public void onResponse(Call<Boolean> call, Response<Boolean> response) {
                if (response.isSuccessful())
                    mIsInvited.setValue(response.body());
            }

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

            }
        });

    }

    //-----------------------------------------------------------------
    // 招待されているリストの取得API
    public void getAccountInvitedList(){

    }


    //-----------------------------------------------------------------
    // 招待承認
    public void acceptInvite() {
        final WorkRest workRest = retrofit.create(WorkRest.class);
        int dummy = 1111;


        // 作品への参加
        // 自身のID, token, 該当作品ID
        // ToDo: Aidを外部から取得する必要あり？ -> AccountのInviteMapから任意の
        // ToDo: getAidを招待一覧画面で更新してから取得すれば変更しなくてよさそう



        Call<Work> call = workRest.updateWork(acanthus.getPreferenceUid(), acanthus.getPreferenceToken(), dummy);
        call.enqueue(new Callback<Work>() {
            @Override
            public void onResponse(Call<Work> call, Response<Work> response) {
            }

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

            }
        });
    }

    //-----------------------------------------------------------------
    //-----------------------------------------------------------------
    // init
    private void init(Acanthus acanthus) {
        this.acanthus = acanthus;
        this.retrofit = new Retrofit.Builder()
                .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/acanthus/")
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(JacksonConverterFactory.create())
                .build();
    }

    //-----------------------------------------------------------------
}
