package com.noa.realtime;

import android.view.MotionEvent;
import android.view.View;

import java.util.HashMap;
import java.util.Random;

/**
 * Created by Noa on 30/10/2017.
 */

public class TouchListener implements View.OnTouchListener {

    private MyView view;

    public TouchListener(MyView view) {
        this.view = view;
    }

    @Override
    public boolean onTouch(View view, MotionEvent event) {


        int action = event.getAction();

        switch (action) {

            case MotionEvent.ACTION_DOWN:

                Circle circle;
                boolean create = true;

                for(String uuid : this.view.getManager().getCircles().keySet()) {

                    circle = this.view.getManager().getCircles().get(uuid);

                    if (circle.getR() * circle.getR() > ( (circle.getX() - event.getX()) * (circle.getX() - event.getX()) + (circle.getY() - event.getY()) * (circle.getY() - event.getY())) ) {
                        create = false;
                        this.view.getManager().removeCircle(uuid);
                        break;
                    }
                }

                if (create) {

                    float x = event.getX();
                    float y = event.getY();
                    int[] color = new int[] {new Random().nextInt(255), new Random().nextInt(255), new Random().nextInt(255), 255};

                    this.view.getManager().addCircle(x, y, color);
                }


                return true;
            default:
                return true;
        }
    }
}
