// Copyright 2004 Bjorn Danielsson <java@dax.nu>
// $Id: JuestBookPanel.java 28 2004-11-21 13:23:51Z bd $

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.net.URL;
import java.net.URLConnection;
import Acme.JPM.Encoders.GifEncoder;

public class JuestBookPanel extends Panel
	implements ActionListener, ItemListener {

    public DoodleCanvas doodle;
    public boolean stopped;

    private java.applet.Applet myapplet;

    private String paramString;
    private int paramInt;
    private Color paramColor;

    private String uploadURL = "http://127.0.0.1/juestbook/upload.jsp";
    private String showURL = "http://127.0.0.1/juestbook/main.jsp";
    private int gifWidth = 640;
    private int gifHeight = 480;
    private volatile Thread uploadThread = null;

    private Button cmdReset;
    private Button cmdSend;
    private Choice selAntialias;
    private Choice selDebug;

    public JuestBookPanel() {
	this(null);
    }

    public JuestBookPanel(java.applet.Applet a) {
	myapplet = a;
	stopped = true;
    }

    private boolean parseParamString(String p) {
	if (myapplet != null) {
	    String val = myapplet.getParameter(p);
	    if (val != null) {
		paramString = val;
		return true;
	    }
	}
	return false;
    }

    private boolean parseParamInt(String p) {
	if (myapplet != null) {
	    String val = myapplet.getParameter(p);
	    if (val != null) {
		paramInt = Integer.parseInt(val);
		return true;
	    }
	}
	return false;
    }

    private boolean parseParamColor(String p) {
	if (myapplet != null) {
	    String val = myapplet.getParameter(p);
	    if (val != null) {
		if (val.length() > 0 && val.charAt(0) == '#') {
		    paramColor = new Color(Integer.parseInt(val.substring(1), 16));
		    return true;
		}
	    }
	}
	return false;
    }

    public void paint(Graphics g) {
	if (doodle == null) {
	    Dimension d = this.getSize();
	    g.setColor(Color.blue);
	    g.fillRect(0, 0, d.width, d.height);
	}
    }

    protected class Uploader extends Thread {
	Image img;

	public Uploader(Image img) {
	    this.img = img;
	}

	public void run() {
	    uploadThread = this;
	    if (!stopped) {
		doodle.setBusy("Bilden skickas...");
		Image imgExport = createImage(gifWidth, gifHeight);
		Graphics g = imgExport.getGraphics();
		int x = (gifWidth - img.getWidth(null))/2;
		int y = (gifHeight - img.getHeight(null))/2;
		g.setColor(Color.white);
		g.fillRect(0, 0, gifWidth, gifHeight);
		g.drawImage(img, x, y, null);
		try {
		    URL url = new URL(uploadURL);
		    URLConnection c = url.openConnection();
		    c.setDoOutput(true);
		    c.setRequestProperty("Content-Type", "application/octet-stream");
		    OutputStream s = c.getOutputStream();
		    GifEncoder gifenc = new GifEncoder(imgExport, s);
		    gifenc.encode();
		    s.close();
		    InputStream is = (InputStream) c.getContent();
		    if (myapplet != null) {
			myapplet.getAppletContext().showDocument(new URL(showURL));
		    }
		} catch (Exception e) {
		    System.out.println("Ouch! Exception in upload");
		    System.out.println(e);
		}
		doodle.setBusy(null);
		uploadThread = null;
	    }
	}
    }

    public void actionPerformed(ActionEvent e) {
	String what = e.getActionCommand();
	if (doodle != null) {
	    if (what.equals("Rensa")) {
		//
		// Rensa
		//
		doodle.reset();
	    }
	    if (what.equals("Skicka")) {
		//
		// Skicka
		//
		new Uploader(doodle.getImage()).start();
	    }
	}
    }

    public void itemStateChanged(ItemEvent e) {
	Object[] selected = e.getItemSelectable().getSelectedObjects();
	String what = (String) selected[0];
	boolean dopaint = false;
	if (doodle != null) {
	    //
	    // Antialiasing On/Off choice
	    //
	    if (what.equals("Antialiasing On") && doodle.antialiasing != true) {
		doodle.antialiasing = true;
		dopaint = true;
	    } else if (what.equals("Antialiasing Off") && doodle.antialiasing != false) {
		doodle.antialiasing = false;
		dopaint = true;
	    } else if (what.equals("Debugging On")) {
		doodle.debugGraphics = true;
		dopaint = true;
	    } else if (what.equals("Debugging Off")) {
		doodle.debugGraphics = false;
		dopaint = true;
	    }

	    if (dopaint) {
		doodle.repaint();
	    }
	}
    }

    private WindowAdapter mywindowlistener = new WindowAdapter() {
	    public void windowClosing(WindowEvent e) { 
		e.getWindow().dispose();
	    }
	};

    public Dimension getPreferredSize() {
	if (myapplet != null) {
	    Dimension size = myapplet.getSize();
	    return new Dimension(size.width, size.height);
	} else {
	    return super.getPreferredSize();
	}
    }

    public void init() {
	if (parseParamString("POSTURL")) {
	    uploadURL = paramString;
	}
	if (parseParamInt("GIFWIDTH")) {
	    gifWidth = paramInt;
	}
	if (parseParamInt("GIFHEIGHT")) {
	    gifHeight = paramInt;
	}
	if (parseParamString("SHOWURL")) {
	    showURL = paramString;
	}

	doodle = new DoodleCanvas();

	Font myFont = new Font("SansSerif", Font.PLAIN, 12);
	setFont(myFont);

	setBackground(new Color(0xd0d0d0));

	GridBagLayout gridbag = new GridBagLayout();
	setLayout(gridbag);
	GridBagConstraints gcon = new GridBagConstraints();

	gcon.weightx = 0.0;
	gcon.weighty = 0.0;

	gcon.insets = new Insets(4, 0, 0, 0);

	// Change this comment to force a CVS revision increment..
	String version = "$Id: JuestBookPanel.java 28 2004-11-21 13:23:51Z bd $";
	Label tmpLabel = new Label(version);

	/*
	add(tmpLabel);
	gcon.anchor = GridBagConstraints.WEST;
	gcon.fill = GridBagConstraints.NONE;
	gcon.gridwidth = GridBagConstraints.REMAINDER;
	gridbag.setConstraints(tmpLabel, gcon);
	*/

	cmdReset = new Button("Rensa");
	add(cmdReset);
	cmdReset.addActionListener(this);
	gcon.anchor = GridBagConstraints.WEST;
	gcon.fill = GridBagConstraints.NONE;
	gcon.gridwidth = 1;
	gridbag.setConstraints(cmdReset, gcon);

	cmdSend = new Button("Skicka");
	add(cmdSend);
	cmdSend.addActionListener(this);
	gcon.anchor = GridBagConstraints.WEST;
	gcon.fill = GridBagConstraints.NONE;
	gcon.gridwidth = 1;
	gridbag.setConstraints(cmdSend, gcon);

	gcon.weightx = 1.0;
	gcon.weighty = 0.0;
	gcon.fill = GridBagConstraints.BOTH;

	for (int i=0; i<doodle.brush.length; i++) {
	    BrushWidget bw = new BrushWidget(doodle, i);
	    add(bw);
	    gridbag.setConstraints(bw, gcon);
	    if (i == doodle.brushIndex) BrushWidget.selected = bw;
	}

	for (int i=0; i<doodle.palette.length; i++) {
	    ColorWidget cw = new ColorWidget(doodle, i);
	    add(cw);
	    gridbag.setConstraints(cw, gcon);
	    if (i == doodle.paletteIndex) ColorWidget.selected = cw;
	}

	tmpLabel = new Label("");
	add(tmpLabel);
	// Place it last
	gcon.fill = GridBagConstraints.NONE;
	gcon.gridwidth = GridBagConstraints.REMAINDER;
	gridbag.setConstraints(tmpLabel, gcon);

	// Place the doodle canvas below
	gcon.insets = new Insets(4, 4, 4, 4);
	add(doodle);
	gcon.weightx = 1.0;
	gcon.weighty = 1.0;
	gcon.fill = GridBagConstraints.BOTH;
	gridbag.setConstraints(doodle, gcon);
	doodle.addEventListeners();
    }

    public void start() {
	stopped = false;
    }

    public void stop() {
	stopped = true;
	if (uploadThread != null) {
	    uploadThread.interrupt();
	}
    }

    // For pre-1.2 java plugins
    public int getWidth() { return getSize().width; }
    public int getHeight() { return getSize().height; }
}

