import java.awt.event.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;

import javax.imageio.ImageIO;
public class HWMenu extends HWMainDisplay {
	public static final int MENU_MAIN = 0;
	public static final int MENU_NEWGAME = 1;
	public static final int MENU_SCROLL = 2;
	public static final int MENU_GAMEOVER = 3;
	private static final int[] MAX_SELECTION = {3,4,0,2};
	private static final int DEF_SCROLL_SPEED_SLOW = 2;
	private static final int DEF_SCROLL_SPEED_FAST = 8;
	private static final long serialVersionUID = 2;
	private static final int[] xPoints = {100,100,115};
	private static final int[][] yPoints = {{182,202,192},
											{232,252,242},
											{282,302,292},
											{332,352,342}};
	private static final String INTRO_IMG_NAME = "img/intro/";
	private static final int INTRO_PANE_NUM = 4;
	private static final String OUTRO_IMG_NAME = "img/end/";
	private static final int OUTRO_PANE_NUM = 3;
	private ClassLoader cl = HWDisplay.cl;
	private int selection;
	private int menu;
	private int scrollSpeed;
	private int scrollAmount;
	private int levelNum;
	private HWMainDisplay nextLevelDisplay;
	private BufferedImage menuBkg;
	private BufferedImage titleBkg;
	private BufferedImage[] titleTxt;
	private BufferedImage titleFrg;
	
