package org.ntlab.acanthus_client.views.paint;

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

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import org.jetbrains.annotations.NotNull;
import org.ntlab.acanthus_client.Acanthus;
import org.ntlab.acanthus_client.entities.Position;
import org.ntlab.acanthus_client.entities.Stroke;

import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


//-----------------------------------------------------------------
// 描画処理の仲介
public class PaintViewModel extends ViewModel implements Runnable {

    private PaintModelContainer paintModelContainer;

    private MutableLiveData<Integer> mPageNo = new MutableLiveData<>();
    private MutableLiveData<Integer> mStrokeNo = new MutableLiveData<>();
    private MutableLiveData<Collection<Stroke>> mStrokes = new MutableLiveData<>();
    private ScheduledThreadPoolExecutor thread = new ScheduledThreadPoolExecutor(1);

    //-----------------------------------------------------------------
    // getter
    public MutableLiveData<Collection<Stroke>> getStroke() {
        return this.mStrokes;
    }

    public LiveData<Integer> getStrokeNo() {
        return this.mStrokeNo;
    }

    public LiveData<Integer> getPageNo() {
        return this.mPageNo;
    }

    //-----------------------------------------------------------------
    //-----------------------------------------------------------------
    //  init
    public void init(Acanthus acanthus) {
        paintModelContainer = new PaintModelContainer(acanthus);
        paintModelContainer.getPaintConnectionModel().init(acanthus);
        paintModelContainer.getInvitesConnectionModel().init(acanthus);

        mStrokes.setValue(new ArrayList<>());
        mStrokes.getValue().add(new Stroke());

        thread.scheduleWithFixedDelay(this, 1000L, 100L, TimeUnit.MILLISECONDS);
    }

    //-----------------------------------------------------------------
    // 描画APIの通信リクエスト
    public void addLocalStrokeRequest() {
        paintModelContainer.getPaintConnectionModel().addLocalStroke(mStrokeNo);
    }

    public void addPositionRequest(float x, float y) {
        paintModelContainer.getPaintConnectionModel().addPosition(x, y, mStrokeNo);
    }

    //-----------------------------------------------------------------
    // 招待APIの通信リクエスト
    public void inviteNewUserRequest(Integer invitedUid) {
        paintModelContainer.getInvitesConnectionModel().inviteNewEditor(invitedUid);
    }

    //-----------------------------------------------------------------
    // 描画情報の全クリア通信リクエスト
    public void deleteStrokesRequest() {
        paintModelContainer.getPaintConnectionModel().deleteStrokes();
    }

    //-----------------------------------------------------------------
    // ページの追加通信リクエスト
    public void addPageRequest() {
        paintModelContainer.getPageConnectionModel().addPage(mPageNo);
    }

    //-----------------------------------------------------------------
    // 一定間隔でサーバー上の筆跡を取得する（GET）
    @Override
    public void run() {
//        Log.d("debug", "run: ");
        paintModelContainer.getPaintConnectionModel().getStrokes(mStrokes);
        paintModelContainer.getPaintConnectionModel().getStrokeNo(mStrokeNo);
    }

    //-----------------------------------------------------------------
    //
    public void stop() {
        thread.shutdown();
    }
    //-----------------------------------------------------------------
}