package fr.iutfbleau.dev45.wamster.ex2choix;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.graphics.Path;

public class Dessin extends View {
    private Forme choix;
    private final Paint pinceau;

    public Dessin(Context context, AttributeSet attrs){
        super(context, attrs);
        this.pinceau = new Paint();
        this.pinceau.setStyle(Paint.Style.STROKE);
        this.pinceau.setStrokeWidth(20.0f);
        this.pinceau.setColor(0xff000000);
        this.choix = Forme.DEFAULT;
    }

    public void setForme(Forme forme){
        this.choix = forme;
        this.invalidate();
    }

    @Override
    public void onDraw(Canvas canvas){
        super.onDraw(canvas);
        float largeur = getWidth();
        float hauteur = getHeight();
        float r = largeur/4;
        float mx = largeur/2;
        float my = hauteur/2;

        switch (this.choix){
            case CARRE:
                canvas.drawRect(mx-r, my-r, mx+r, my+r, this.pinceau);
                break;
            case ROND:
                canvas.drawCircle(largeur/2, hauteur/2, largeur/4, this.pinceau);
                break;
            case TRIANGLE:
                Path triangle = new Path();
                triangle.moveTo(mx,my-r);
                triangle.lineTo(mx+r, my+r);
                triangle.lineTo(mx-r, my+r);
                triangle.close();
                canvas.drawPath(triangle, this.pinceau);
                break;
            case CROIX:
                float l=2*r/3;
                Path croix = new Path();
                croix.moveTo(mx-l/2,my-r);
                croix.rLineTo(l,0);
                croix.rLineTo(0,l);
                croix.rLineTo(l,0);
                croix.rLineTo(0,l);
                croix.rLineTo(-l,0);
                croix.rLineTo(0,l);
                croix.rLineTo(-l,0);
                croix.rLineTo(0,-l);
                croix.rLineTo(-l,0);
                croix.rLineTo(0,-l);
                croix.rLineTo(l,0);
                croix.rLineTo(0,-l);
                croix.close();
                canvas.drawPath(croix, this.pinceau);
                break;
        }
    }
}
