package net.woodyfolsom.msproj.gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import net.woodyfolsom.msproj.Action;
import net.woodyfolsom.msproj.GameConfig;
import net.woodyfolsom.msproj.GameState;
import net.woodyfolsom.msproj.Player;
import net.woodyfolsom.msproj.sfx.SfxPlayer;

public class Goban extends JFrame {
	
	private static final long serialVersionUID = 1L;

	private GridPanel gridPanel;
	private SfxPlayer sfxPlayer;
	
	public Goban(GameConfig gameConfig, Player guiPlayer) {
		setLayout(new BorderLayout());
		
		addWindowListener(new WindowAdapter() {
			@Override
	        public void windowClosing(WindowEvent e) {
				sfxPlayer.cleanup();
	        }
		});
		
		try {
			sfxPlayer = new SfxPlayer();
		} catch (Exception ex) {
			System.out.println("Unable to initialize click sound due to: " + ex.getMessage());
		}
		
		this.gridPanel = new GridPanel(gameConfig, guiPlayer, sfxPlayer);
		add(gridPanel,BorderLayout.CENTER);
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		
		JButton passBtn = new JButton("Pass");
		JButton resignBtn = new JButton("Resign");
		
		passBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				gridPanel.addAction(Action.PASS);
			}});
		
		resignBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				gridPanel.addAction(Action.RESIGN);
			}});
		
		JPanel bottomPanel = new JPanel();
		bottomPanel.add(passBtn);
		bottomPanel.add(resignBtn);
		
		add(bottomPanel, BorderLayout.SOUTH);
		pack();
	}

	public Action getAction() {
		return gridPanel.getAction();
	}
	
	public void setGameState(GameState gameState) {
		gridPanel.setGameState(gameState);
	}

}