package fr.iutfbleau.dessin;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

public class Composant extends View {

    private final Paint rectangle;
    private Rect rect;
    private float[] coordCercle;
    private final Paint peintureCercle;
    private final Paint peintureTexte;
    private float[] coordTexte;
    private Bitmap image;
    private float[] coordImage;
    private Paint peintureImage;

    public Composant(Context c, AttributeSet s) {
        super(c, s);
        this.rect = new Rect(0, 0, this.getWidth(), this.getHeight());
        this.rectangle = new Paint();
        this.rectangle.setColor(0xff0000ff);
        this.rectangle.setStyle(Paint.Style.FILL_AND_STROKE);
        this.rectangle.setStrokeWidth(15.0f);
        this.coordCercle = new float[2];
        this.peintureCercle = new Paint();
        this.peintureCercle.setStrokeWidth(5f);
        this.peintureCercle.setColor(0xff7f00ff);
        this.peintureCercle.setStyle(Paint.Style.STROKE);
        this.peintureTexte = new Paint();
        this.peintureTexte.setFakeBoldText(true);
        this.peintureTexte.setTextSize(80f);
        this.peintureTexte.setTextAlign(Paint.Align.CENTER);
        this.coordTexte = new float[2];

        Resources res = this.getResources();

        this.image = BitmapFactory.decodeResource(res, R.drawable.cercles);

        this.coordImage = new float[2];
        this.peintureImage = new Paint();

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawRect(this.rect, this.rectangle);
        canvas.drawCircle(this.coordCercle[0], this.coordCercle[1], 200, this.peintureCercle);
        canvas.drawText("Ceci est un test.", this.coordTexte[0], this.coordTexte[1], this.peintureTexte);
        canvas.drawBitmap(this.image, this.coordImage[0], this.coordImage[1], this.peintureImage);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        this.rect = new Rect(0, 0, w/3*2, h/6);
        this.coordCercle[0] = 205;
        this.coordCercle[1] = h/6*2;
        this.coordTexte[0] = (this.rect.right - this.rect.left) / 2;
        this.coordTexte[1] = (this.rect.bottom - this.rect.top) / 2 + 40;
        this.coordImage[0] = 0;
        this.coordImage[1] = h/6*4;

    }

}
