package gui.components;

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

import javax.swing.JPanel;
import javax.swing.JOptionPane;

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

public class JokerPanel extends JPanel implements MouseListener{

	private Resources resources;
	private Game game;
	
	private MapPanel map;
	
	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;
	}
	
	public void setMapPanel(MapPanel map) {
		this.map = map;
	}
	
	private void setJoker(int index, JokerTypes j) {
		jokers[index] = j;
	}
	
	public void playJoker(int index) {
		if(game.getRound() < 2)
			return;
		
		if(index == 0) {
			
			 int choice = JOptionPane.showOptionDialog(this, "Dieser Joker gibt dir 5 Truppen. Sicher, dass du ihn verwenden willst?",
                     "Truppen-Joker",
                     JOptionPane.YES_NO_OPTION,
                     JOptionPane.INFORMATION_MESSAGE,
                     null, new String[]{"Benutzen", "Abbrechen"}, "Benutzen");
			if(choice == 1)
				return;
			game.getCurrentPlayer().addTroops(5);
			gv.updateStats();
			gv.repaint();
			setJoker(index, JokerTypes.ADD_TROOPS_USED);
		}
		
		if(index == 1) {
			 int choice = JOptionPane.showOptionDialog(this, "Dieser Joker jagt den gegnerischen Truppen in einer Burg deiner Wahl Angst ein, sodass sie in benachbarte Burgen fliehen.\nFalls es keine benachbarten Burgen gibt, rennen die gegnerischen Truppen in den Wald und sterben einen qualvollen Hungertod. Sicher, dass du ihn verwenden willst?",
                     "Truppen verscheuchen",
                     JOptionPane.YES_NO_OPTION,
                     JOptionPane.INFORMATION_MESSAGE,
                     null, new String[]{"Benutzen", "Abbrechen"}, "Benutzen");
			 if(choice == 1)
				 return;
			 
			 System.out.println("Joker used");
			 map.setIsChoosingJoker(true);
			 setJoker(index, JokerTypes.SCARE_TROOPS_USED);
			 
		}
	}
	
	@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);
		}
		
	}
	

	@Override
	public void mouseClicked(MouseEvent e) {
		System.out.println("X: " + e.getX() + " Y: " + e.getY());
		if(e.getX() <= 41 && jokers[0] == JokerTypes.ADD_TROOPS) {
			playJoker(0);
		} else
			playJoker(1);
		
	}


	@Override
	public void mousePressed(MouseEvent e) {
		
	}


	@Override
	public void mouseReleased(MouseEvent e) {
		
	}


	@Override
	public void mouseEntered(MouseEvent e) {

	}


	@Override
	public void mouseExited(MouseEvent e) {

	}
	
}
