//last one

import java.util.Date;
import java.awt.*;
import java.lang.Math;
import java.net.*;
import java.applet.Applet;

public class pizza  extends NoFlickerApplet
{
  static final int NUMGIFS = 8;
  private Button Restart, Send;
  private int who_focus = 0;		//which pic was grabbed last
  private ingredients images[]; 
  private Image image_pizza;		//Image array
  private ListOfIngredients list;
  private Label totalLabel;
  private Choice size, thick, special;
  private double BasicPrice[][] = {{7.99,8.49,8.99}, {8.99,9.49,9.99}, {9.99,10.49,10.99}};
  private TextField Name, Address, Phone, email;
  private Label name, address, phone, e_mail;
  private int sleepTime=50;
  private Font font1, font2, font3;

public void init()
  {
    images=new ingredients[NUMGIFS];
    Image image1 = null;
    ingredients i;
    for(int j=0; j<=NUMGIFS-1; j++)
      {    i = new ingredients();
      images[j] = i;      }

    image1 = getImage(getDocumentBase(), "GreenOlive.gif");
    images[0].set("Green Olive", image1, 25, 20, 20);
    image1 = getImage(getDocumentBase(), "Mushroom.gif");
    images[1].set("Mushrooms", image1, 30, 170, 20);
    image1 = getImage(getDocumentBase(), "GreenPepper.gif");
    images[2].set("Green Peppers", image1, 20, 320, 20);
    image1 = getImage(getDocumentBase(), "Garlic.gif");
    images[3].set("Garlic", image1, 25, 500, 20);
    image1 = getImage(getDocumentBase(), "Onion.gif");
    images[4].set("Onion", image1, 25, 90, 55);
    image1 = getImage(getDocumentBase(), "Tomato1.gif");
    images[5].set("Tomato", image1, 15, 240, 55);
    image1 = getImage(getDocumentBase(), "Beef.gif");
    images[6].set("Beef", image1, 35, 390, 55);
    image1 = getImage(getDocumentBase(), "Cheese.gif");
    images[7].set("Double Cheese", image1, 10, 540, 55);
    image_pizza  = getImage(getDocumentBase(), "Pizza.gif");

    list = new ListOfIngredients(10, false, NUMGIFS);
    setLayout(null);
    size = new Choice();
    thick = new Choice();
    Restart = new Button("Restart");
    Send= new Button("Send");
    special = new Choice();
    font1 = new Font("TimesRoman", Font.ITALIC + Font.BOLD, 18); 
    font2 = new Font("TimesRoman", Font.BOLD, 14);
    font3 = new Font("TimesRoman", Font.ITALIC + Font.BOLD, 12); 

    Name = new TextField();
    Address = new TextField();
    Phone = new TextField();
    email= new TextField();
    
    name= new Label("Name: ");
    address = new Label("Address: ");
    phone = new Label("Phone Number: ");
    e_mail = new Label("email Address: ");
    name.setFont(font2);
    address.setFont(font2);
    phone.setFont(font2);
    e_mail.setFont(font2);
    

    add(Name);
    add(name);
    add(Address);
    add(address);
    add(Phone);
    add(phone);
    add(email);
    add(e_mail);

    Name.reshape(430, 370, 100, 30);
    Address.reshape(430, 400, 300, 30);
    Phone.reshape(430, 440, 120, 27);
    email.reshape(430, 470, 120, 30);

    name.reshape(320, 370, 50, 20);
    address.reshape(320, 400, 55, 20);
    phone.reshape(320, 440, 100, 20);
    e_mail.reshape(320, 470, 100, 20);
    
   

    size.addItem("Small");
    size.addItem("Medium");
    size.addItem("Large");
    thick.addItem("Thin");
    thick.addItem("Normal");
    thick.addItem("Thick");
    special.addItem("Custom");
    special.addItem("Veggie");
    special.addItem("Meat Cravers");

   
    totalLabel = new Label("Total                 = $" +  Double.toString(list.getTotal()/100 + BasicPrice[size.getSelectedIndex()][thick.getSelectedIndex()]));    
    
    totalLabel.setFont(font2);
    add(size);
    size.reshape(525, 290, 100, 20);
    add(thick);
    thick.reshape(650, 290, 100, 20);
    list.setFont(font2);
    add(list);
    list.reshape(60, 240, 150, 150);
    add(totalLabel);
    totalLabel.reshape(60, 380, 200, 20);
    add(Restart);
    Restart.reshape(150, 460, 60, 35);
    add(Send);
    Send.reshape(60, 460, 60, 35);
    add(special);
    special.reshape(570, 187, 100, 20);
    
    Send.setFont(font3);
    Restart.setFont(font3);
    special.setFont(font3);
    thick.setFont(font3);
    size.setFont(font3);
}


