/* Generated by Together */

package gui;

import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JComponent;
import javax.swing.JTabbedPane;
import javax.swing.border.*;
import java.util.List;
import evolution.events.*;
import evolution.WorldController;

public class InfoPanelContainer extends JPanel implements Observer {

    public InfoPanelContainer(WorldController worldController, String title, String iconPath) {
        super();
        this.worldController = worldController;
        this.title = title;
        this.iconPath = worldController.getResourcePath() + iconPath;
        init();
        setPreferences();
        addComponents();
    }

    protected void init() {
        EventManager.getInst().subscribe(this, SquareSelectedEvt.class);
        EventManager.getInst().subscribe(this, ClockTickEvt.class);
        this.tabbedPane = new JTabbedPane();
        tabbedPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        initPanels();
    }
    
    protected void initPanels() {
        resourcesInfo = new InfoPanel(worldController.getResourcesInfo(), "World resources", iconPath, true);
        squareInfo = new InfoPanel(worldController.getSquareInfo(), null, iconPath, false);
        humanInfo = new InfoPanel(worldController.getHumanInfo(), null, iconPath, false);
        constructionInfo = new InfoPanel(worldController.getConstructionInfo(), null, iconPath, false);
    }

    protected void setPreferences() {
        setBorder(new CompoundBorder(new EmptyBorder(5, 5, 5, 5), new SoftBevelBorder(BevelBorder.RAISED))); 
        setLayout(new BorderLayout());
    }

    protected synchronized void squareSelectedAction() {
        refreshPanels();
    }

    protected synchronized void clockTickAction() {
        refreshPanels();
    }

    protected void refreshPanels() {
       resourcesInfo.refresh(worldController.getResourcesInfo());
       squareInfo.refresh(worldController.getSquareInfo());
       humanInfo.refresh(worldController.getHumanInfo());
       constructionInfo.refresh(worldController.getConstructionInfo());
    }
           
    protected void addComponents() {
        tabbedPane.addTab("Square", null, squareInfo, "Info about the selected square");
        tabbedPane.addTab("Human", null, humanInfo, "Info about the selected human");
        tabbedPane.addTab("Construction", null, constructionInfo, "Info about the selected construction");
        add(BorderLayout.NORTH, resourcesInfo);
        add(BorderLayout.CENTER, tabbedPane);
        revalidate();
    }

    public void update(Event event) {
        if (event instanceof SquareSelectedEvt) {
            squareSelectedAction();
        }
        if (event instanceof ClockTickEvt) {
            clockTickAction();
        }
    }

    private static String title;
    private static String iconPath;
    private WorldController worldController;
    private JTabbedPane tabbedPane;
    /** A panel with information about the resources. */
    private InfoPanel resourcesInfo;
    /** A panel with information about the selected square. */
    private InfoPanel squareInfo;
    /** A panel with information about the human on the selected square. */
    private InfoPanel humanInfo;
    /** A panel with information about the construction on the selected square. */
    private InfoPanel constructionInfo;
}
