/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ambienteGrafico.ferramentas;

import algoritmo.Obstaculo;
import algoritmo.ConjuntoDeObstaculos;
import algoritmo.EspacoDeBusca;
import ambienteGrafico.componentes.Canvas;
import ambienteGrafico.componentes.PainelFerramentaObstaculos;
import auxiliar.Conversor;
import java.awt.Cursor;
import java.awt.event.MouseEvent;
import javax.swing.SwingUtilities;

/**
 *
 * @author Marília
 */
public class FerramentaSelecionarObstaculo extends Ferramenta{
    private Cursor cursor;
    private PainelFerramentaObstaculos painelFerramenta;
    private Canvas painelCanvas;

    public FerramentaSelecionarObstaculo(PainelFerramentaObstaculos painel) {
        this.cursor = new Cursor(Cursor.HAND_CURSOR);
        this.painelFerramenta = painel;
    }

    @Override
    public Cursor getCursor() {
        return this.cursor;
    }

    @Override
    public void mousePressed(MouseEvent e) {
        int posicaoXObstaculo = e.getX();
        int posicaoYObstaculo = e.getY();
        if (SwingUtilities.isLeftMouseButton(e)) {

            EspacoDeBusca espacoDeBusca = EspacoDeBusca.getInstance();
            ConjuntoDeObstaculos obstaculos = espacoDeBusca.getObstaculos();

            painelCanvas = (Canvas) e.getSource();

            int posicaoYObstaculoInvertida = Math.abs(posicaoYObstaculo - painelCanvas.getHeight());
            double[] posicao = Conversor.converterEscalaCanvasParaEscalaMetro(posicaoXObstaculo,
                                                              posicaoYObstaculoInvertida,
                                                              painelCanvas.getWidth(),
                                                              painelCanvas.getHeight(),
                                                              espacoDeBusca.getLimiteMaxCoordX(),
                                                              espacoDeBusca.getLimiteMaxCoordY());
            Obstaculo obstaculo = obstaculos.buscarObstaculo(posicao);
            if(obstaculo!=null){
                painelFerramenta.getTf_ID().setText(""+obstaculo.getId());
                painelFerramenta.getTf_Posicao().setText("("+(int)Math.round(obstaculo.getPosicao()[0])+","
                                                            +(int)Math.round(obstaculo.getPosicao()[1])+")");
                painelFerramenta.getTf_Tamanho().setText("("+obstaculo.getTamanho()[0]+","+obstaculo.getTamanho()[1]+")");
                painelFerramenta.getTf_RaioRepulsao().setText(""+obstaculo.getRaioAreaRepulsao());
                painelFerramenta.getChb_ObstaculoAtivo().setSelected(obstaculo.isAtivo());
                //Desenha a área de repulsão na tela
                painelCanvas.pintarAreaRepulsaoDoObstaculo(obstaculo,null);
            }
         }
        else{
            painelCanvas.mudarFerramenta(new FerramentaNula(null));
        }
    }

    @Override
    public void mouseDragged(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }
}