package mef2;

import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class Marco extends JFrame {

    public JPanel panel;
    public JTextArea area;
    public JTextArea area2;
    public JTextArea mapa;
    public JTextField fila;
    Metodos obMetodos = new Metodos();

    public Marco() {
        setSize(700, 400);
        setLocationRelativeTo(null);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setTitle("Validacin Cadena C++");

        componentes();

    }

    private void componentes() {
        panel();
        etiquetas();
        botones();
        texto();
    }

    private void panel() {
        panel = new JPanel();
        panel.setLayout(null);
        this.getContentPane().add(panel);
    }

    private void etiquetas() {
        JLabel etiqueta = new JLabel("Ingrese la cadena de C++ para validar:", SwingConstants.CENTER);
        //etiqueta.setOpaque(true);
        etiqueta.setBounds(10, 10, 350, 30);
        etiqueta.setFont(new Font("Bell MT", 1, 20));
        //etiqueta.setBackground(Color.WHITE);
        //etiqueta.setForeground(Color.BLACK);
        etiqueta.setForeground(Color.BLACK);
        panel.add(etiqueta);
        
        /*
        JLabel fondo = new JLabel(new ImageIcon("futuro.png"));
        fondo.setBounds(0, 0, 700, 400);
        panel.add(fondo);
        */
    }

    private void botones() {
        JButton boton = new JButton("Calificar Cadena");
        boton.setBackground(Color.WHITE);
        boton.setForeground(Color.BLACK);
        boton.setBounds(250, 50, 180, 30);
        boton.setFont(new Font("Bell MT", 0, 20));
        panel.add(boton);

        ActionListener accion = new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                fila.setText("");
                area.setText("");
                area2.setText("");
                mapa.setText("");

                String Cadena = fila.getText();
                int Signo = 0;

                for (int i = 0; i < Cadena.length(); i++) {
                    String sub = Cadena.substring(i, i + 1);
                    if ("=".equals(sub)) {
                        Signo++;
                    }
                }

                if ("".equals(Cadena) || ";".equals(Cadena) || "=".equals(Cadena)) {
                    fila.setText("");
                    area.setText("");
                    area2.setText("");
                    mapa.setText("Cadena invalida! \n Por favor, digite otra cadena.");

                } else if (Signo > 1) {
                    fila.setText("");
                    area.setText("");
                    area2.setText("");
                    mapa.setText("Exceso de signos igual '='! \n Por favor, digite otra cadena.");

                } else {
                    String[] Caracteres = Cadena.replaceAll(" ", ";").split("[\\ \\;\\+\\-\\=\\*\\/]");

                    String constantes = "";
                    String variables = "";

                    for (int j = 0; j < Caracteres.length; j++) {
                        String temporal = Caracteres[j];
                        boolean Resultado = obMetodos.Constante(temporal);

                        if (Resultado == true) {
                            constantes = constantes + Caracteres[j] + " ";
                            constantes = constantes + "\n";

                        } else {
                            variables = variables + Caracteres[j] + " ";
                            variables = variables + "\n";
                        }
                    }

                    String Resultado1 = obMetodos.Variables_repetidas(variables);
                    String Resultado2 = obMetodos.Constantes_repetidas(constantes);
                    area.setText(Resultado1);
                    area2.setText(Resultado2);
                }
            }
        };

        boton.addActionListener(accion);

    }

    private void texto() {
        fila = new JTextField("", 300);
        area = new JTextArea();
        area2 = new JTextArea();
        mapa = new JTextArea();

        fila.setBounds(360, 15, 310, 20);
        area.setBounds(270, 90, 410, 130);
        area2.setBounds(270, 230, 410, 130);
        mapa.setBounds(10, 90, 250, 270);

        panel.add(fila);
        panel.add(area);
        panel.add(area2);
        panel.add(mapa);
    }

}
