import Slider;
import java.awt.*;

final class SliderAbout extends Dialog
{

	private Slider game;
	private Panel cards;
	private CardLayout cardmgr;
	private TextField timefield;
	private TextField movesfield;
	private final String VERSION = "Version";
	private final String HELP = "Help";
	private final String STATISTICS = "Statistics";
	private final String CLONE = "Clone";
	private final String COLORS = "Colors";

	Button clonebutton;

	private int tilecolor,boardcolor;
	private ImageCanvas icon;
	private Scrollbar tred,tgrn,tblu,bred,bgrn,bblu;
	private TextField tilepatch,boardpatch;
	
	private final String[] HELPTEXT = {
		"                         (scroll down for more)",
		"OBJECT",
		"Slider is a puzzle consisting of ten",
		"tiles of 3 different sizes in a box.",
		"You move the tiles around the box",
		"by dragging them with the mouse.",
		"The object is to move the biggest",
		"tile to the bottom of the box.",
		"If you've done that, center it there.",
		"",
		"RESET",
		"Pressing the Reset button returns all",
		"tiles to their starting positions and",
		"resets the time and move counters.",
		"",
		"ABOUT",
		"Pressing the About button displays",
		"the About_Slider dialog (where this",
		"text appears). Repeatedly pressing",
		"the button cycles through the pages",
		"which are directly accessible through",
		"the buttons to the left.",
		"Each puzzle (see CLONE below) has",
		"its own About_Slider dialog",
		"",
		"STATISTICS",
		"Two counters are maintained on the",
		"Statistics page:  Elapsed time and",
		"Number of moves.  Both are started",
		"when you first move a tile.",
		"If you are running Slider as an applet",
		"(from a web browser) the time counter",
		"will be suspended if you browse to",
		"another page and resumed when",
		"you return (provided the browser has",
		"not discarded the game entirely).",
		"The time counter is stopped when",
		"you get the big tile to the bottom.",
		"",
		"CLONE",
		"Pressing the Clone Button produces",
		"a duplicate of the puzzle in a new",
		"window. Please do not mix tiles",
		"from different puzzles.  Clones will",
		"remain if you browse to another page",
		"and will be killed when you exit",
		"your browser.",
		"",
		"COLORS",
		"If you believe that the designer of",
		"the web page containing Slider must",
		"be color blind or otherwise afflicted,",
		"you can adjust the color of the tiles",
		"and of the board that they slide upon",
		"by playing with the controls on the",
		"color page.",
		"",
		"SOLUTION",
		"Yes Virginia, there is a solution...",
		"I originally wrote Slider in 1991 in C",
		"for Microsoft Windows and inspired",
		"by an X-Windows implementation of",
		"the same puzzle named \"Xasperate\"",
		"(for which I never saw the code - I've",
		"no idea what algorithms were used).",
		"It took me almost as long to solve the",
		"puzzle the first time as it did to write",
		"the code.  So you'll understand that",
		"I'm reluctant to spoil your fun by",
		"showing you a solution.  Unfortunately",
		"many will be convinced that there is",
		"no solution unless they see it done.",
		"If you're one of those, go back to the",
		"Version page and click on the little",
		"puzzle.  Since this feature is only",
		"intended to show you that a solution",
		"is possible, you are only allowed to",
		"see it once.  To see it again, you'll",
		"probably need to exit & restart your",
		"browser.  You'll get no reward for",
		"solving the puzzle this way and there",
		"is a much shorter way to get the big",
		"tile to the bottom.  Enjoy!"
	};

	

