package a8;

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.JButton;
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 JLabel turnIndicator;
	private JLabel whoseTurn;
	private JButton undo;
	private JPanel rightSide;
	private JPanel moves;
	private JPanel capturedP1;
	private JPanel capturedP2;
	
	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(1500,1500);
		add(grid,BorderLayout.CENTER);
		BoardSpot[][] cells = new BoardSpot[8][8];	
		
		this.setBackground(Color.WHITE);
		player1= new JLabel("P2");
		player1.setHorizontalAlignment(getWidth()/2);
		//add(player1,BorderLayout.SOUTH);
		player2= new JLabel("P1");
		player2.setHorizontalAlignment(getWidth()/2);
		//add(player2,BorderLayout.NORTH);
		turnIndicator= new JLabel("Turn:");
		whoseTurn = new JLabel("P2");
		
		
		capturedP1 = new JPanel();
		capturedP2 = new JPanel();
		capturedP1.add(player1,BorderLayout.SOUTH);
		capturedP2.add(player2,BorderLayout.SOUTH);
		
		
		add(capturedP1,BorderLayout.SOUTH);
		add(capturedP2,BorderLayout.NORTH);
		
		JPanel leftSide = new JPanel();
		undo= new JButton("Undo");
		undo.setSize(10,100);
		undo.addMouseListener(this);
		leftSide.add(undo,BorderLayout.WEST);
		
		
		add(leftSide,BorderLayout.WEST);
		
		
		moves = new JPanel();
		moves.setLayout(new GridLayout(50,0));
		
		
		JPanel rightSide = new JPanel();
		rightSide.add(turnIndicator,BorderLayout.NORTH);
		rightSide.add(whoseTurn,BorderLayout.EAST);
		rightSide.add(moves,BorderLayout.SOUTH);
		add(rightSide,BorderLayout.EAST);
		
		
		
		
		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){
		if(e.getSource().getClass().equals(JButton.class)){
			game.undo();
		}else{
		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){
				 try {
					m.moveAttempted(move);
				} catch (WrongTurn e1) {
					// TODO Auto-generated catch block
					System.out.println("Not your turn, bro");
				}
			 }
			 
			 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) {
		
		ChessMove[] allMoves = game.getMoves(0);
		
		moves.removeAll();
		capturedP1.removeAll();
		capturedP2.removeAll();
		capturedP1.add(player1,BorderLayout.SOUTH);
		capturedP2.add(player2,BorderLayout.SOUTH);
		for(int i=0;i<allMoves.length;i++){
			JLabel move = new JLabel(allMoves[i].toString());
			
			moves.add(move);
		}
		String p1Captured = "Captured: ";
		String p2Captured = "Captured: ";
		for(int i=0;i<allMoves.length;i++){
			if(allMoves[i].pieceWasCaptured()){
			if(allMoves[i].getCaptured().getOwner().getName()=="P1"){
				//capturedP1.add(new JLabel(String.valueOf(allMoves[i].getCaptured().getMark())));
				p1Captured+=String.valueOf(allMoves[i].getCaptured().getMark())+",";
			}else{
				p2Captured+=String.valueOf(allMoves[i].getCaptured().getMark())+",";
			}
			}
		}
		capturedP1.add(new JLabel(p1Captured));
		capturedP2.add(new JLabel(p2Captured));
		
		if(this.game.getPlayer1Move()){
			this.whoseTurn.setText("P2");
		}else{
			this.whoseTurn.setText("P1");
		}
		
		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

	}
}
