import java.awt.Color;

import com.jmxp.MXPProcessor;
import com.jmxp.MXPResult;
import com.jmxp.libmxp;
import com.jmxp.structures.SendStruct;
import com.jmxp.structures.flagStruct;
import com.jmxp.structures.formatStruct;
import com.jmxp.structures.gaugeStruct;
import com.jmxp.structures.imageStruct;
import com.jmxp.structures.internalWindowStruct;
import com.jmxp.structures.linkStruct;
import com.jmxp.structures.moveStruct;
import com.jmxp.structures.relocateStruct;
import com.jmxp.structures.soundStruct;
import com.jmxp.structures.statStruct;
import com.jmxp.structures.varStruct;
import com.jmxp.structures.windowStruct;


public class test 
{
	private MXPProcessor proc; 
	public test()
	{
		proc = new MXPProcessor("Test", "0.1");
		try
		{
			test1();
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
	
	private void processText(String text) throws Exception
	{
		proc.processText(text);
	}
	
	private void test1() throws Exception
	{
		  Color white = Color.white;
		  Color black = Color.black;
		  Color yellow = Color.YELLOW;

		  //initialize the MXP handler
		  mxpSetDefaultText ("fixed", 12, false, false, false, false, white, black);
		  mxpSetHeaderParams (1, "fixed", 28, true, true, true, false, yellow, black);
		  mxpSetHeaderParams (2, "fixed", 24, true, true, true, false, yellow, black);
		  mxpSetHeaderParams (3, "fixed", 20, true, true, true, false, yellow, black);
		  mxpSetHeaderParams (4, "fixed", 16, true, true, false, false, yellow, black);
		  mxpSetHeaderParams (5, "fixed", 14, true, true, false, false, yellow, black);
		  mxpSetHeaderParams (6, "fixed", 12, true, true, false, false, yellow, black);
		  mxpSetDefaultGaugeColor(white);
		  mxpSetNonProportFont ("fixed");
		  mxpSetClient ("Test", "0.1");

		  //feed the library with text
		  proc.processText("\u001B[1z");
		  proc.processText("&unknownentity;&lt;&gt;<c yellow back=blue>starting</c> text");
		  proc.processText(" and<font size=11 blue blackx face='\"\"><<>Times New Roman' 13>more");
		  proc.processText("<font face=\"baaah\"></font></font> text\r\n\u001B[1zline 2");
		  proc.processText(" <S><b><i>with bold text, &Aacute;, &lt;, &gt;, unfinished &");
		  proc.processText("\u001B[3z</BoLd>:)<a href=\r\nline 3");
		  proc.processText("\u001B[6z");
		  proc.processText("<hr><p>line1\r\nline2\r\nline3\r\nline4\r\n all on the same line!</p>");	  
		  
		  proc.processText("break line here<br><nobr>this won't break \r\nstill");
		  proc.processText("the same line\r\nanother line");
		  proc.processText("<c darkgreen> dark green <h>High-color</h> Normal color</c>\r\n");
		  proc.processText("<a \"http://www.kmuddy.org\" KMuddy kmuddy>Click here for KMuddy</a>");
		  proc.processText("<send href=\"buy bread|buy milk\" hINt=\"this is a hint|bread hint|");
		  proc.processText("milk hint\" expire=link>yes, bread</send>");
		  proc.processText("<expire link><expire>\n");
		  proc.processText("<VeRsIoN>\n");
		  proc.processText("<sound bah.wav v=10000 p=30 c=1><music chrm.mid u=none>");
		  proc.processText("<gauge hp max=maxhp caption='Your HP' color=green><stat sp max=maxsp");
		  proc.processText("caption='Spell points' invalid>");
		  proc.processText("<relocate port=8080 name=bah server=test><user><password>\n");
		  proc.processText("<image bah.jpg URL=www.url.org t=type1 h=100 w=50 align=middle");
		  proc.processText("vspace=10 hspace=10><image bah.jpg ISMaP><send showmap>");
		  proc.processText("<image bah.jpg ismap></send>\n");
		  proc.processText("<!en test mytest DESC=desc PUBLISH>&test;<!en test DELETE>");
		  proc.processText("<v bah>some<b></b>thing</v>\n");
		  proc.processText("<h2>Heading 2</h2><h5>Heading 5</h5>\r\n<small>small</small>");
		  proc.processText("<tt>non-proportional</tt>\r\n");

		  proc.processText("<frame name=Status redirect Height=30>");
		  proc.processText("text for status");
		  proc.processText("<frame _previous redirect>");
		  proc.processText("<dest status>100</dest>");
		  proc.processText("<dest status eof>100</dest>");
		  proc.processText("<frame Status close>\r\n");
		  proc.processText("<!-- this is a comment, <b> stuff not parsed -->\r\n");
		  proc.processText("This will break the line: \rnew line\r\n");
		  
		  //OKay, here goes the !element testing...
		  //EMPTY and OPEN flags have been successfully tested :)*/
		  proc.processText("<!element boldred '<color red><b>'>");
		  proc.processText("normal, <boldred>bold red</boldred>, normal again\n");
		  proc.processText("<!element ex '<send>'><ex>n</ex><ex>s</ex>\r\n");
		  proc.processText("<!entity col 'This should not be displayed' private>");
		  proc.processText("<!element bt '<b><c &col;>' att='col=red' flag=hrm>");
		  proc.processText("<bt>bold red <bt blue>bold blue</bt></bt>\r\n");
		  proc.processText("<!attlist bt 'col=green'>");
		  proc.processText("<bt>bold green <bt blue>bold blue</bt></bt>\r\n");
		  proc.processText("<!el bt delete><bt>in invalid tag</bt>\r\n");
		  proc.processText("<!el tagtest '<b><u><i>' tag=28>\r\n");
		  proc.processText("\u001B[28zthis should be b,u,i\r\nnormal\r\n");
		  
		  //display results
		  while ( proc.hasResult())
		  {
		    MXPResult res = proc.nextResult();
		    displayResult(res);
		  }
		
	}

	private void mxpSetClient(String name, String version) 
	{
		proc.setClient(name, version);		
	}

	private void mxpSetNonProportFont(String font) 
	{
		proc.setNonProportFont(font);		
	}

	private void mxpSetDefaultGaugeColor(Color color) 
	{
		proc.setDefaultGaugeColor(color);
	}

	private void mxpSetHeaderParams(int which, String font, int size, boolean _bold,
			boolean _italic, boolean _underline, boolean _strikeout, Color fg, Color bg) 
	{
		proc.setHeaderParams(which, font, size, _bold, _italic, _underline,
			      _strikeout, fg, bg);		
	}

	private void mxpSetDefaultText(String font, int size, boolean _bold, boolean _italic,
			boolean _underline, boolean _strikeout, Color fg, Color bg) 
	{
		proc.setDefaultText (font, size, _bold, _italic, _underline,
			      _strikeout, fg, bg);
	}

	void displayResult (MXPResult res)
	{
	  switch (res.type)
	  {
	    case 0: {
	    	
	      System.out.print("Nothing.\n");
	    }
	    break;
	    case 1: {
	      String s = (String) res.data;
	      if (s.equals("\r\n"))  //newlines are always sent separately
	        System.out.print("Newline.\n");
	      else
	        System.out.print("Text: "+s+"\n");
	    }
	    break;
	    case 2: {
	      int t = (Integer) res.data;
	      System.out.print("Line tag: "+t+"\n");
	    }
	    break;
	    case 3: {
	      flagStruct fs = (flagStruct) res.data;
	      if (fs.begin)
	        System.out.print("Start of flag: "+fs.name+"\n");
	      else
	        System.out.print("End of flag: "+fs.name+"\n");
	    }
	    break;
	    case 4: {
	      varStruct vs = (varStruct) res.data;
	      if (vs.erase)
	        System.out.print("Erase variable: "+vs.name+"\n");
	      else
	        System.out.print("Variable: "+vs.name+", value: "+vs.value+"\n");
	    }
	    break;
	    case 5: {
	      formatStruct fs = (formatStruct) res.data;
	      int mask = fs.usemask;
	      if ((mask & formatStruct.USE_BOLD) != 0 )
	        System.out.print("Formatting: "+
	            (((fs.attributes & libmxp.Bold) != 0 ) ? "bold" : "not bold")+"\n");
	      if ((mask & formatStruct.USE_ITALICS) != 0 )
	        System.out.print("Formatting: "+
	            (((fs.attributes & libmxp.Italic) != 0 ) ? "italics" : "not italics")+"\n");
	      if ((mask & formatStruct.USE_UNDERLINE) != 0 )
	        System.out.print("Formatting: "+
	            (((fs.attributes & libmxp.Underline) != 0 ) ? "underline" : "not underline")+"\n");
	      if ((mask & formatStruct.USE_STRIKEOUT) != 0 )
	        System.out.print("Formatting: "+
	            (((fs.attributes & libmxp.Strikeout) != 0 ) ? "strikeout" : "not strikeout")+"\n");
	      if ((mask & formatStruct.USE_FG) != 0 )
	        System.out.printf("Formatting: foreground color (%d, %d, %d)\n", fs.fg.getRed(), 
	        		fs.fg.getGreen(), fs.fg.getBlue());
	      if ((mask & formatStruct.USE_BG) != 0 )
	        System.out.printf("Formatting: background color (%d, %d, %d)\n", fs.bg.getRed(), 
	        		fs.bg.getGreen(), fs.bg.getBlue());
	      if ((mask & formatStruct.USE_FONT) != 0 )
	        System.out.print("Formatting: font "+fs.font+"\n");
	      if ((mask & formatStruct.USE_SIZE) != 0 )
	        System.out.printf("Formatting: size %d\n", fs.size);
	    }
	    break;
	    case 6: {
	      linkStruct ls = (linkStruct) res.data;
	      System.out.printf("URL link: name %s, URL %s, text %s, hint %s\n",
	          ls.name, ls.url, ls.text, ls.hint);
	    }
	    break;
	    case 7: {
	      SendStruct ss = (SendStruct) res.data;
	      System.out.printf("Send link: name %s, command %s, text %s, hint %s, to prompt: %s, menu: %s\n",
	          ss.name, ss.command, ss.text,
	          ss.hint, (ss.toprompt?"yes":"no"), (ss.ismenu?"yes":"no"));
	    }
	    break;
	    case 8: {
	      String c = (String)res.data;
	      System.out.print("Expire: "+c+"\n");
	    }
	    break;
	    case 9: 
	    {
	    	String c = (String)res.data;
		    System.out.print("Send this: "+c+"\n");
	    }
	    break;
	    case 10: {
	      System.out.print("Horizontal line\n");
	    }
	    break;
	    case 11: {
	      soundStruct ss = (soundStruct) res.data;
	      if (ss.isSOUND)
	        System.out.printf("Sound: file %s, URL %s, volume %d, %d repeats, priority %d, type %s\n",
	            ss.fname, ss.url, ss.vol, ss.repeats, ss.priority,
	            ss.type);
	      else
	        System.out.printf("Music: file %s, URL %s, volume %d, %d repeats, continue %s, type %s\n",
	            ss.fname, ss.url, ss.vol, ss.repeats,
	            (ss.continuemusic?"yes":"no"), ss.type);
	    }
	    break;
	    case 12: {
	      windowStruct ws = (windowStruct) res.data;
	      System.out.printf("Create window: name %s, title %s, left %d, top %d, width %d, height %d, scrolling %s, floating %s\n",
	          ws.name, ws.title, ws.left, ws.top, ws.width, ws.height,
	          (ws.scrolling?"yes":"no"), (ws.floating?"yes":"no"));
	    }
	    break;
	    case 13: {
	      internalWindowStruct ws = (internalWindowStruct) res.data;
	      String s = "none";
	      switch (ws.align) {
	        case Left: s = "left"; break;
	        case Right: s = "right"; break;
	        case Bottom: s = "bottom"; break;
	        case Top: s = "top"; break;
	        case Middle: s = "middle (invalid!)"; break;
	      };
	      System.out.printf("Create internal window: name %s, title %s, align %s, scrolling %s\n",
	          ws.name, ws.title, s, (ws.scrolling?"yes":"no"));
	    }
	    break;
	    case 14: {
	      String s = (String)res.data;
	      System.out.printf("Close window: %s\n", s);
	    }
	    break;
	    case 15: {
	    	String s = (String)res.data;
	      System.out.printf("Set active window: %s\n", s);
	    }
	    break;
	    case 16: {
	      moveStruct ms = (moveStruct) res.data;
	      System.out.printf("Move cursor: X=%d, Y=%d\n", ms.x, ms.y);
	    }
	    break;
	    case 17: {
	      System.out.printf("Erase text: %s\n", ((res.data!=null) ? "rest of frame" : "rest of line"));
	    }
	    break;
	    case 18: {
	      relocateStruct rs = (relocateStruct) res.data;
	      System.out.printf("Relocate: server %s, port %d\n", rs.server, rs.port);
	    }
	    break;
	    case 19: {
	      System.out.print( ((Boolean)res.data) ? "Send username\n" : "Send password\n");
	    }
	    break;
	    case 20: {
	      imageStruct is = (imageStruct) res.data;
	      String s = "";
	      switch (is.align) {
	        case Left: s = "left"; break;
	        case Right: s = "right"; break;
	        case Bottom: s = "bottom"; break;
	        case Top: s = "top"; break;
	        case Middle: s = "middle"; break;
	      };
	      System.out.printf("Image: name %s, URL %s, type %s, height %d, width %d, hspace %d, vspace %d, align %s\n",
	          is.fname, is.url, is.type, is.height,
	          is.width, is.hspace, is.vspace, s);
	    }
	    break;
	    case 21: {
	    	String s = (String)res.data;
	      System.out.printf("Image map: %s\n", s);
	    }
	    break;
	    case 22: {
	      gaugeStruct gs = (gaugeStruct) res.data;
	      System.out.printf("Gauge: variable %s, max.variable %s, caption %s, color (%d, %d, %d)\n",
	          gs.variable, gs.maxvariable,
	          gs.caption, gs.color.getRed(), gs.color.getGreen(), gs.color.getBlue());
	    }
	    break;
	    case 23: {
	      statStruct ss = (statStruct) res.data;
	      System.out.printf("Status bar: variable %s, max.variable %s, caption %s\n",
	          ss.variable, ss.maxvariable,
	          ss.caption);
	    }
	    break;
	    case -1: {
	    	String s = (String)res.data;
	      System.out.printf("Error: %s\n", s);
	    }
	    break;
	    case -2: {
	    	String s = (String)res.data;
	      System.out.printf("Warning: %s\n", s);
	    }
	    break;
	  }
	}
	
	
	
	public static void main(String[] args) 
	{
		new test();
	}
	
}