  public void paint(Graphics g) 
  {
    g.drawImage(image_pizza, 300, 150, this);
    g.setFont(font3);
    for(int i=0;i<=NUMGIFS-1;i++)		//draw them all
    {
      images[i].drawImage(g, this);
      images[i].drawString(g);
    }
    g.setFont(font1);
    g.drawString("Drag ingredients onto the Pizza:", 15, 173);
    
}

public void RestartPizza()
  {
    list.Restart();
    for(int j=0; j<=NUMGIFS-1; j++)
      images[j].reset();
    size.select(0);
    thick.select(0);
    special.select(0);
    repaint();
    printTotal();
  }
    

public void calcShort(int x, int y){
    who_focus = 1;
    int distance[] = new int[NUMGIFS];
    Math spud = null;
    for(int i = 0; i <= NUMGIFS-1; i++)
      distance[i] = (int) spud.sqrt(((images[i].getx()-x) * (images[i].getx()-x)) + ((images[i].gety()-y) * (images[i].gety()-y)));  
    for(int i = 0; i <= NUMGIFS-1; i++)
      if( distance[i] < distance[who_focus] )
	who_focus = i;
    if(distance[who_focus]>50)
      who_focus=-1;

}
  
  public boolean mouseDown(Event evt, int x, int y) {
    calcShort(x, y);
    return true;
  }

  public boolean mouseDrag(Event evt, int x, int y) {
    if(who_focus<0)
      return true;
    images[who_focus].setx(x);
    images[who_focus].sety(y);
    showStatus(x+"  "+y);

    repaint();
    

    return true;
  }

  public boolean mouseUp(Event evt, int x, int y) 
  {
    if(who_focus<0)
      return true;
    if(x>=300 && x<=495 && y>=165 && y<=260)
      list.addIngredient(who_focus, images[who_focus].getString(), images[who_focus].getPrice());
      else
	{
	  list.delIngredient(who_focus, images[who_focus].getPrice());
	  images[who_focus].reset();
	  repaint();
	}
    printTotal();
    return true;
  }
  public void animate(int x, int y, int imageNum)
  {
    try
	  {
	    Thread.sleep(sleepTime);
	  }
	catch(InterruptedException e)
	  {
	    showStatus(e.toString());
	  }
	images[imageNum].setx(x);
	images[imageNum].sety(y);
	Graphics g = getGraphics();
	update(g);
  }
  

  public void specialPizza()
  {
    if(special.getSelectedIndex() == 0)
      return;
    list.Restart();
    for(int j=0; j<=NUMGIFS-1; j++)
     images[j].reset();
     repaint();
    printTotal();    
   
    if(special.getSelectedIndex() == 1)
      {
	animate(31, 49, 0);
	animate(187, 44, 1);
	animate(336, 48, 2);
	animate(36, 64, 0);
	animate(208, 62, 1);
	animate(341, 62, 2);
	animate(59, 77, 0);
	animate(204, 73, 1);
	animate(346, 67, 2);
	animate(70, 92, 0);
	animate(218, 89, 1);
	animate(347, 85, 2);
	animate(126, 92, 0);
	animate(224, 88, 1);
	animate(350, 100, 2);
	animate(171, 94, 0);
	animate(254, 115, 1);
	animate(357, 119, 2);
	animate(206, 97, 0);
	animate(257, 117, 1);
	animate(366, 135, 2);
	animate(245, 136, 0);
	animate(277, 120, 1);
	animate(376, 167, 2);
	animate(250, 160, 0);
	animate(274, 124, 1);
	animate(380, 178, 2);
	animate(284, 164, 0);
	animate(281, 129, 1);
	animate(386, 205, 2);
	animate(308, 190, 0);
	animate(398, 173, 1);
	animate(406, 239, 2);
	animate(347, 198, 0);
	animate(429, 188, 1);
	animate(405, 254, 2);
	
	list.addIngredient(0, images[0].getString(), images[0].getPrice());
	list.addIngredient(1, images[1].getString(), images[1].getPrice());      
	list.addIngredient(2, images[2].getString(), images[2].getPrice());
        printTotal();
	}
    if(special.getSelectedIndex() == 2)
	{
	animate(400, 74, 6);
 	animate(499, 49, 3);
	animate(120, 81, 4);
	animate(400, 91, 6);
	animate(498, 60, 3);
	animate(160, 85, 4);
	animate(405, 111, 6);
	animate(480, 82, 3);
	animate(171, 93, 4);
	animate(401, 117, 6);
	animate(478, 81, 3);
	animate(192, 91, 4);
	animate(404, 133, 6);
	animate(477, 95, 3);
	animate(217, 110, 4);
	animate(400, 144, 6);
	animate(437, 122, 3);
	animate(222, 116, 4);
	animate(400, 176, 6);
	animate(447, 173, 3);
	animate(240, 132, 4);
	animate(412, 232, 6);
	animate(460, 195, 3);
	animate(241, 136, 4);
	animate(407, 252, 6);
	animate(256, 141, 4);
	animate(291, 147, 4);
	animate(303, 171, 4);
	animate(327, 179, 4);
	animate(353, 190, 4);

        list.addIngredient(6, images[6].getString(), images[6].getPrice());
        list.addIngredient(3, images[3].getString(), images[3].getPrice());      
        list.addIngredient(4, images[4].getString(), images[4].getPrice());
	printTotal();

}


  }
  public boolean action(Event evt, Object o)
  {
    if(evt.target instanceof Choice)
      if(evt.target == size || evt.target == thick)
	printTotal(); 
      else 
	specialPizza();
    else
      if(evt.target == Restart)
	RestartPizza();
    else
      if(evt.target == Send)
	send();
    return true;
  }
  
