package gui.components;

import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JPanel;

import game.Game;
import gui.Resources;
import gui.views.GameView;

public class JokerPanel extends JPanel implements MouseListener{

	private Resources resources;
	private Game game;
	
	private GameView gv;
	
	private enum JokerTypes {
			ADD_TROOPS,
			ADD_TROOPS_USED,
			SCARE_TROOPS,
			SCARE_TROOPS_USED,
	}
	
	private JokerTypes[] jokers;
	
	public JokerPanel(Resources resources, Game game) {
		this.game = game;
		this.resources = resources;
		this.addMouseListener(this);
		initJokers();
	}
	
	private void initJokers() {
		jokers = new JokerTypes[]{JokerTypes.ADD_TROOPS, JokerTypes.SCARE_TROOPS };
	}
	
	public void setGameView(GameView gv) {
		this.gv = gv;
	}

	public void setGame(Game game) {
		this.game = game;
	}
	
	private void setJoker(int index, JokerTypes j) {
		jokers[index] = j;
	}
	
	public void playJoker(JokerTypes joker) {
		if(game.getRound() < 2)
			return;
		
		if(joker == JokerTypes.ADD_TROOPS) {
			if(game == null)
				System.out.println("Nll");
			game.getCurrentPlayer().addTroops(5);
			gv.updateStats();
			gv.repaint();
			System.out.println("Tee");
			joker = JokerTypes.ADD_TROOPS_USED;
			setJoker(joker.ordinal(), JokerTypes.ADD_TROOPS_USED);
			System.out.println(joker.ordinal());
		}		
	}
	
	@Override
	public void paintComponent(Graphics g) {
        super.paintComponent(g);

        paintJokers(g);
	}

	public void paintJokers(Graphics g) {
		if(jokers == null)
			return;
		
		
		
		for(int i = 0; i < jokers.length; i++) {
			System.out.print("i: " + i + jokers[i] + " ");
			g.drawImage(resources.getJokers()[jokers[i].ordinal()], 9 + 32 * i + 10 * i, 5, 32, 32, null);
			/*if(jokers[i] == JokerTypes.ADD_TROOPS) {
	        	g.drawImage(resources.getJokers()[JokerTypes.valueOf("ADD_TROOPS").ordinal()], 9 + 32 * i + 10 * i, 5, 32, 32, null);
			} else if(jokers[i] == JokerTypes.ADD_TROOPS_USED) {
	        	g.drawImage(resources.getJokers()[JokerTypes.valueOf("ADD_TROOPS_USED").ordinal()], 9 + 32 * i + 10 * i, 5, 32, 32, null);
			} else if(jokers[i] == JokerTypes.SCARE_TROOPS) {
	        	g.drawImage(resources.getJokers()[JokerTypes.valueOf("SCARE_TROOPS").ordinal()], 9 + 32 * i + 10 * i, 5, 32, 32, null);
			} else if(jokers[i] == JokerTypes.SCARE_TROOPS) {
	        	g.drawImage(resources.getJokers()[JokerTypes.valueOf("SCARE_TROOPS_USED").ordinal()], 9 + 32 * i + 10 * i, 5, 32, 32, null);
			}*/
		}
		
		
	}
	

	@Override
	public void mouseClicked(MouseEvent e) {
		playJoker(JokerTypes.ADD_TROOPS);
		for(JokerTypes j : jokers) {
			System.out.println(j.ordinal());
		}
	}


	@Override
	public void mousePressed(MouseEvent e) {
		
	}


	@Override
	public void mouseReleased(MouseEvent e) {
		
	}


	@Override
	public void mouseEntered(MouseEvent e) {

	}


	@Override
	public void mouseExited(MouseEvent e) {

	}
	
}
