/* CasinoUSM.java */
import java.awt.*;
import javax.swing.*;
import javax.swing.Timer;

import java.util.*;
import java.awt.event.*;
import javax.imageio.*;
import java.io.*;

public class CasinoUSM {  
   private static Image fondo;
   private static boolean modo;
   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() { 
         public void run() {
            
			// Alumnos fila de espera
			
        	
			ArrayList<Integer> alumnos = new ArrayList<Integer>();
			File Imagen_fondo = new File("fondo.png");
    		try
    		{
    			fondo = ImageIO.read(Imagen_fondo);
    		}
    		catch(IOException e)
    		{
    			System.out.println("Imagen no encontrada");
    		}
			
			Casino_Panel C_Panel = new Casino_Panel(fondo); 
            Casino_Frame frame = new Casino_Frame(); 
            
            
       	 
        	JMenuBar mb = new JMenuBar();
        	JMenu menu = new JMenu("Cambiar Modo");
        	JMenu menu2 = new JMenu("Agregar Aleatorio");
        	JMenu menu3 = new JMenu("Filas Dieta");
        	JButton diet1 = new JButton("1 Fila");
        	JButton diet2 = new JButton("2 Filas");
        	JButton Manual = new JButton("Manual");
        	Manual.addActionListener( new ActionListener ()
				{
					public void actionPerformed(ActionEvent event) 
					{
						if (!modo) {
						Manual.setText("Solo Automatico");
						modo = true;}
						else {Manual.setText("Manual");
						modo = false;}
					};
				});
        	
        	JButton BG2 = new JButton("Generador Masivo: 60-30-10");
        	JButton BG1 = new JButton("Generador Masivo: 70-20-10");
        	// Action listeners!!
        	menu.add(Manual);
        	menu2.add(BG1);
        	menu2.add(BG2);
        	menu3.add(diet1);
        	menu3.add(diet2);
        	mb.add( menu );
        	mb.add( menu2 );
        	mb.add( menu3 );
        	frame.setJMenuBar( mb );
        	frame.setTitle("CasinoUsm ELO329 1s2019");
            
           	
            
            
			frame.add(C_Panel, BorderLayout.CENTER);
			JPanel SPanel = new JPanel(); 
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            
            ImageIcon img_normal = new ImageIcon("bnormal.png");
            JButton BN = new JButton(img_normal);
            SPanel.add(BN);
          	BN.addActionListener( new ActionListener ()
    				{
    					public void actionPerformed(ActionEvent event) 
    					{
    						if(!modo) {
    						if (alumnos.size()<10) {
    						alumnos.add(0);}
    						C_Panel.repaint();}
    					};
    				});
          	ImageIcon img_veg = new ImageIcon("bveg.png");
          	JButton BV = new JButton(img_veg);
            SPanel.add(BV);
          	BV.addActionListener( new ActionListener ()
    				{
    					public void actionPerformed(ActionEvent event) 
    					{
    						if(!modo) {
    						if (alumnos.size()<10) {
    						alumnos.add(1);}
    						C_Panel.repaint();}
    					};
    				});
          	ImageIcon img_diet = new ImageIcon("bdiet.png");
          	JButton BD = new JButton(img_diet);
            SPanel.add(BD, BorderLayout.SOUTH);
          	BD.addActionListener( new ActionListener ()
    				{
    					public void actionPerformed(ActionEvent event) 
    					{
    						if(!modo) {
    						if (alumnos.size()<10) {
    						alumnos.add(2);}
    						C_Panel.repaint();}
    					};
    				});
			Fila_Espera Fila_Esp = new Fila_Espera(10,580,80, alumnos);
			C_Panel.addView(Fila_Esp);
            Generador_Masivo generador = new Generador_Masivo(Fila_Esp, 70,20,10);
            Generador_Masivo generador2 = new Generador_Masivo(Fila_Esp, 60,30,10);

            
            BG1.addActionListener( new ActionListener ()
            {
              public void actionPerformed(ActionEvent event)
              {

                if (!generador.isOn()&!generador2.isOn())
                {
                  System.out.println("Empieza prueba con porcentajes 70-20-10 (N-V-D)");
                  generador.setOn();
                  generador.genera();
                  C_Panel.repaint();
                }
              };
            });

            

            BG2.addActionListener( new ActionListener ()
            {
              public void actionPerformed(ActionEvent event)
              {
                if (!generador2.isOn()&!generador.isOn())
                {
                  System.out.println("Empieza prueba con porcentajes 60-30-10 (N-V-D)");
                  generador2.setOn();
                  generador2.genera();
                  C_Panel.repaint();
                }
              };
            });
			//Alumnos fila 1
			
			ArrayList<Integer> alumnos_fila1 = new ArrayList<Integer>();
			alumnos_fila1.add(3);
			alumnos_fila1.add(3);
			
			Fila Fila_1 = new Fila(570,200,80, alumnos_fila1);
			
			//Alumnos fila 2
			
			ArrayList<Integer> alumnos_fila2 = new ArrayList<Integer>();
			alumnos_fila2.add(3);
			alumnos_fila2.add(3);
			
			Fila Fila_2 = new Fila(810,200,80, alumnos_fila2);
			
			//Alumnos fila 3
			
			ArrayList<Integer> alumnos_fila3 = new ArrayList<Integer>();
			alumnos_fila3.add(3);
			alumnos_fila3.add(3);
			
			Fila Fila_3 = new Fila(570,400,80, alumnos_fila3);
			
			//Alumnos fila 4
			
			ArrayList<Integer> alumnos_fila4 = new ArrayList<Integer>();
			alumnos_fila4.add(3);
			alumnos_fila4.add(3);
			
			Fila Fila_4 = new Fila(810,400,80, alumnos_fila4);
			
			//Alumnos_Transicion
			
			ArrayList<Integer> Arreglo_transicion = new ArrayList<Integer>();
			
			Alumnos_transicion A_transc = new Alumnos_transicion(10, 580, Arreglo_transicion, Fila_1, Fila_2, Fila_3, Fila_4);
			
			
			
			C_Panel.addView(Fila_Esp);
			C_Panel.addView(Fila_1);
			C_Panel.addView(Fila_2);
			C_Panel.addView(Fila_3);
			C_Panel.addView(Fila_4);
			C_Panel.addView(A_transc);
			frame.add(SPanel, BorderLayout.SOUTH);
            frame.setVisible(true);
            Controlador Control = new Controlador(Fila_1, Fila_2, Fila_3, Fila_4, alumnos, A_transc, C_Panel,generador,generador2);
            diet1.addActionListener( new ActionListener ()
			{
				public void actionPerformed(ActionEvent event) 
				{
					Control.CambiarDieta(false);
				};
			});
        	diet2.addActionListener( new ActionListener ()
			{
				public void actionPerformed(ActionEvent event) 
				{
					Control.CambiarDieta(true);
				};
			});
            Control.manageCasino();
         } 
      });
   }
}
class Casino_Frame extends JFrame  {
   public Casino_Frame() {
      setTitle("CasinoUSM");
      setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
   }
   public static final int DEFAULT_WIDTH = 1024;
   public static final int DEFAULT_HEIGHT = 968;  
}
class Casino_Panel extends JPanel  { 
	public Casino_Panel(Image f) {  
		setFocusable(true);
		fondo = f;
		views = new ArrayList<View> ();
		
	}
	public void addView(View v) {  
		views.add(v);
		v.setPanel(this);
	}
	public void paintComponent(Graphics g){  
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D)g; 
		g2.drawImage(fondo, 0, 0, null);
		for (View v: views){     // has Graphics as parameter, but now an intance
			v.paint_view(g2);   // of a subclass of Graphics, instance of Graphics2D,
		}                      // is passed as argument.
	}
	private ArrayList<View> views;
	private Image fondo;

}