  public void printTotal()
  {
     totalLabel.setText("Total                = $"+Double.toString(list.getTotal()/100+BasicPrice[size.getSelectedIndex()][thick.getSelectedIndex()]));
  }

  public boolean check()
  {
    if(Name.getText().length() < 3)
      {
	showStatus("Please fill in your Name correctly!");
	return false;
      }
    if(Address.getText().length() == 0)
      {
	showStatus("Please fill in your Address correctly!");
	return false;
      }
    if(Phone.getText().length() < 7)
      {
	showStatus("Please fill in your Phone correctly!");
	return false;
      }
  return true;
  
  }
  
protected void send() 
  {
    if (!check())
      return;
    String[] headers = new String[2];
    Date d = new Date();
    headers[0] = email.getText();
    headers[1] = (d.toString());
    String sizeString = new
      String(size.getItem(size.getSelectedIndex()));
    String thickString = new
      String(thick.getItem(thick.getSelectedIndex()));

    StringBuffer Buffer = new StringBuffer();
    boolean first=true;
    for(int i=0; i<=NUMGIFS-1; i++)
      if(list.IsIn(i))
	if(!first)
	  Buffer.append(" -- " + images[i].getString());
	else
	  {
	    Buffer.append(images[i].getString());
	    first=false;
	  } 
    String IngredientsString = new String(Buffer.toString());
    String Minutes = new String();
    if(d.getMinutes() < 10)
      Minutes = "0"+ Integer.toString(d.getMinutes());
    else 
      Minutes = Integer.toString(d.getMinutes());
    
    String Message = new String("\nTime: " + d.getHours() + ":" +
				Minutes + "\n\n" +
				"Name: " + Name.getText() + "\n" +
				"Address: " + Address.getText() + "\n"
				+ "Phone Number " + Phone.getText() +
				"\n\n" + sizeString + ", " +
				thickString + "\n\n" + "Ingredients: " +
				IngredientsString + "\nTotal: $" +
				Double.toString(list.getTotal()/100+BasicPrice[size.getSelectedIndex()][thick.getSelectedIndex()]) );

    MailSender ms = new MailSender(headers, Message, this);
    ms.start();
      
      }
}
class ingredients 
{
  double price;
  private Image Picture;
  private int xcoordinate, ycoordinate;
  private String name;
  private int defaultx, defaulty, xtext, ytext;

  public ingredients()
  {
    xcoordinate = 0;
    ycoordinate =0;
    Picture = null;
  }
  
  public void set (String aname, Image Picture1, double price1, int x, int y)
  {
    name=aname;
    Picture=Picture1;
    price=price1;
    xcoordinate=x;
    ycoordinate=y;
    defaultx=x;
    defaulty=y;
    xtext= defaultx+40;
    ytext= defaulty+20;
}
  
  public void setx(int x)
  {
    xcoordinate=x;
  }

  public void sety(int y)
  {
    ycoordinate=y;
  }

  public int getx()
  {
    return xcoordinate;
  }
  
  public int gety()
  {
    return ycoordinate;
  }
  
  public double getPrice()
  {
    return price;
  }

  public void drawImage(Graphics g, Applet O)
  {
   if(Picture != null)
    {
      if(xcoordinate != defaultx || ycoordinate != defaulty)
	g.drawImage(Picture, defaultx, defaulty, O);
      g.drawImage(Picture, xcoordinate, ycoordinate, O);    

    }
   
  }

public void reset()
  {
    xcoordinate=defaultx;
    ycoordinate=defaulty;
  }

public void drawString(Graphics g)
  {
    g.setColor(Color.black);
    g.drawString(name +" "+Double.toString(price)+"c", xtext, ytext);
  } 
public String getString()
  {
    return name;
  }

}

class ListOfIngredients extends List
{
  private int index[];
  private int maxnum;
  private double Total;

  public ListOfIngredients(int rows, boolean Selection, int max)
  {
   super(rows, Selection);
   maxnum = max;
   Total = 0;
   index = new int[maxnum];
   for (int i=0; iindex[who])
	    index[j]--;
	 }
	index[who]=-1;
	Total = Total - price;
      }
  }
  
  public double getTotal()
  {
    return Total;
  }
  
}