import javax.swing.JTextArea;
import javax.swing.text.Highlighter.HighlightPainter;

public class MoveList
{
    RobboFrame RF;
    int w[];
    String string[], direct[];
    int LENGTH, MAX_MOVE;
    boolean WTM, CLEAR;
    String FEN_START;

    public MoveList (RobboFrame ROBBO_FRAME)
    {
	super ();
	RF = ROBBO_FRAME;
	w = new int[4096];
	direct = new String[4096];
	string = new String[4096];
	FEN_START = new String("");
	CLEAR = true;
	MAX_MOVE = 0;
	LENGTH = 0;
	for (int i = 0; i < 4096; i++) 
	    w[i] = -1;
	WTM = true;

    }

    public void Clear ()
    {
	CLEAR = true;
	MAX_MOVE = 0;
	LENGTH = 0;
	for (int i = 0; i < 4096; i++) 
	    w[i] = -1;
	WTM = true;
	ApplyText (RF.MOVE_LIST_TEXT_AREA, RF.HIGH_LIGHTER);
	RF.MOVE_LIST_TEXT_AREA.repaint ();
    }

    public void Begin ()
    {
	Clear ();
	FEN_START = new String (RF.BOARD_POSITION.GetFEN ());
	WTM = RF.BOARD_POSITION.WTM;
	CLEAR = false;
    }

    public void ApplyText (JTextArea JTA, HighlightPainter HILITE)
    {
	String S, R = new String ("");
	int START = 0, END = 0;
	for (int i = 0; i< MAX_MOVE; i++)
	    {
		S = string[i];
		R = R + " ";
		if (i == 0 && !WTM)
		    R += "  1.          ";
		if ((i % 2 != 0) ^ WTM)
		    {
			if (i < 197) R = R + " ";
			if (i < 17) R += " ";
			R += "" + ((i + 3) / 2) + ".  ";
			if (i == LENGTH - 1)
			    {
				START = R.length ();
				END = START + S.length ();
			    }
			R += S;
			for (int j = 0; j < 7 - S.length (); j++)
			    R = R + " ";
		    }
		else
		    {
			if (i == LENGTH - 1)
			    {
				START = R.length ();
				END = START + S.length ();
			    }
			R += S + "\n";
		    }
	    }
	JTA.setText (R);
	JTA.append (" ");
	try
	    {
		JTA.getHighlighter ().addHighlight (START, END, HILITE);
	    }
	catch (Exception exc) {}
	JTA.setCaretPosition (END);
    }

    public void ApplyMove (int W)
    {
	int MOVE = RF.BOARD_POSITION.move_list[W];
	int mv = MOVE & 262143;
	int prom = MOVE >> 19;
	int from_file = 1 + (mv & 7), from_rank = 1 + ((mv >> 3) & 7),
	    to_file = 1 + ((mv >> 6) & 7), to_rank = 1 + ((mv >> 9) & 7);
	w[LENGTH] = W;
	direct[LENGTH] = new String("");
	direct[LENGTH] += (char) ('a' + (char) (from_file - 1));
	direct[LENGTH] += (char) ('1' + (char) (from_rank - 1));
	direct[LENGTH] += (char) ('a' + (char) (to_file - 1));
	direct[LENGTH] += (char) ('1' + (char) (to_rank - 1));
	if (prom > 0)
	    {
		if (prom == 2)
		    direct[LENGTH] += 'n';
		if (prom == 3)
		    direct[LENGTH] += 'b';
		if (prom == 4)
		    direct[LENGTH] += 'r';
		if (prom==5)
		    direct[LENGTH] += 'q';
	    }
	string[LENGTH++] = new String (RF.BOARD_POSITION.move_list_annotated[W]); // copy
	MAX_MOVE = LENGTH;
    }
}
