package fr.iutfbleau.dev45.wamster.tp04ex01_pois;

import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class Evenement implements View.OnTouchListener{

    private Dessin dessin;

    public Evenement(Dessin dessin){
        this.dessin = dessin;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getActionMasked();
        int index = event.getActionIndex();
        int id = event.getPointerId(index);
        Log.v("test1", id+"");
        float x,y;

        switch (action) {
            case MotionEvent.ACTION_DOWN:
                x = event.getX();
                y = event.getY();
                this.dessin.addDoigt(new Doigt(x,y), id);
                break;

            case MotionEvent.ACTION_POINTER_DOWN:
                x = event.getX(index);
                y = event.getY(index);
                this.dessin.addDoigt(new Doigt(x, y), id);
                break;

            case MotionEvent.ACTION_MOVE:
                int i;
                for (i = 0; i < event.getPointerCount(); i++) {
                    int idPointer = event.getPointerId(i);
                    x = event.getX(i);
                    y = event.getY(i);
                    this.dessin.getDoigt(idPointer).updateCoordonnee(x,y);

                }
                break;

            case MotionEvent.ACTION_POINTER_UP:
                this.dessin.removeDoigt(id);
                break;

            case MotionEvent.ACTION_UP:
                this.dessin.removeDoigt(id);
                break;
            case MotionEvent.ACTION_CANCEL:
                this.dessin.removeDoigt(id);
                break;
        }
        this.dessin.invalidate();
        return true;
    }
}
