package fr.iutfbleau.dev45.wamster.tp04ex01_pois;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Dessin extends View {
    private final Paint pinceau;
    private List<Doigt> listeDoigt;

    public Dessin(Context context, AttributeSet attrs){
        super(context, attrs);
        this.pinceau = new Paint();
        this.listeDoigt = new ArrayList<Doigt>();
    }

    @Override
    public void onDraw(Canvas canvas){
        super.onDraw(canvas);
        Log.v("test", String.valueOf(this.listeDoigt.size()));
        for (Doigt doigt : this.listeDoigt){
            if (doigt != null) {
                this.pinceau.setColor(doigt.getColor());
                Log.v("test", doigt.toString());
                Log.v("test", doigt.getX() + " " + doigt.getY() + " " + doigt.getColor());
                canvas.drawCircle(doigt.getX(), doigt.getY(), 100, this.pinceau);
            }
        }
    }

    public Doigt addDoigt(Doigt doigt, int id){
        while (this.listeDoigt.size() <= id){
            this.listeDoigt.add(null);
        }
        return this.listeDoigt.set(id, doigt);
    }

    public Doigt removeDoigt(int id){
        if (id < this.listeDoigt.size()){
            return this.listeDoigt.set(id, null);
        }
        return null;
    }

    public Doigt getDoigt(int index){
        return this.listeDoigt.get(index);
    }

    public static int createRandomColor(){
        Random rand = new Random();
        int r = rand.nextInt(256);
        int g = rand.nextInt(256);
        int b = rand.nextInt(256);
        return 0xFF000000 | (r << 16) | (g << 8) | b;
    }
}
