package org.ntlab.acanthus_client.views.paint.models;

import androidx.lifecycle.MutableLiveData;

import org.ntlab.acanthus_client.Acanthus;
import org.ntlab.acanthus_client.entities.Page;
import org.ntlab.acanthus_client.resources.gallery.PagesRest;
import org.ntlab.acanthus_client.resources.gallery.StrokesRest;

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

//-----------------------------------------------------------------
// ページ更新通信
public class PageConnectionModel {

    private Retrofit retrofit;
    private Acanthus acanthus;

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

    //-----------------------------------------------------------------
    //-----------------------------------------------------------------
    // init
    public 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();
    }

    //-----------------------------------------------------------------
    //-----------------------------------------------------------------
    // ページ追加API
    public void addPage(MutableLiveData<Integer> mPageNo) {
        final PagesRest pagesRest = retrofit.create(PagesRest.class);

        Call<Integer> call = pagesRest.addPage(acanthus.getAid());
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                if (response.isSuccessful()) mPageNo.setValue(response.body());
            }

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

            }
        });

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


}
