import java.awt.*;
import java.awt.event.*;
import java.io.*;

/*
 * az ATM automata limitmodosito ablaka 
 */
class LimitModositasAblak extends Frame 
                          implements ActionListener, 
                                     MouseListener,
                                     Runnable
{
   Button visszaButton, /* vissza az ATM automatahoz */
          kiveszButton, /* limit csokkentese */
          hozzaadButton, /* limit novelese */
          modositButton, /* a modositasok elvegzese */
          melyikButton;  /* a limit valtoztatasahoz kell */
   Label limitLabel; /* a Limit akt. erteke */
   Thread thread; /* a gyurut figyelo szal */
   String iButtonID = new String(""); /* a gyuru azonositoja */
   String ATMazonosito = new String(""); /* az ATM automata azonositoja */

   int Limit = Gyuru.eszkoz.limitKerdez(); /* a limit akt. erteke */

   public LimitModositasAblak(String azonosito, String ID) 
   {
      ATMazonosito = azonosito;
      iButtonID = ID;

      setLayout(new BorderLayout());
      
      setSize(300, 200);
      setResizable(false);
      setLocation(250,200);

      Panel gombPanel = new Panel();
      {
       gombPanel.setLayout(new FlowLayout());
       visszaButton = new Button("Vissza az ATM automat\u00e1hoz");
       {
        gombPanel.add(visszaButton);
        visszaButton.addActionListener(this);
       }
       add("South",gombPanel);
      }

      Panel szovegPanel = new Panel();
      {
       szovegPanel.setLayout(new BorderLayout());
       Panel penzPanel = new Panel();
       {
        penzPanel.setLayout(new FlowLayout());
        Panel limitPanel = new Panel();
        {
         limitPanel.setLayout(new BorderLayout());
         {
          limitPanel.add("North",new Label("Limit: "));
          limitLabel = new Label(Integer.toString(Limit));
          limitPanel.add("South",limitLabel);
         }
         penzPanel.add("Center",limitPanel);
        }
        kiveszButton = new Button("-");
        {
         penzPanel.add(kiveszButton);
         kiveszButton.addActionListener(this);
         kiveszButton.addMouseListener(this);
        }
        hozzaadButton = new Button("+");
        {
         penzPanel.add(hozzaadButton);
         hozzaadButton.addActionListener(this);
         hozzaadButton.addMouseListener(this);
        }
        szovegPanel.add("North",penzPanel);
       }
        Panel modositPanel = new Panel();
        {
         modositPanel.setLayout(new BorderLayout());
         modositButton = new Button("M\u00f3dos\u00edt\u00e1s!");
         {
          modositPanel.add("Center",modositButton);
          modositButton.addActionListener(this);
         }
         szovegPanel.add("South",modositPanel);
        }
       add("Center",szovegPanel);
      }
      thread = new Thread(this);
      thread.start();
   }

   public void run()
   {
      boolean haveiButton = !(Gyuru.eszkoz.eszkozJelen()&&
                              Gyuru.eszkoz.appletJelen());
      while (true)
      {
         try
         {
            Thread.sleep(250);
         }
         catch (InterruptedException e)
         {
         }

         try
         {
            if (haveiButton&& 
                !(Gyuru.eszkoz.eszkozJelen()&&
                  Gyuru.eszkoz.appletJelen()))
            {
               haveiButton = false;
               setTitle("Limit m\u00f3dos\u00edt\u00e1sa "+
                        "(nincs eszk\u00f6z jelen)");
               limitLabel.setVisible(false);
               kiveszButton.setEnabled(false);
               hozzaadButton.setEnabled(false);
               modositButton.setEnabled(false);
            }
            else if (!haveiButton&& 
                     (Gyuru.eszkoz.eszkozJelen()&&
                      Gyuru.eszkoz.appletJelen()))
            {
               haveiButton = true;
               if(!iButtonID.equals(Gyuru.eszkoz.getiButtonID()))
               {
                  Limit = Gyuru.eszkoz.limitKerdez();
                  limitLabel.setText(Integer.toString(Limit));
                  setTitle("Limit m\u00f3dos\u00edt\u00e1sa "+
                           "(eszk\u00f6z megv\u00e1ltozott)");
                  limitLabel.setVisible(true);
                  kiveszButton.setEnabled(true);
                  hozzaadButton.setEnabled(true);
                  modositButton.setEnabled(true);
               }
               else
               {
                  Limit = Gyuru.eszkoz.limitKerdez();
                  limitLabel.setText(Integer.toString(Limit));
                  setTitle("Limit m\u00f3dos\u00edt\u00e1sa "+
                           "(eszk\u00f6z el\u00e9rhet\u00f4)");
                  limitLabel.setVisible(true);
                  kiveszButton.setEnabled(true);
                  hozzaadButton.setEnabled(true);
                  modositButton.setEnabled(true);
               }
            }
         }
         catch (NullPointerException e)
         {
         }
         if(melyikButton==hozzaadButton)
         {
            /* Egyenleg novelese */
            Limit+=1;
            limitLabel.setText(Integer.toString(Limit));
         }
         else if(melyikButton==kiveszButton)
         {
            /* Egyenleg csokkentese */
            if(Limit>0)
            {
               Limit-=1;
               limitLabel.setText(Integer.toString(Limit));
            }
         }
      }
   }

   /* 
    * a modositasok lejegyzese (log) - TITKOSITANI KELL 
    */
   private void write()
   {
      try
      {
         String s;
         RandomAccessFile raf = new RandomAccessFile(new File("limit.txt"),"rw");
         raf.seek(raf.length());
         s = kodol(Gyuru.eszkoz.getiButtonID()+" $"+Limit+"\n");
         raf.writeBytes(s);
      }
      catch(IOException e)
      {
         System.out.println("\u00cdr\u00e1si hiba: limit.txt");
      }
      catch(SecurityException e)
      {
         System.out.println("Hozz\u00e1f\u00e9r\u00e9si hiba: limit.txt");
      }
   }

   /*
    * a logfile-ba kerulo adatok kodolasa (erdemes idotol fuggo kodolsat alkalmazni)
    */
   private String kodol(String s)
   {
      return(s);
   }

   public void mouseClicked(MouseEvent evt)
   {
   }

   public void mousePressed(MouseEvent evt)
   {
      if(evt.getSource()==hozzaadButton)
      {
         melyikButton=hozzaadButton;
      }
      else if(evt.getSource()==kiveszButton)
      {
         melyikButton=kiveszButton;
      }
   }

   public void mouseReleased(MouseEvent evt)
   {
      melyikButton=null;
   }

   public void mouseEntered(MouseEvent evt)
   {
   }

   public void mouseExited(MouseEvent evt)
   {
   }

   public void actionPerformed(ActionEvent evt)
   {
      if(evt.getSource()==visszaButton)
      {
         /* Vissza */
         dispose();
         thread.stop();
         Gyuru.atmablak = new ATMAblak();
         Gyuru.atmablak.show();
      }
      if(evt.getSource()==hozzaadButton)
      {
         /* Limit novelese */
         Limit+=1;
         limitLabel.setText(Integer.toString(Limit));
      }
      if(evt.getSource()==kiveszButton)
      {
         /* Limit csokkentese */
         if(Limit>0)
         {
            Limit-=1;
            limitLabel.setText(Integer.toString(Limit));
          }
       }
       if(evt.getSource()==modositButton)
       {
          /* A modositasok elvegzese */
          if(Gyuru.eszkoz.limitModosit(
               Gyuru.bank.jelszoKerdez(ATMazonosito, iButtonID),Limit))
          {
             write();
             dispose();
             thread.stop();
             Gyuru.atmablak = new ATMAblak();
             Gyuru.atmablak.show();
          }
          else
          {
             Limit = Gyuru.eszkoz.limitKerdez();
             limitLabel.setText(Integer.toString(Limit));
          }
       }
   }

}