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

import android.preference.SwitchPreference;
import android.util.Log;
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 changeEditPageNo(MotionEvent event, MutableLiveData<Integer> mCurPageNo, Integer pageSize) {
        mCurPageNo.setValue(acanthus.getEditingPageNo());

        //ページの枚数以上現在のページ番号を更新しない
        if (getVelocityDirection(event).equals(SwipeDirectionType.LEFT))
            incrementPageNo(mCurPageNo, pageSize);
        if (getVelocityDirection(event).equals(SwipeDirectionType.RIGHT))
            decrementPageNo(mCurPageNo);


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

    //-----------------------------------------------------------------
    //-----------------------------------------------------------------
    // ページを進める
    public void incrementPageNo(MutableLiveData<Integer> mPageNo, Integer pageSize) {
        Integer editingNo = acanthus.getEditingPageNo();

        if (editingNo < pageSize) {
            acanthus.setEditingPageNo(editingNo + 1);
            mPageNo.setValue(acanthus.getEditingPageNo());

            Log.d("pageNo", acanthus.getEditingPageNo().toString());
        }
    }

    //-----------------------------------------------------------------s
    // ページ番号を前に戻す
    public void decrementPageNo(MutableLiveData<Integer> mPageNo) {
        Integer editingNo = acanthus.getEditingPageNo();

        if (1 < editingNo) {
            acanthus.setEditingPageNo(acanthus.getEditingPageNo() - 1);
            mPageNo.setValue(acanthus.getEditingPageNo());

            Log.d("pageNo", acanthus.getEditingPageNo().toString());
        }
    }

    //-----------------------------------------------------------------
    // 操作からスワイプの向きを取得する
    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;
    }

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

}