	public HWMenu(HWDisplay frame) {
		super(frame);
		selection = 0;
		menu = MENU_MAIN;
		speed = 10;
		ked = new KeyEventDispatcher() {
			public boolean dispatchKeyEvent(KeyEvent e) {
				if (menu == MENU_MAIN || menu == MENU_NEWGAME || menu == MENU_GAMEOVER) {
					if (e.getID()==KeyEvent.KEY_PRESSED) {
						if (e.getKeyCode() == KeyEvent.VK_UP) {
							selectionUp();
						} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
							selectionDown();
						} else if (e.getKeyCode() == KeyEvent.VK_ENTER) {
							selectionEnter();
						}
					}
				} else if (menu == 2) {
					if (e.getID()==KeyEvent.KEY_PRESSED) {
						if (e.getKeyCode() == KeyEvent.VK_SPACE) {
							scrollSpeed = DEF_SCROLL_SPEED_FAST;
						} else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
							selectionEnter();
						}
					} else if (e.getID()==KeyEvent.KEY_RELEASED) {
						if (e.getKeyCode() == KeyEvent.VK_SPACE) {
							scrollSpeed = DEF_SCROLL_SPEED_SLOW;
						}
					}
				}
				return false;
			}
		};
		try {
			menuBkg = ImageIO.read(cl.getResource("img/menubkg.png"));
			titleBkg = ImageIO.read(cl.getResource("img/intro/background.png"));
			titleFrg = ImageIO.read(cl.getResource("img/intro/foreground.png"));
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}
	public void introLevel(int levelNum) {
		this.levelNum=levelNum;
		if (levelNum == -1) {
			loadScrollImgs(INTRO_IMG_NAME, INTRO_PANE_NUM);
			nextLevelDisplay=this;
		} else if (levelNum == 5) {
			loadScrollImgs(OUTRO_IMG_NAME, OUTRO_PANE_NUM);
			nextLevelDisplay=this;
		} else {
			String levelFile = "level"+levelNum+".hwl";
			try {
				InputStream fis = cl.getResourceAsStream(levelFile);
				ObjectInputStream in = new ObjectInputStream(fis);
				LevelDec levInfo = (LevelDec)in.readObject();
				in.close();
				loadScrollImgs(levInfo.imgPath, levInfo.scrollPaneNum);
				if (levelNum != 1) {
					nextLevelDisplay=new HWPlatformer(frame, levelNum, levInfo);
				} else {
					nextLevelDisplay=new HWTopDown(frame, levelNum, levInfo);
				}
			} catch (IOException e) {
				System.out.println("Missing level file: " + levelFile);
				System.exit(1);
			} catch (ClassNotFoundException e) {
				System.out.println("Corrupt level file: " + levelFile);
				System.exit(1);
			}
		}
		if (levelNum > 0) {
			frame.saveGame();
		}
		scrollSpeed = DEF_SCROLL_SPEED_SLOW;
		scrollAmount = -400;
		menu=MENU_SCROLL;
	}
	public void loadScrollImgs(String imgPath, int scrollPaneNum) {
		titleTxt = new BufferedImage[scrollPaneNum];
		try {
			for (int i=0; i<scrollPaneNum; i++) {
				titleTxt[i] = ImageIO.read(cl.getResource(imgPath+"text"+i+".png"));
			}
		} catch (IOException e1) {
			System.out.println(imgPath+"text");
			e1.printStackTrace();
		}
	}
	public void gameOver() {
		menu = MENU_GAMEOVER;
		selection = 0;
	}
	
	public void setSelection(int sel) {
		selection=sel;
	}
	public void setMenu(int m) {
		menu = m;
	}
	
	public void selectionUp() {
		selection = (selection+MAX_SELECTION[menu]-1)%MAX_SELECTION[menu];
	}
	public void selectionDown() {
		selection = (selection+1)%MAX_SELECTION[menu];
	}
	public void selectionEnter() {
		if (menu == MENU_MAIN) {
			if (selection == 0) {
				//New game
				frame.gamestate.reset();
				frame.changeTo(-1);
			} else if (selection == 1) {
				//Load game
				frame.loadGame();
			} else if (selection == 2) {
				//Exit
				System.exit(0);
			}
		} else if (menu == MENU_SCROLL) {
			if (nextLevelDisplay == this) {
				if (levelNum == -1) {
					introLevel(0);
				} else {
					menu=0;
					selection=0;
				}
			} else { 
				frame.changeTo(nextLevelDisplay);
			}
			frame.gamestate.setLevel(frame.gamestate.getLevel()+1);
		} else if (menu == MENU_GAMEOVER) {
			if (selection == 0) {
				//Retry
				frame.loadGame();
			} else if (selection == 1) {
				//Main Menu
				menu = 0;
				selection = 0;
			}
		}
	}
	
	protected int getImgSize() {
		return 512;
	}
	
	protected void update(BufferedImage img) {
		Graphics2D g = (Graphics2D)img.getGraphics();
		if (menu == MENU_MAIN) {
			g.drawImage(menuBkg, 0, 0, 512, 512, 0, 0, 256, 256, null);
			g.setColor(Color.black);
			g.setFont(new Font("Monospaced", Font.BOLD, 36));
			g.drawString("Hobo Wars:",100,100);
			g.setFont(new Font("Monospaced", Font.BOLD, 18));
			g.drawString("Taking Over the Government of",100,125);
			g.drawString("The United States of America",100,150);
			g.setFont(new Font("Monospaced", Font.BOLD, 24));
			g.drawString("New Game",125,200);
			g.drawString("Load Game",125,250);
			g.drawString("Exit Game",125,300);
			g.setColor(Color.red);
			g.fillPolygon(xPoints,yPoints[selection],3);
		} else if (menu == MENU_NEWGAME) {
			g.drawImage(menuBkg, 0, 0, 512, 512, 0, 0, 256, 256, null);
			g.setColor(Color.black);
			g.setFont(new Font("Monospaced", Font.BOLD, 36));
			g.drawString("Hobo Wars:",100,100);
			g.setFont(new Font("Monospaced", Font.BOLD, 18));
			g.drawString("Taking Over the Government of",100,125);
			g.drawString("The United States of America",100,150);
			g.setFont(new Font("Monospaced", Font.BOLD, 24));
			g.drawString("Platformer Test",125,200);
			g.drawString("Top Down Test",125,250);
			g.drawString("Back",125,300);
			g.drawString("New Game Test", 125, 350);
			g.setColor(Color.red);
			g.fillPolygon(xPoints,yPoints[selection],3);
		} else if (menu == MENU_SCROLL) {
			g.drawImage(titleBkg, 0, 0, null);
			int join;
			if (scrollAmount <= 0) {
				join = 512+scrollAmount;
				g.drawImage(titleTxt[0], 0, 512-join, 512, 512, 0, 0, 512, join, null);
			} else {
				int i;
				for (i=1; i<titleTxt.length && i>0; i++) {
					if (scrollAmount <= i*512) {
						join = i*512-scrollAmount;
						g.drawImage(titleTxt[i-1], 0, 0, 512, join, 0, scrollAmount-(i-1)*512, 512, 512, null);
						g.drawImage(titleTxt[i], 0, join, 512, 512, 0, 0, 512, 512-join, null);
						i=-1;
					}
				}
				if (i > 0) {
					g.drawImage(titleTxt[titleTxt.length-1], 0, 0, 512, 512, 0, 0, 512, 512, null);
					scrollSpeed = 0;
				}
			}
			g.drawImage(titleFrg, 0, 0, null);
			scrollAmount+=scrollSpeed;
		} else if (menu == MENU_GAMEOVER) {
			g.drawImage(menuBkg, 0, 0, 512, 512, 0, 0, 256, 256, null);
			g.setColor(Color.black);
			g.setFont(new Font("Monospaced", Font.BOLD, 24));
			g.drawString("You lost the game.",100,100);
			g.setFont(new Font("Monospaced", Font.BOLD, 18));
			g.drawString("You received a score of: " + frame.gamestate.getPoints(),100,125);
			g.setFont(new Font("Monospaced", Font.BOLD, 24));
			g.drawString("Retry",125,200);
			g.drawString("Main Menu",125,250);
			g.setColor(Color.red);
			g.fillPolygon(xPoints,yPoints[selection],3);
		}
	}
	
	protected void paintComponent(Graphics g) {
		g.drawImage(screen, 0, 0, null);
	}
}
