package a7;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;

public class ChessGameView extends JPanel implements Observer,MouseListener {

	BoardSpot[][] cells;
	private JPanel grid;
	private JLabel player1;
	private JLabel player2;
	private ChessPosition firstClick; 
	ArrayList<MoveListener> MoveListenerList = new ArrayList();
	
	ChessGame game;
	
	public ChessGameView(ChessGame game) {
		firstClick=null;
		this.game=game;
		game.addObserver(this); //is this correct?
		setLayout(new BorderLayout());
		
		grid = new JPanel();
		grid.setLayout(new GridLayout(8,8));
		grid.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		grid.setSize(500,500);
		add(grid,BorderLayout.CENTER);
		BoardSpot[][] cells = new BoardSpot[8][8];	
		
		this.setBackground(Color.WHITE);
		player1= new JLabel("P1");
		add(player1,BorderLayout.SOUTH);
		player2= new JLabel("P2");
		add(player2,BorderLayout.NORTH);
		
		
		

		for(int i=0;i<8;i++){
			for(int j=0;j<8;j++){
				try{
					cells[i][j] = new BoardSpot(new ChessPosition(j,i), String.valueOf(game.getBoard().getPieceAt(new ChessPosition(j,i)).mark));
					cells[i][j].addMouseListener(this);	//is this correct?
					if(i%2+j%2==1){
					cells[i][j].setBackground(Color.white);
					}else{
						cells[i][j].setBackground(Color.lightGray);
					}
					
					grid.add(cells[i][j]);
					

				}catch(java.lang.NullPointerException e){
					cells[i][j]= new BoardSpot(new ChessPosition(j,i),"");
					cells[i][j].addMouseListener(this);	
					if(i%2+j%2==1){
						cells[i][j].setBackground(Color.white);
						}else{
							cells[i][j].setBackground(Color.lightGray);
						}
					grid.add(cells[i][j]);
				}
			}
		}

		
		
		this.cells =cells;

		



		//create ui for interacting with board
		//make this an observer of the chessgame
	}

	public void mouseClicked(MouseEvent e){
		BoardSpot b = (BoardSpot) e.getSource();
		
		if(firstClick==null){
			firstClick=b.position;
		}else{
			 ChessMove move = new ChessMove(game.getBoard().getPieceAt(firstClick), firstClick, b.position, game.getBoard().getPieceAt(b.position));
		
			 for(MoveListener m:MoveListenerList){
				 m.moveAttempted(move);
			 }
			 
			 firstClick=null;
		}
		
		//check if it's the first of two
		//if it is, create a new chessmove object and inform any registered move listener objects about the move
		//by invoking the moveattempted method for each move listener
		
	}

	public void addMoveListener(MoveListener l) {
		MoveListenerList.add(l);
	}

	public void removeMoveListener(MoveListener l) {
		MoveListenerList.remove(l);
	}

	@Override
	public void update(Observable o, Object arg) {
		
		
		for(int i=0;i<8;i++){
			for(int j=0;j<8;j++){
				
					
					if(i%2+j%2==1){
						cells[i][j].setBackground(Color.white);
						}else{
							cells[i][j].setBackground(Color.lightGray);
						}

					
					if(game.getBoard().getPieceAt(cells[i][j].position)!=null){
					cells[i][j].setText(String.valueOf(game.getBoard().getPieceAt(cells[i][j].position).mark));
					
					}else{
						cells[i][j].setText("");
						
					}
			}
			}
		grid.repaint();
		
		
	}
	


	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}
}