	public SliderAbout(Slider gm) {
		super((Frame)gm.getParent(),"About Slider",false);
		game = gm;
		setResizable(false);
		
		cards = new Panel();
		cards.setLayout(cardmgr = new CardLayout());

		Panel p = new Panel();
		p.setLayout(new BorderLayout());
		p.add("North",new Label(" "));		// vertical space
		Panel pp = new Panel();
		pp.add(icon = new ImageCanvas(game.createIcon(),pp,32,32));
		Label l = new Label(game.NAME,Label.RIGHT);
		l.setFont(new Font("Helvetica", Font.BOLD, 16));
		pp.add(l);
		pp.add(new Label(game.VERSION));
		p.add("Center",pp);
		p.add("South",new Label(game.COPYWRITE,Label.CENTER));
		cards.add(VERSION,p);

		p = new Panel();
		p.setLayout(new GridLayout(1,1));
		TextArea text = new TextArea(5,30);
		text.setEditable(false);
		for (int i = 0; i < HELPTEXT.length; i++)
			text.appendText(HELPTEXT[i]+"\r\n");
		p.add(text);
		cards.add(HELP,p);

		p = new Panel();
		p.setLayout(new GridLayout(3,1));
		l = new Label("  Statistics");
		l.setFont(new Font("Helvetica", Font.BOLD, 12));
		p.add(l);
		pp = new Panel();
		pp.add(new Label("       Elapsed time:"));
		timefield = new TextField(5);
		timefield.setEditable(false);
		pp.add(timefield);
		p.add(pp);
		pp = new Panel();
		pp.add(new Label("Number of moves:"));
		movesfield = new TextField(5);
		movesfield.setEditable(false);
		pp.add(movesfield);
		p.add(pp);
		cards.add(STATISTICS,p);

		tilecolor = game.puzzle.tilecolor;
		boardcolor = game.puzzle.boardcolor;
		p = new Panel();
		p.setLayout(new BorderLayout());
		pp = new Panel();
		pp.setLayout(new GridLayout(6,1));
		pp.add(new Panel());
		pp.add(new Label("Tiles"));
		pp.add(new Panel());
		pp.add(new Panel());
		pp.add(new Label("Board   "));
		p.add("West",pp);
		pp = new Panel();
		pp.setLayout(new GridLayout(8,1));
		pp.add(new Panel());
		pp.add(tred = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 255));
		pp.add(tgrn = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 255));
		pp.add(tblu = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 255));
		pp.add(new Panel());
		pp.add(bred = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 255));
		pp.add(bgrn = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 255));
		pp.add(bblu = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 255));
		tred.setBackground(new Color(0xff0000));
		tgrn.setBackground(new Color(0x00ff00));
		tblu.setBackground(new Color(0x0000ff));
		bred.setBackground(new Color(0xff0000));
		bgrn.setBackground(new Color(0x00ff00));
		bblu.setBackground(new Color(0x0000ff));
		tred.setValue((tilecolor>>16) & 255);
		tgrn.setValue((tilecolor>>8) & 255);
		tblu.setValue((tilecolor) & 255);
		bred.setValue((boardcolor>>16) & 255);
		bgrn.setValue((boardcolor>>8) & 255);
		bblu.setValue((boardcolor) & 255);
		p.add("Center",pp);
		cards.add(COLORS,p);

		add("Center",cards);
		cardmgr.first(cards);

		p = new Panel();
		p.setLayout(new GridLayout(5,1));
		p.add(new Button(VERSION));
		p.add(new Button(HELP));
		p.add(new Button(STATISTICS));
		p.add(clonebutton = new Button(CLONE));
		if (game.solving) clonebutton.disable();
		p.add(new Button(COLORS));
		add("West",p);
		pack();
		if (game.timer != 0) displaytime(game.timer);
		move(game.aboutloc.x, game.aboutloc.y);
		show();
	}

	void sliderclone() {
		if (game.solving) return; 
		Slider slider = new Slider();
		slider.m_fStandAlone = true;
		slider.m_tilecolor = game.m_tilecolor;
		slider.m_boardcolor = game.m_boardcolor;
		slider.m_shimcolor = game.m_shimcolor;
		slider.m_width = game.size().width;
		slider.m_height = game.size().height;
		slider.init();

		slider.puzzle.width = game.puzzle.width;
		slider.puzzle.height = game.puzzle.height;
		slider.puzzle.bevel = game.puzzle.bevel;
		for (int i=0; i<10; i++) {
			slider.puzzle.tile[i].left = game.puzzle.tile[i].left;
			slider.puzzle.tile[i].top = game.puzzle.tile[i].top;
			slider.puzzle.tile[i].right = game.puzzle.tile[i].right;
			slider.puzzle.tile[i].bottom = game.puzzle.tile[i].bottom;
		}
		slider.moves = game.moves;
		slider.timer = game.timer;
		slider.winner = game.winner;
		slider.bigwinner = game.bigwinner;
		slider.solved = game.solved;

		AppletFrame frame = new AppletFrame("Slider", slider, game.clonecount);
		// Must show Frame before we size it so insets() will return valid values
		frame.show();
		frame.hide();
		frame.setBackground(new Color(slider.m_shimcolor));
//		frame.setIconImage(slider.createIcon());	// WHY doesn't this work!?!?
		frame.resize(frame.insets().left + frame.insets().right + slider.m_width,
					 frame.insets().top + frame.insets().bottom + slider.m_height);
		frame.add("Center", slider);
		slider.start();
		frame.show();
		game.clonecount[0]++;
	}

	void displaymoves(int moves) {
		if (moves < 0) movesfield.setText("XXX");
		else if (moves > 9999) movesfield.setText("Lots");
		else movesfield.setText(""+moves);
	}

	void displaytime(int seconds) {
		String s = "";
		int h = seconds/3600;
		if (h > 0) {
			if (h<10) s += "0";
			s += h + ":";
			seconds %= 3600;
		}
		int m = seconds/60;
		if (m<10) s += "0";
		s += m + ":";
		seconds %= 60;
		if (seconds<10) s += "0";
		s += seconds;
		timefield.setText(s);
	}

	void displaywipe() {
		movesfield.setText("");
		timefield.setText("");
	}

	public boolean action(Event evt, Object arg) {
		if (evt.target instanceof Button) {
			if (((String)arg).equals(CLONE)) sliderclone();
			else cardmgr.show(cards,(String)arg);
			return true;
		}
		return false;
	}

	void next() {
		cardmgr.next(cards);
	}

	public boolean handleEvent(Event e) {

		if (e.id == Event.WINDOW_DESTROY) {
			game.aboutloc = location();
			dispose();
			game.about = null;
			return true;
		}
		if (e.target instanceof Scrollbar) {
			tilecolor = tred.getValue()<<16 | tgrn.getValue()<<8 | tblu.getValue();
			game.puzzle.tilecolor = tilecolor;
			game.m_tilecolor = tilecolor;
			boardcolor = bred.getValue()<<16 | bgrn.getValue()<<8 | bblu.getValue();
			game.puzzle.boardcolor = boardcolor;
			game.m_boardcolor = boardcolor;
			game.puzzle.repaint();
        }
		else if (e.target == icon) {
			if (e.id == Event.MOUSE_UP && !game.solving)
				game.showsolution();
		}
		return super.handleEvent(e);
	}

}


