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

import android.preference.SwitchPreference;
import android.view.MotionEvent;
import android.view.VelocityTracker;

import androidx.lifecycle.MutableLiveData;

import org.ntlab.acanthus_client.Acanthus;

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

public class PageOperationModel {
    //-----------------------------------------------------------------
    // スワイプの向き
    public enum SwipeDirectionType {
        LEFT, RIGHT
    }

    //-----------------------------------------------------------------
    //-----------------------------------------------------------------
    private Acanthus acanthus;
    private VelocityTracker velocityTracker;

    //-----------------------------------------------------------------
    //-----------------------------------------------------------------
    public PageOperationModel(Acanthus acanthus) {
        this.acanthus = acanthus;
    }

    //-----------------------------------------------------------------
    //-----------------------------------------------------------------
    public void updatePageNo(MotionEvent event, MutableLiveData<Integer> mPageNo) {
        if (getVelocityDirection(event).equals(SwipeDirectionType.RIGHT)) incrementPageNo(mPageNo);
        if (getVelocityDirection(event).equals(SwipeDirectionType.LEFT)) decrementPageNo(mPageNo);
    }

    //-----------------------------------------------------------------
    //-----------------------------------------------------------------
    //
    public void incrementPageNo(MutableLiveData<Integer> mPageNo) {
//        if(acanthus.getEditingPageNo())
        Integer editingNo = acanthus.getEditingPageNo();

        acanthus.setEditingPageNo(editingNo + 1);
        mPageNo.setValue(acanthus.getEditingPageNo());
    }

    //-----------------------------------------------------------------s
    //
    private void decrementPageNo(MutableLiveData<Integer> mPageNo) {
        Integer editingNo = acanthus.getEditingPageNo();
        if (0 < editingNo ) {
            acanthus.setEditingPageNo(acanthus.getEditingPageNo() - 1);
            mPageNo.setValue(acanthus.getEditingPageNo());
        }
    }


    //-----------------------------------------------------------------
    // 操作からスワイプの向きを取得する
    private SwipeDirectionType getVelocityDirection(MotionEvent event) {
        if (velocityTracker == null) velocityTracker = VelocityTracker.obtain();
        else velocityTracker.clear();

        velocityTracker.addMovement(event);
        velocityTracker.computeCurrentVelocity(1000);

        if (velocityTracker.getXVelocity() < 0) return SwipeDirectionType.LEFT;
        else return SwipeDirectionType.RIGHT;
    }

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

}
