import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class gui extends Applet implements ActionListener
{
   Button button1, button2, button3;
   boolean flag2;

   finalresult window1;
//   stepresult window2;

   int no = 4;

   Label space1, space2, space3, space4, space5;
   Label space6, space7, space8, space9, space10; 
   Label space11, space12, space13, space14, space15;
   Label fill1, fill2, fill3, fill4, fill5, fill6, fill7;

   public void init()
   {
      setLayout(new GridLayout(5, 5));

      space1 = new Label();
      add(space1);
      space2 = new Label();
      add(space2);
      space3 =  new Label();
      add(space3);
      space4 = new Label();
      add(space4);
      space5 = new Label();
      add(space5);

      fill1 = new Label();
      add(fill1);
      button1 = new Button("Stepwise");
      add(button1);
      button1.setEnabled(false);
      fill2 = new Label();
      add(fill2);
      button2 = new Button("Final result");
      add(button2);
      fill3 = new Label();
      add(fill3);

      space6 = new Label();
      add(space6);
      space7 = new Label();
      add(space7);
      space8 = new Label();
      add(space8);
      space9 = new Label();
      add(space9);
      space10 = new Label();
      add(space10);

      fill4 = new Label();
      add(fill4);
      fill5 = new Label();
      add(fill5);
      button3 = new Button("Close");
      add(button3);
      fill6 = new Label();
      add(fill6);
      fill7 = new Label();
      add(fill7);

      space11 = new Label();
      add(space11);
      space12 = new Label();
      add(space12);
      space13 = new Label();
      add(space13);
      space14 = new Label();
      add(space14);
      space15 = new Label();
      add(space15);

      button1.addActionListener(this);
      button2.addActionListener(this);
      button3.addActionListener(this);
      window1 = new finalresult("Final Result", no);
      window1.resize(400, 500);

//      window2 = new stepresult("Stepwise result");
//      window2.resize(300, 300);
   }

   public void paint(Graphics g)
   { 
      if (flag2 == true)
      {
         window1.show();
      }
   }

   public void actionPerformed(ActionEvent e)
   {
      if (e.getSource() == button2)
      {
         flag2 = true;
      }

      if (e.getSource() == button3)
      {
         String main_pt = "p2";
         JMPF obj = new JMPF(1, 21);
         int main_pno = obj.LocatePort(main_pt, true);
         int status = obj.Send(main_pno, "close");
         obj.ClosePorts();
         window1.closing();
         System.exit(1);
      }

      repaint();
   }
}

class finalresult extends Frame
{
   Northpanel panel1;
   Centerpanel panel2;
   Southpanel panel3;
   int no;
   int choice;

   finalresult(String title, int n)
   {
      super(title);
      no = n;

      panel1 = new Northpanel(this, no);
      add("North",panel1);

      panel2 = new Centerpanel(this, no);
      add("Center", panel2);

      panel3 = new Southpanel(this, panel2);
      add("South",panel3);
   }

   public int getChoice()
   {
      choice = panel1.getSelectedChoice();
      return(choice);
   }

   public Centerpanel getcenterpanel()
   {
      return(panel2);
   }

   public void closing()
   {
      panel2.CloseAll();
   }
}

class Northpanel extends Panel implements ItemListener
{
   Choice choice1;
   int choice = 0;
   int no;
   TextField text1;
   finalresult base;
   Centerpanel panelc;

   Northpanel(finalresult base1, int n)
   {
      base = base1;
      no = n;
      Label label1 = new Label("     ");
      add(label1);
      choice1 = new Choice();
      for (int i = 1; i <= no; i++)
      {
         choice1.add("Selection" + " " + String.valueOf(i));
      }
      add(choice1);
      choice1.addItemListener(this);

      Label label2 = new Label("   ");
      add(label2);
		text1 = new TextField(10);
		text1.setEditable(false);
		add(text1);
   }

   public void itemStateChanged(ItemEvent e)
   {
      if (e.getItemSelectable() == choice1)
      {
         String str = ((Choice)e.getItemSelectable()).getSelectedItem();
         StringTokenizer st = new StringTokenizer(str);
         String s = st.nextToken();
         choice = Integer.parseInt(st.nextToken());
         text1.setText(str);
         panelc = base.getcenterpanel();
         panelc.prev.setEnabled(false);
         panelc.next.setEnabled(false);
         panelc.repaint();
//docalculate(choice);
      }
   }

   public int getSelectedChoice()
   {
      return(choice);
   } 
}

class Southpanel extends Panel implements ActionListener
{
	Button ret;
   finalresult base;

   Centerpanel panelc;

   Southpanel(finalresult base1, Centerpanel panel)
   {
      base = base1;
      panelc = panel;

      ret = new Button(" Return ");
      add(ret);
      ret.addActionListener(this);
   }

   public void actionPerformed(ActionEvent e)
   {
/*
      if (e.getSource() == showbut)
      {
         int status = obj.Send(main_pno, "showbut");
         int choice = base.getChoice();
         docalculate(choice);
         System.out.println("Sp.. Choice is : " + choice);
      }
*/
      if (e.getSource() == ret)
      {
         JMPF obj = new JMPF(1, 100);;
         String main_pt = "p2";
         int main_pno;
         main_pno = obj.LocatePort(main_pt, true);
         int status = obj.Send(main_pno, "return");
         System.out.println("The status of sending stop is : " + status);
         obj.ClosePorts();
         base.dispose();
      }
   }
}

