package org.ntlab.acanthus_client.views.paint;

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

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

import org.ntlab.acanthus_client.Acanthus;
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> mPageLength = new MutableLiveData<>();
    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);
    private ArrayList<Double>positionLocalx = new ArrayList<>();
    private ArrayList<Double>positionLocaly = new ArrayList<>();
    private ArrayList<Integer>localstrokeNo = new ArrayList<>();
    private int cnt=0;
    private float fsx=0;
    private float fsy=0;
    //-----------------------------------------------------------------
    // getter
    public MutableLiveData<Collection<Stroke>> getStroke() {
        return this.mStrokes;
    }
    public void deleteStroke() {
        this.mStrokes.getValue().clear();
    }

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

    public LiveData<Integer> getPageLength() {
        return this.mPageLength;
    }

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

    public PaintModelContainer getPaintModelContainer() {
        return paintModelContainer;
    }

    //-----------------------------------------------------------------
    //-----------------------------------------------------------------
    //  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(float x,float y) {
        paintModelContainer.getPaintConnectionModel().addLocalStroke(mStrokeNo);
        fsx=x;
        fsy=y;
        cnt=1;
    }
    public void addPositionRequest(float x, float y) {
//        Log.d("mStrokeNo", mStrokeNo.getValue().toString());
                paintModelContainer.getPaintConnectionModel().addPosition(cnt,x,y,mStrokeNo.getValue());
                //positionLocalx.add((double)x);
                //positionLocaly.add((double)y);
        cnt++;
    }
    public void addPositionRequestfirst() {
        paintModelContainer.getPaintConnectionModel().addPosition(0,fsx,fsy,mStrokeNo.getValue());
        paintModelContainer.getPaintConnectionModel().addPosition(cnt,-1,-1,mStrokeNo.getValue());
        //Log.d("x", positionLocalx.toString());
        //Log.d("y", positionLocaly.toString());
        //positionLocalx.clear();
        //positionLocaly.clear();
    }

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

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

    //-----------------------------------------------------------------
    //
    public void updatePageNo(MotionEvent event) {
        paintModelContainer.getPageOperationModel().updatePageNo(event, mPageNo);
    }

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

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

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