/*
 * PainelFerramentaAlvos.java
 *
 * Created on 26 de Dezembro de 2008, 22:59
 */

package ambienteGrafico.componentes;

import algoritmo.Alvo;
import algoritmo.ConjuntoDeAgentes;
import algoritmo.ConjuntoDeAlvos;
import algoritmo.EspacoDeBusca;
import ambienteGrafico.ferramentas.FerramentaInserirAlvo;
import ambienteGrafico.ferramentas.FerramentaSelecionarAlvo;
import java.awt.Cursor;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.DefaultListModel;
import javax.swing.JList;
/**
 *
 * @author  Marília
 */
public class PainelFerramentaAlvos extends MeuPainel {

    private Canvas canvas = null;
    
    //Possiveis Listeners para o canvas:
    private FerramentaInserirAlvo ferramentaInserirAlvo = null;
    private FerramentaSelecionarAlvo ferramentaSelecionarAlvo = null;

    private JList listaAlvos = null;
	private DefaultListModel listModel = null;

    /** Creates new form PainelFerramentaAlvos */
    public PainelFerramentaAlvos(Canvas canvas) {
        super.mudarLookAndFeel();
        initComponents();
        this.canvas = canvas;
        inicializarListeners();
    }
 
    private void inicializarListeners(){
        ferramentaInserirAlvo = new FerramentaInserirAlvo(this);
        ferramentaSelecionarAlvo = new FerramentaSelecionarAlvo(this);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new MeuPainel();
        bt_AlvoX = new MyJButton(); ;
        bt_AlvoX1 = new MyJButton(); ;
        sp_ListaAlvos = new javax.swing.JScrollPane();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        tf_ID = new javax.swing.JTextField();
        tf_Posicao = new javax.swing.JTextField();
        tf_Tamanho = new javax.swing.JTextField();
        tf_CorTextura = new javax.swing.JTextField();
        chb_AlvoAtivo = new javax.swing.JCheckBox();
        painelCores = new PainelCores(this);
        painelFerramentasDesenho = new PainelFerramentasDesenho(this);
        bt_AtribuirAlvoATodosAgentes = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(200, 500));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Objetos e Marcações", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 0))); // NOI18N
        jPanel1.setPreferredSize(new java.awt.Dimension(200, 100));

