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

import android.util.Log;
import android.view.VelocityTracker;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.navigation.ActivityNavigator;

import org.ntlab.acanthus_client.Acanthus;
import org.ntlab.acanthus_client.entities.Animation;
import org.ntlab.acanthus_client.entities.Position;
import org.ntlab.acanthus_client.entities.Stroke;
import org.ntlab.acanthus_client.resources.gallery.GalleryRest;
import org.ntlab.acanthus_client.resources.gallery.LayersRest;
import org.ntlab.acanthus_client.resources.gallery.StrokesRest;

import java.lang.reflect.Array;
import java.net.Inet4Address;
import java.util.ArrayList;
import java.util.Collection;

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

//-----------------------------------------------------------------
// 描画通信
public class PaintConnectionModel {
    private Animation editedAnimation;
    private Retrofit retrofit;
    private Acanthus acanthus;

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

    //-----------------------------------------------------------------
    // setter
    public void setEditedAnimation(Animation editedAnimation) {
        this.editedAnimation = editedAnimation;
    }

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

    //-----------------------------------------------------------------
    // ローカルでのストロークの追加(POST)
    public void addLocalStroke(int mStrokeNo) {
        final StrokesRest strokesRest = retrofit.create(StrokesRest.class);

        Log.d("strokeNo:", "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^add----------------------");
        //-----------------------------------------------------------------
        // ストローク追加API
        Call<Integer> call = strokesRest.addStroke(
                acanthus.getAid(), acanthus.getEditingPageId(), 0, acanthus.getPreferenceUid(),mStrokeNo, acanthus.getPreferenceToken(),
                0, 0, 10);

        // strokeNoを更新
        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) {
                Log.d("POSTonFailure: ", "strokeNo");
            }
        });
    }

    //-----------------------------------------------------------------
    // ローカルでの筆跡追加(POST)
    public void addPosition(int i, double x, double y, int currentStrokeNo) {
        final StrokesRest strokesRest = retrofit.create(StrokesRest.class);

        //Log.d("strokeNo:", currentStrokeNo);
        Log.d("curPageId:", acanthus.getEditingPageId().toString());
        Log.d("curPageNo:", acanthus.getEditingPageNo().toString());

        //-----------------------------------------------------------------
        // 座標追加
        Call<Integer> call = strokesRest.addPositions(i, acanthus.getAid(), acanthus.getEditingPageId(), currentStrokeNo, x, y);
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {

            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {
                Log.d("POSTonFailure: ", "i:" + i + ",x:" + x + ",y:" + y);
            }
        });
    }

    //-----------------------------------------------------------------
    // 全strokeを取得する
    public void getStrokes(MutableLiveData<Collection<Stroke>> strokes) {
        final StrokesRest strokesRest = retrofit.create(StrokesRest.class);

        //-----------------------------------------------------------------
        // 筆跡追加API
        Call<Collection<Stroke>> call = strokesRest.getStrokes(acanthus.getAid(), acanthus.getEditingPageId(), 0);
        call.enqueue(new Callback<Collection<Stroke>>() {
            @Override
            public void onResponse(Call<Collection<Stroke>> call, Response<Collection<Stroke>> response) {
                if (response.isSuccessful()) strokes.setValue(response.body());
            }

            @Override
            public void onFailure(Call<Collection<Stroke>> call, Throwable t) {
                Log.d("a", "onFailure: ");
            }
        });

    }

    //-----------------------------------------------------------------
    // 現在のstrokeNoを取得する
    public void getStrokeNo(MutableLiveData<Integer> mStrokeNo) {
        final LayersRest layersRest = retrofit.create(LayersRest.class);

        //-----------------------------------------------------------------
        // 現在の筆跡番号の取得
        Call<Integer> call = layersRest.getStrokeSize(acanthus.getAid(), acanthus.getEditingPageId(), 0);
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                if (response.isSuccessful()) mStrokeNo.setValue(response.body());
            }

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

            }
        });
    }

    //-----------------------------------------------------------------
    // "Clear"ボタンによる描画の全消去 / サーバー側の筆跡全削除
    public void deleteStrokes() {
        final StrokesRest strokesRest = retrofit.create(StrokesRest.class);

        //-----------------------------------------------------------------
        // 筆跡をすべて削除する
        Call<String> call = strokesRest.deleteStrokes(acanthus.getAid(), acanthus.getEditingPageId(), acanthus.getPreferenceUid());
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
            }
        });
    }

    //-----------------------------------------------------------------

}