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

import android.util.Log;

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() {
        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) {
            }

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

            }
        });

    }

    //-----------------------------------------------------------------
    // ページ削除
    public void deletePage() {
        final PagesRest pagesRest = retrofit.create(PagesRest.class);

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

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

            }
        });
    }

    //-----------------------------------------------------------------
    // ページサイズ取得
    public void getPageSize(MutableLiveData<Integer> pageSize) {
        final PagesRest pagesRest = retrofit.create(PagesRest.class);

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

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

            }
        });
    }

    //-----------------------------------------------------------------
    // ページ情報を取得するリクエスト
    public void getPage(MutableLiveData<Integer> pageId) {
        final PagesRest pagesRest = retrofit.create(PagesRest.class);

        Call<Page> call = pagesRest.getPage(acanthus.getAid(), acanthus.getEditingPageNo());
        call.enqueue(new Callback<Page>() {
            @Override
            public void onResponse(Call<Page> call, Response<Page> response) {
                if (response.isSuccessful()) {
                    //acanthus.setEditingPageId(response.body());
                    // ページ番号とページIDを取得する
                    acanthus.setEditingPageNo(response.body().getPageNo());
                    acanthus.setEditingPageId(response.body().getPid());

                    Log.d("pid", acanthus.getEditingPageId().toString());
                }
            }

            @Override
            public void onFailure(Call<Page> call, Throwable t) {
                Log.d("failure", "omg");
            }
        });
    }

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