package com.example.grille;

import android.graphics.Color;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.GridLayout;

public class CellView extends View {
    private GestureDetector gestureDetector;

    public CellView(MainActivity context) {
        super(context);
        GridLayout.LayoutParams params = new GridLayout.LayoutParams();
        params.width = 250;
        params.height = 250;
        params.setMargins(10, 10, 10, 10);
        setLayoutParams(params);
        setBackgroundColor(Color.LTGRAY);

        gestureDetector = new GestureDetector(context, new GestureListener());

        setOnTouchListener((v, event) -> gestureDetector.onTouchEvent(event));
    }

    private class GestureListener extends GestureDetector.SimpleOnGestureListener {
        private long touchStart;

        @Override
        public boolean onDown(MotionEvent e) {
            touchStart = System.currentTimeMillis();
            return true;
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            setBackgroundColor(Color.BLUE);
            return true;
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            setBackgroundColor(Color.MAGENTA);
            return true;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            setBackgroundColor(Color.YELLOW);
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            setBackgroundColor(Color.LTGRAY);
            return true;
        }
    }
}