import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class CharacterSelector {
	int x1 = 0;
	int y1 = 0;

	int x2 = 0;
	int y2 = 1;

	static String[][] characters = {{"Dog","UMA","UMA","UMA","UMA","UMA"},
			{"Cat","UMA","UMA","UMA","UMA","UMA"}};

	private static final int CHARACTER_IMAGE_HEIGHT = (int) (128 * (2.0 / characters.length));
	private static final int CHARACTER_IMAGE_WIDTH = (int) (128 * (6.0 / characters[0].length));

	Image[][] characterImages = new Image[characters.length][characters[0].length];

	public CharacterSelector() {
		for (int row = 0; row < characters.length; row++) {
			for (int col = 0; col < characters[0].length; col++) {
				try {
					characterImages[row][col] = ((Animal) Class.forName(characters[row][col])
							.getDeclaredConstructor(int.class, int.class, boolean.class)
							.newInstance(0, 215, false)).animalBufferedImage.getScaledInstance(CHARACTER_IMAGE_WIDTH, CHARACTER_IMAGE_HEIGHT, Image.SCALE_DEFAULT);
				} catch (InstantiationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (NoSuchMethodException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		if (AnimalTestG.AUDIO) {
			File f = new File("s6.wav");
			Clip clip = null;
			try {
				AudioInputStream audioIn = AudioSystem.getAudioInputStream(f.toURI().toURL());  
				clip = AudioSystem.getClip();
				clip.open(audioIn);
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (UnsupportedAudioFileException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (LineUnavailableException e) {
				e.printStackTrace();
			}
			clip.start();
			clip.loop(Clip.LOOP_CONTINUOUSLY);
			clip.setFramePosition(0);
		}
	}

	public void draw(Graphics g) {
		g.setColor(new Color(255, 255, 255));
		g.draw3DRect((AnimalTestG.W - CHARACTER_IMAGE_WIDTH * characters[0].length) / 2, AnimalTestG.H - CHARACTER_IMAGE_HEIGHT * characters.length - (AnimalTestG.W - CHARACTER_IMAGE_WIDTH * characters[0].length) / 2, CHARACTER_IMAGE_WIDTH * characters[0].length, 
				CHARACTER_IMAGE_HEIGHT * characters.length, true);

		g.setColor(new Color(255, 0, 0));
		g.draw3DRect((AnimalTestG.W - CHARACTER_IMAGE_WIDTH * characters[0].length) / 2 + x1 * CHARACTER_IMAGE_WIDTH, AnimalTestG.H - CHARACTER_IMAGE_HEIGHT * characters.length + y1 * CHARACTER_IMAGE_HEIGHT - (AnimalTestG.W - CHARACTER_IMAGE_WIDTH * characters[0].length) / 2, CHARACTER_IMAGE_WIDTH, CHARACTER_IMAGE_HEIGHT, true);

		g.setColor(new Color(0, 255, 0));
		g.draw3DRect((AnimalTestG.W - CHARACTER_IMAGE_WIDTH * characters[0].length) / 2 + x2 * CHARACTER_IMAGE_WIDTH, AnimalTestG.H - CHARACTER_IMAGE_HEIGHT * characters.length + y2 * CHARACTER_IMAGE_HEIGHT - (AnimalTestG.W - CHARACTER_IMAGE_WIDTH * characters[0].length) / 2, CHARACTER_IMAGE_WIDTH, CHARACTER_IMAGE_HEIGHT, true);

		for (int row = 0; row < characters.length; row++) {
			for (int col = 0; col < characters[0].length; col++) {
				g.drawImage(characterImages[row][col], (AnimalTestG.W - CHARACTER_IMAGE_WIDTH * characters[0].length) / 2 + col * CHARACTER_IMAGE_WIDTH, AnimalTestG.H - CHARACTER_IMAGE_HEIGHT * characters.length + row * CHARACTER_IMAGE_HEIGHT - (AnimalTestG.W - CHARACTER_IMAGE_WIDTH * characters[0].length) / 2, null);
			}
		}
	}

	public void update() {}

	public void keyPressed(KeyEvent evt) {
		if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
			try {
				AnimalTestG.player2 = (Animal) Class.forName(characters[y2][x2])
						.getDeclaredConstructor(int.class, int.class, boolean.class)
						.newInstance(AnimalTestG.W - 128, 215, true);
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}

		if (evt.getKeyCode() == KeyEvent.VK_SPACE) {
			try {
				AnimalTestG.player1 = (Animal) Class.forName(characters[y1][x1])
						.getDeclaredConstructor(int.class, int.class, boolean.class)
						.newInstance(0, 215, false);
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}

		if (evt.getKeyCode() == KeyEvent.VK_W) {
			y1--; if (y1 < 0) y1 = characters.length - 1;
		}
		if (evt.getKeyCode() == KeyEvent.VK_A) {
			x1--; if (x1 < 0) x1 = characters[0].length - 1;
		}
		if (evt.getKeyCode() == KeyEvent.VK_S) {
			y1++; if (y1 > characters.length - 1) y1 = 0;
		}
		if (evt.getKeyCode() == KeyEvent.VK_D) {
			x1++; if (x1 > characters[0].length - 1) x1 = 0;
		}

		if (evt.getKeyCode() == KeyEvent.VK_UP) {
			y2--; if (y2 < 0) y2 = characters.length - 1;
		}
		if (evt.getKeyCode() == KeyEvent.VK_LEFT) {
			x2--; if (x2 < 0) x2 = characters[0].length - 1;
		}
		if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
			y2++; if (y2 > characters.length - 1) y2 = 0;
		}
		if (evt.getKeyCode() == KeyEvent.VK_RIGHT) {
			x2++; if (x2 > characters[0].length - 1) x2 = 0;
		}
	}

	public void keyReleased(KeyEvent evt) {}
}