import java.awt.*;

import javax.swing.*;
import javax.swing.event.*;

/** class for presenting a game tree and one game state of the selected node side by side */
@SuppressWarnings("serial")
public class GameTreeDisplay extends JFrame implements TreeSelectionListener {

    private JPanel content;
    private GameTree gameTree;

    public GameTreeDisplay(GameTree gameTree) {
        this.setTitle("TTT Explorer");
        this.gameTree = gameTree;
        Game game = gameTree.getRootGame();
        int xSize = game.getHorizontalSize();
        int ySize = game.getVerticalSize();
        this.setSize(2*xSize*GUI.UNIT, ySize*GUI.UNIT);
        this.content = new JPanel(new GridLayout(1,2));
        JTree tree = new JTree(gameTree);
        tree.addTreeSelectionListener(this);
        JScrollPane left = new JScrollPane(tree);
        JPanel right = new GUIPanel(game);
        this.content.add(left);
        this.content.add(right);
        this.setContentPane(this.content);
        this.setLocation(100,100);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
    @Override
    public void valueChanged(TreeSelectionEvent event) {
        Object selectedNode = event.getPath().getLastPathComponent();
        Game selectedGame = this.gameTree.getGameForNode(selectedNode);
        this.content.remove(1);
        this.content.add(new GUIPanel(selectedGame));
        this.validate();
    }
}