        bt_AlvoX.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/IconeAlvoX.png"))); // NOI18N
        bt_AlvoX.setToolTipText("Adicionar Alvo");
        bt_AlvoX.setIconTextGap(0);
        bt_AlvoX.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_AlvoXActionPerformed(evt);
            }
        });

        bt_AlvoX1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/IconeSelecionar.gif"))); // NOI18N
        bt_AlvoX1.setToolTipText("Selecionar Alvo");
        bt_AlvoX1.setIconTextGap(0);
        bt_AlvoX1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_AlvoX1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(bt_AlvoX1, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                    .addComponent(bt_AlvoX, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 28, Short.MAX_VALUE))
                .addContainerGap(156, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(bt_AlvoX, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(bt_AlvoX1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        listModel = new DefaultListModel();
        listaAlvos = new JList(listModel);
        listaAlvos.setSelectedIndex(0);
        listaAlvos.setVisibleRowCount(10);
        listaAlvos.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        //listaAlvos.setBounds(new java.awt.Rectangle(809,377,157,206));
        listaAlvos.addMouseListener(new ListaDeAlvosListener());
        sp_ListaAlvos.getViewport().setView(listaAlvos);
        //Adicionar os alvos default
        listModel.addElement("Alvo 0");

        jLabel1.setForeground(new java.awt.Color(1, 1, 1));
        jLabel1.setText("Lista de Alvos");

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Configurações do Alvo", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 0))); // NOI18N

        jLabel2.setText("ID:");

        jLabel3.setText("Tamanho:");

        jLabel4.setText("Posicao:");

        jLabel5.setText("Cor/Textura:");

        tf_ID.setText("0");

        tf_Posicao.setText("(0,0)");

        tf_Tamanho.setText("(0,0)");

        tf_CorTextura.setText("(0,0,0,0)");

        chb_AlvoAtivo.setSelected(true);
        chb_AlvoAtivo.setText("Alvo Ativo");
        chb_AlvoAtivo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chb_AlvoAtivoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(chb_AlvoAtivo, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tf_CorTextura, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
                            .addComponent(tf_Tamanho, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
                            .addComponent(tf_Posicao, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
                            .addComponent(tf_ID, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tf_ID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(tf_Posicao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(tf_Tamanho, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(tf_CorTextura, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(chb_AlvoAtivo)
                .addContainerGap())
        );

        painelCores.setPreferredSize(new java.awt.Dimension(200, 90));

        javax.swing.GroupLayout painelCoresLayout = new javax.swing.GroupLayout(painelCores);
        painelCores.setLayout(painelCoresLayout);
        painelCoresLayout.setHorizontalGroup(
            painelCoresLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 200, Short.MAX_VALUE)
        );
        painelCoresLayout.setVerticalGroup(
            painelCoresLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 90, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout painelFerramentasDesenhoLayout = new javax.swing.GroupLayout(painelFerramentasDesenho);
        painelFerramentasDesenho.setLayout(painelFerramentasDesenhoLayout);
        painelFerramentasDesenhoLayout.setHorizontalGroup(
            painelFerramentasDesenhoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 200, Short.MAX_VALUE)
        );
        painelFerramentasDesenhoLayout.setVerticalGroup(
            painelFerramentasDesenhoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 120, Short.MAX_VALUE)
        );

        bt_AtribuirAlvoATodosAgentes.setText("Atribuir Alvo");
        bt_AtribuirAlvoATodosAgentes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_AtribuirAlvoATodosAgentesActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(painelCores, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bt_AtribuirAlvoATodosAgentes)
                .addContainerGap(29, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(sp_ListaAlvos, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(painelFerramentasDesenho, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(painelFerramentasDesenho, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(painelCores, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(bt_AtribuirAlvoATodosAgentes))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(sp_ListaAlvos, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(71, 71, 71))
        );
    }// </editor-fold>//GEN-END:initComponents

private void bt_AlvoXActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_AlvoXActionPerformed
    
    double[] tamanhoPadraoAlvoX = {16,16};
    ferramentaInserirAlvo.setTamanhoAlvo(tamanhoPadraoAlvoX);
    ferramentaInserirAlvo.setCorTexturaAlvo(new java.awt.Color(255,255,255,150));//Cor padrao
    ferramentaInserirAlvo.setEmParaleloComFerramentasDeDesenho(false);
    try {
        Image img = ImageIO.read(getClass().getResource("/imagens/ImagemAlvoX.png"));
        Cursor cursor = Toolkit.getDefaultToolkit().createCustomCursor(img, new Point(16, 16),"AlvoX");
        ferramentaInserirAlvo.setCursor(cursor);
    } catch (IOException ex) {
        ex.printStackTrace();//
    }
    this.canvas.mudarFerramenta(ferramentaInserirAlvo);
}//GEN-LAST:event_bt_AlvoXActionPerformed

private void bt_AlvoX1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_AlvoX1ActionPerformed
    this.canvas.mudarFerramenta(ferramentaSelecionarAlvo);
}//GEN-LAST:event_bt_AlvoX1ActionPerformed

private void chb_AlvoAtivoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chb_AlvoAtivoActionPerformed
    if (!tf_ID.getText().equalsIgnoreCase("")) {

        EspacoDeBusca espacoDeBusca = EspacoDeBusca.getInstance();
        ConjuntoDeAlvos conjuntoDeAlvos = espacoDeBusca.getAlvos();
        int agenteId = Integer.parseInt(tf_ID.getText());
        conjuntoDeAlvos.get(agenteId).setAtivo(chb_AlvoAtivo.isSelected());

    }
}//GEN-LAST:event_chb_AlvoAtivoActionPerformed

private void bt_AtribuirAlvoATodosAgentesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_AtribuirAlvoATodosAgentesActionPerformed
            int indice = listaAlvos.getSelectedIndex();
            EspacoDeBusca espacoDeBusca = EspacoDeBusca.getInstance();
            ConjuntoDeAlvos alvos = espacoDeBusca.getAlvos();
            Alvo alvo = alvos.get(indice);
            alvo.setAtivo(true);
            chb_AlvoAtivo.setSelected(alvo.isAtivo());
            ConjuntoDeAgentes agentes = espacoDeBusca.getAgentes();
            for(int i=0;i<agentes.getNumAgentes();i++){
                agentes.get(i).estabelecerAlvo(alvo);
            }

}//GEN-LAST:event_bt_AtribuirAlvoATodosAgentesActionPerformed

     public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                JFrame frame =  new JFrame();
                PainelFerramentaAlvos panel = new PainelFerramentaAlvos(null);
                panel.setPreferredSize(new java.awt.Dimension(200,300));
                panel.setVisible(true);
                frame.setContentPane(panel);
                frame.setVisible(true);
                frame.setSize(new java.awt.Dimension(200,300));
                frame.pack();
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt_AlvoX;
    private javax.swing.JButton bt_AlvoX1;
    private javax.swing.JButton bt_AtribuirAlvoATodosAgentes;
    private javax.swing.JCheckBox chb_AlvoAtivo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel painelCores;
    private javax.swing.JPanel painelFerramentasDesenho;
    private javax.swing.JScrollPane sp_ListaAlvos;
    private javax.swing.JTextField tf_CorTextura;
    private javax.swing.JTextField tf_ID;
    private javax.swing.JTextField tf_Posicao;
    private javax.swing.JTextField tf_Tamanho;
    // End of variables declaration//GEN-END:variables

    public JTextField getTf_CorTextura() {
        return tf_CorTextura;
    }

    public JTextField getTf_ID() {
        return tf_ID;
    }

    public JTextField getTf_Posicao() {
        return tf_Posicao;
    }

    public JTextField getTf_Tamanho() {
        return tf_Tamanho;
    }

    public JCheckBox getChb_AlvoAtivo() {
        return chb_AlvoAtivo;
    }

    public DefaultListModel getListModel() {
        return listModel;
    }

    public JList getListaAlvos() {
        return listaAlvos;
    }

    public FerramentaInserirAlvo getFerramentaInserirAlvo() {
        return ferramentaInserirAlvo;
    }

    public FerramentaSelecionarAlvo getFerramentaSelecionarAlvo() {
        return ferramentaSelecionarAlvo;
    }

    public Canvas getCanvas() {
        return canvas;
    }

    public JPanel getPainelCores() {
        return painelCores;
    }

    /***********CLASSE INTERNA**********/
	class ListaDeAlvosListener implements java.awt.event.MouseListener, java.awt.event.ActionListener{

        @Override
        public void mouseClicked(MouseEvent e) {
            int indice = listaAlvos.getSelectedIndex();
            System.out.println("Alvo selecionado:"+indice);
            EspacoDeBusca espacoDeBusca = EspacoDeBusca.getInstance();
            ConjuntoDeAlvos alvos = espacoDeBusca.getAlvos();
            Alvo alvo = alvos.get(indice);
            tf_ID.setText(alvo.getId()+"");
            tf_Posicao.setText("("+alvo.getPosicao()[0]+","+alvo.getPosicao()[1]+")");
            tf_Tamanho.setText("("+alvo.getTamanho()[0]+","+alvo.getTamanho()[1]+")");
            tf_CorTextura.setText("("+alvo.getCorTextura().getRed()+","+
                                      alvo.getCorTextura().getGreen()+","+
                                      alvo.getCorTextura().getBlue()+","+
                                      alvo.getCorTextura().getAlpha()+")");
            chb_AlvoAtivo.setSelected(alvo.isAtivo());
        }

        @Override
        public void mousePressed(MouseEvent e) {
            
        }

        @Override
        public void mouseReleased(MouseEvent e) {
           
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            
        }

        @Override
        public void mouseExited(MouseEvent e) {
            
        }

        @Override
        public void actionPerformed(ActionEvent e) {
           
        }

    }
    
}
