package com.example.gribouillage;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;


public class Gribouillage extends View {
    private Paint rectPaint;
    private Paint circlePaint;
    private Paint textPaint;
    private Bitmap image;
    private String text = "Ceci est un test";

    public Gribouillage(Context context, AttributeSet attrs) {
        super(context, attrs);

        // Peinture pour le rectangle bleu
        rectPaint = new Paint();
        rectPaint.setColor(Color.BLUE);
        rectPaint.setStyle(Paint.Style.FILL);

        // Peinture pour le cercle violet
        circlePaint = new Paint();
        circlePaint.setColor(Color.MAGENTA);
        circlePaint.setStyle(Paint.Style.STROKE);
        circlePaint.setStrokeWidth(5);

        // Peinture pour le texte
        textPaint = new Paint();
        textPaint.setColor(Color.BLACK);
        textPaint.setTextSize(50);
        textPaint.setFakeBoldText(true);
        textPaint.setTextAlign(Paint.Align.CENTER);

        // Charger l'image depuis l'URL
        image = BitmapFactory.decodeResource(getResources(), R.drawable.cercles);
        postInvalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int width = getWidth();
        int height = getHeight();
        int rectWidth = width / 2;
        int rectHeight = height / 6;
        int rectLeft = rectWidth / 2;
        int rectTop = rectHeight / 6;

        // Dessiner le rectangle bleu
        canvas.drawRect(rectLeft, rectTop, rectLeft + rectWidth, rectTop + rectHeight, rectPaint);

        // Dessiner le cercle violet
        float radius = Math.min(rectWidth, rectHeight) / 2 - 10;
        canvas.drawCircle(width / 2, height / 2, radius, circlePaint);

        // Dessiner le texte centré horizontalement
        Rect textBounds = new Rect();
        textPaint.getTextBounds(text, 0, text.length(), textBounds);
        float textX = rectWidth;
        float textY = rectTop + (rectHeight + textBounds.height()) / 2;
        canvas.drawText(text, textX, textY, textPaint);

        // Dessiner l'image si elle est chargée
        if (image != null) {
            int imgWidth = image.getWidth();
            int imgHeight = image.getHeight();
            int imgX = (width - imgWidth) / 2;
            int imgY = rectTop + rectHeight + 20; // Sous le rectangle
            canvas.drawBitmap(image, imgX, imgY, null);
        }
    }
}