class Centerpanel extends Panel implements ActionListener
{
   int no;
   int choice;
   int arr[][];
   int total = 1;
   int cnt = 0;
   boolean flag1, flag2;
   finalresult base;

   int count;

   JMPF obj;
   String main_pt = "p2";
   int main_pno;
   int ptno;

   Button prev, next;

   Centerpanel(finalresult base1, int n)
   {
      base = base1;

      obj = new JMPF(1, 200);
      main_pno = obj.LocatePort(main_pt, true);
      ptno = obj.CreatePort("gui");

      no = n;
      for (int i = 1; i < no-1; i++)
         total = total + total * i;
      arr = new int[total][no];

      prev = new Button("Previous");
      add(prev);
      prev.addActionListener(this);

      next = new Button("  next  ");
      add(next);
      next.addActionListener(this);
   }

   public void actionPerformed(ActionEvent e)
   {
      flag1 = false;
      flag2 = false;

      if (e.getSource() == prev)
      {
         if (cnt <= 0)
         {
            flag1 = false;
         }
         else
            flag1 = true;
      }
      if (e.getSource() == next)
      {
         if (cnt >= total)
         {
            flag2 = false;
         }
         else
            flag2 = true;
      }
      repaint();
   }

   public void paint(Graphics g)
   {
      choice = base.getChoice();
      docalculate(g, choice);
/*
      if (flag1 = true)
      {
         drawChessBoard(g);
      }
      if (flag2 == true)
      {
         drawChessBoard(g);
      }
*/
   }

   public void docalculate(Graphics g, int choiceno)
   {
      for (int i = 0; i < total; i++)
         for (int j = 0; j < no; j++)
            arr[i][j] = 0;
      Vector vector = new Vector(1,1);
      int status = obj.Send(main_pno, "continue");
      status = obj.Send(main_pno, choiceno);
      System.out.println("The status of sending choice is : " + choiceno);
      while(true)
      {
         String str = obj.ReceiveString(ptno);
         if ("false".equals(str))
            break;
         else
            vector.addElement(str);
      }
      System.out.println("Vector is " + vector);
      StringTokenizer st;

      int cnt = 0;

      for (int i = 0; i < vector.size(); i++)
      {
         st = new StringTokenizer((String)vector.elementAt(i));
         String s;
         for (int j = 0; j < no; j++)
            s = st.nextToken(); 
         s = st.nextToken();
         if ("true".equals(s))
         {
            st = new StringTokenizer((String)vector.elementAt(i));
            for (int j = 0; j < no; j++)
            {
               s = st.nextToken();
               arr[cnt][j] = (int)Integer.parseInt(s);
            }
            cnt = cnt + 1;
         }
      }
      drawChessBoard(g);
   }

   public void CloseAll()
   {
      obj.ClosePorts();
   }

   public void drawChessBoard(Graphics g)
   {
      int flagb, flagw;
      int x1, y1, width, height;

      width = 50;
      height = 50;
      x1 = 50;
      y1 = 50;
      for (int i = 1; i <= no; i++)
      {
         x1 = x1 + height;
         y1 = 50;
         for (int j = 1; j <= no; j++)
         {
            y1 = y1 + width;
            flagb = i % 2;
            flagw = j % 2;
            if (flagb != 0)
            {
               if (flagw != 0)
                  g.fillRect(x1, y1, width, height);
               else
                  g.drawRect(x1, y1, width, height);
            }
            else
            {
               if (flagw != 0)
                  g.drawRect(x1, y1, width, height);
               else
                  g.fillRect(x1, y1, width, height);
            }
         }
      }

      count = 0;

      for (int i = 0; i < total; i++)
         for (int j = 0; j < no; j++)
            if (arr[i][j] != 0)
               count = count + 1;

      System.out.println("The count is ********" + count);

      int x;
      int y = 50;
      for (int i = 0; i < count/no; i++)
      {
         for (int j = 0; j < no; j++)
         {
            x = arr[i][j] * 50 + 50;
            y = y + 50;
            g.drawString("Q", x+25, y+25);
            System.out.println("Centre panel ." + j + " .. " + arr[i][j]);
         }
      }
   }
}

/*
class dialogbox extends Frame implements ActionListener
{
   Button ok;

   dialogbox(String title)
   {
      super(title);
      setLayout(new FlowLayout());
      repaint();
      ok = new Button(" Ok ");
      add(ok);
      ok.addActionListener(this);
   }

   public void actionPerformed(ActionEvent e)
   {
      if (e.getSource() == ok)
      {
         this.hide();
      }
   }

   public void paint(Graphics g)
   {
      g.drawString("No combination satisfies for this column", 10, 10);
      ok = new Button(" Ok ");
      add(ok);
      ok.addActionListener(this);
   }
}

class buttondialog extends Dialog
{
   Button ok;

   buttondialog(Frame hostframe, String title, boolean dModal)
   {
      super(hostframe, title, dModal);
      resize(100, 100);
      setLayout(new FlowLayout());
      ok = new Button("OK");
      add(ok);
      ok.addActionListener((ActionListener)this);
   }

   public void actionPerformed(ActionEvent e)
   {
      if (e.getSource() == ok)
      {
         hide();
      }
   }
}
*/

