package com.example.dimension;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;

public class ResizableRectangleView extends View {
    private float rectWidth = 400;
    private float rectHeight = 600;
    private float centerX, centerY;
    private boolean isRed = false; // Détermine la couleur actuelle
    private float aspectRatio;

    private Paint paint;
    private ScaleGestureDetector scaleDetector;
    private GestureDetector gestureDetector;

    private boolean scaleWidthOnly = false;
    private boolean scaleHeightOnly = false;
    private static final float EDGE_THRESHOLD = 100; // Seuil pour reconnaître les bords

    public ResizableRectangleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        paint.setColor(Color.BLUE);
        aspectRatio = rectWidth / rectHeight;

        scaleDetector = new ScaleGestureDetector(context, new ScaleGestureDetector.SimpleOnScaleGestureListener() {
            @Override
            public boolean onScale(ScaleGestureDetector detector) {
                float scaleFactorX = detector.getCurrentSpanX() / detector.getPreviousSpanX();
                float scaleFactorY = detector.getCurrentSpanY() / detector.getPreviousSpanY();

                if (isRed) {
                    float scaleFactor = Math.max(scaleFactorX, scaleFactorY);
                    rectWidth *= scaleFactor;
                    rectHeight = rectWidth / aspectRatio;
                } else {
                    if (scaleWidthOnly) {
                        rectWidth *= scaleFactorX;
                    } else if (scaleHeightOnly) {
                        rectHeight *= scaleFactorY;
                    } else {
                        rectWidth *= scaleFactorX;
                        rectHeight *= scaleFactorY;
                    }
                }

                invalidate();
                return true;
            }
        });

        gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onDoubleTap(MotionEvent e) {
                isRed = !isRed;
                paint.setColor(isRed ? Color.RED : Color.BLUE);
                invalidate();
                return true;
            }
        });
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        centerX = getWidth() / 2f;
        centerY = getHeight() / 2f;

        float left = centerX - rectWidth / 2;
        float top = centerY - rectHeight / 2;
        float right = centerX + rectWidth / 2;
        float bottom = centerY + rectHeight / 2;

        canvas.drawRect(left, top, right, bottom, paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                float touchX = event.getX();
                float touchY = event.getY();

                float left = centerX - rectWidth / 2;
                float top = centerY - rectHeight / 2;
                float right = centerX + rectWidth / 2;
                float bottom = centerY + rectHeight / 2;

                scaleWidthOnly = (touchX < left + EDGE_THRESHOLD || touchX > right - EDGE_THRESHOLD);
                scaleHeightOnly = (touchY < top + EDGE_THRESHOLD || touchY > bottom - EDGE_THRESHOLD);

                break;
        }

        scaleDetector.onTouchEvent(event);
        gestureDetector.onTouchEvent(event);
        return true;
    }
}
