import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class GraphicalOthello extends Othello implements ActionListener
{
   private JFrame gameFrame;
   private JButton[][] buttons;
   private JPanel gridPanel;
   private JButton Random;
   private JButton First_Available;
   private JButton Greedy;
   private JPanel strategyPanel;
   public static int dimension = 4;
  
    public GraphicalOthello()
    {
        gameFrame = new JFrame(" Othello! ");
        gridPanel = new JPanel(); 
        buttons = new JButton[SIZE][SIZE];
        gridPanel.setLayout(new GridLayout(SIZE, SIZE));
        strategyPanel = new JPanel();
        strategyPanel.setLayout(new GridLayout(1, 3));
        Random = new JButton("Random");
        First_Available = new JButton("First Available");
        Greedy = new JButton("Greedy");
        strategyPanel.add(Random);
        strategyPanel.add(First_Available);
        strategyPanel.add(Greedy);
        
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                String s = "" + grid[i][j];
                buttons[i][j] = new JButton ("");
                buttons[i][j].setBackground(Color.white);
                buttons[i][j].addActionListener(this);
                buttons[i][j].setActionCommand("" + i + j);
                gridPanel.add(buttons[i][j]);
                if(grid[i][j] == 'x'){
                  buttons[i][j].setIcon(new ImageIcon("C:\\Users\\Otonye\\Downloads\\assign4-sol\\bl.jpg"));
                  buttons[i][j].setPreferredSize(new Dimension(dimension, dimension));
               }else if(grid[i][j] == 'o'){
                  buttons[i][j].setIcon(new ImageIcon("C:\\Users\\Otonye\\Downloads\\assign4-sol\\re.jpg"));
                  buttons[i][j].setPreferredSize(new Dimension(dimension, dimension));
               }else if(grid[i][j] == '_'){
                  buttons[i][j].setIcon(new ImageIcon("C:\\Users\\Otonye\\Downloads\\assign4-sol\\wh.png"));
                  buttons[i][j].setPreferredSize(new Dimension(dimension, dimension));
                }
            }
        }
        Random.setActionCommand("Random");
        Random.addActionListener(this);
        First_Available.setActionCommand("First Available");
        First_Available.addActionListener(this);
        Greedy.setActionCommand("Greedy");
        Greedy.addActionListener(this);
        gameFrame.getContentPane().add(gridPanel, BorderLayout.CENTER);
        gameFrame.getContentPane().add(strategyPanel, BorderLayout.SOUTH);
        gameFrame.setPreferredSize(new Dimension(400, 400));
        gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gameFrame.pack();
        startGame();
        gameFrame.setVisible(true);
    }

     public void actionPerformed(ActionEvent e)
    {
       JButton button = (JButton) e.getSource();
       String command = e.getActionCommand();
       
        if(command.equals("Random")){setMoveStrategy(new RandomMove());}
        else if(command == "First Available"){setMoveStrategy(new FirstAvailableMove());}
        else if(command == "Greedy"){setMoveStrategy(new GreedyMove());} 
        else{
       
            int i = Integer.parseInt(command.substring(0, 1));
            int j = Integer.parseInt(command.substring(1, 2));
            Move move = new Move(i, j);
            int status;
       
            if(canPlay(move)){
                status = play(move);
                copyAllgrid();
                toggleTurn();
            }else{ return;}
      
            status = ONGOING;
            while (status == ONGOING){
                if(!(generateMoves().isEmpty())){ 
                    status = machinePlay(); 
                    copyAllgrid();
                    print();
                    toggleTurn();
                    if(!(generateMoves().isEmpty())){  
                        if(canPlay(move)){  
                            status = play(move);
                            copyAllgrid();
                            toggleTurn();
                        } else{ return;}
                    }else if(!(generateMoves().isEmpty())) { 
                        status = machinePlay();
                        copyAllgrid();
                        print();
                        toggleTurn();
                    }else { 
                        print();
                        status = GAME_OVER;
                        JOptionPane.showMessageDialog(gameFrame,determineGame() + " the game ","Game Over", JOptionPane.INFORMATION_MESSAGE);
                        restartGame();
                    }
            
                }else if(!(generateMoves().isEmpty())){ 
                    if(canPlay(move)){  
                        status = play(move);
                        copyAllgrid();
                        toggleTurn();
                    } else{ return;}
                }else{
                    print();
                    status = GAME_OVER;
                    JOptionPane.showMessageDialog(gameFrame,determineGame() + " ","Game Over", JOptionPane.INFORMATION_MESSAGE);
                    restartGame();
                }
            }
       }
    }
    
    private void startGame()
    {
        int question = JOptionPane.showConfirmDialog(gameFrame,"Do you want to start Othello?","Start Game",JOptionPane.YES_NO_OPTION,JOptionPane.INFORMATION_MESSAGE);
        
        if(question == JOptionPane.YES_OPTION)return;
        else if(question == JOptionPane.NO_OPTION){
            status = machinePlay();
            copyAllgrid();
            print();
            toggleTurn();
            return;
        }else{
            System.exit(0);
        }
    }
    
    private void restartGame()
    {
        int question = JOptionPane.showConfirmDialog(gameFrame, "Do you want to restart Othello?","Start Game", JOptionPane.YES_NO_OPTION,JOptionPane.INFORMATION_MESSAGE);
        
        if(question == JOptionPane.YES_OPTION){
            gameFrame.dispose();
            GraphicalOthello GO = new GraphicalOthello();
        }
        else if(question == JOptionPane.NO_OPTION){
            System.exit(0);
        }else{
            System.exit(0);
        }
    }
    
    private String determineGame() {
        determineWinner();
        String str = "";
        if(status == X_WON)  return str = "Blue won the game";
        else if (status == O_WON) return str = "Green won the game"; 
        else if (status == TIE)  return str = "The Game is a TIE"; 
        else return str; 
    }
    
    private void copyAllgrid()
    {
        for(int i = 0; i < SIZE ;i++){
            for(int j =0 ; j< SIZE; j++){
                String s = "" + grid[i][j];
               // buttons[i][j].setText(s);
                 if(grid[i][j] == 'x'){
                  buttons[i][j].setIcon(new ImageIcon("C:\\Users\\Otonye\\Downloads\\assign4-sol\\bl.jpg"));
                  buttons[i][j].setPreferredSize(new Dimension(dimension, dimension));
               }else if(grid[i][j] == 'o'){
                  buttons[i][j].setIcon(new ImageIcon("C:\\Users\\Otonye\\Downloads\\assign4-sol\\re.jpg"));
                  buttons[i][j].setPreferredSize(new Dimension(dimension, dimension));
               }else if(grid[i][j] == '_'){
                  buttons[i][j].setIcon(new ImageIcon("C:\\Users\\Otonye\\Downloads\\assign4-sol\\wh.png"));
                  buttons[i][j].setPreferredSize(new Dimension(dimension, dimension));
                }
                //if ((grid[i][j] != '_')) buttons[i][j].setEnabled(false);
            }
        }
    }
    
    public void print()
    {
        for(int i = 0; i < SIZE; i++){
           for(int j = 0; j < SIZE; j++){
               if(grid[i][j] == 'x'){
                  //buttons[i][j].setText("x");
                  buttons[i][j].setIcon(new ImageIcon("C:\\Users\\Otonye\\Downloads\\assign4-sol\\bl.jpg"));
                  buttons[i][j].setPreferredSize(new Dimension(dimension, dimension));
               }else if(grid[i][j] == 'o'){
                  //buttons[i][j].setText("o");
                  buttons[i][j].setIcon(new ImageIcon("C:\\Users\\Otonye\\Downloads\\assign4-sol\\re.jpg"));
                  buttons[i][j].setPreferredSize(new Dimension(dimension, dimension));
               }
           }    
        }
    }
    
    public static void main(String args[])
    {
       GraphicalOthello GO = new GraphicalOthello();
    }
    
}