package scriptbuilder.gui.panels;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.event.MouseInputAdapter;
import scriptbuilder.gui.ScriptBuilderFrame;
import scriptbuilder.gui.ScriptBuilderGuiConstants;
import scriptbuilder.structures.ScriptIncident;
import scriptbuilder.structures.TimeSlice;

/**
 * Displays the Incident ID number and name. Holds the button to toggle
 * collapsed state.
 *
 * @author Greg Eddington <geddingt@calpoly.edu>
 * @author Bryan McGuffin
 */
public class IncidentNumberPanel extends JPanel
{

    ScriptIncident incident;
    boolean visible;

    /**
     * Listener for the mouse. Receives notifications if the mouse is clicked
     * inside the button.
     */
    private class IncidentNumberMouseListener extends MouseInputAdapter
    {

        /**
         * If the click occurs inside the icon borders, toggle the collapsed
         * state.
         *
         * @param e the mouse event
         */
        @Override
        public void mouseClicked(MouseEvent e)
        {

        }
    }

    /**
     * Constructor. Sets up the mouse listener.
     */
    public IncidentNumberPanel()
    {
        super();

        // Add the mouse listener
        IncidentNumberMouseListener mouseListener
                = new IncidentNumberMouseListener();
        addMouseMotionListener(mouseListener);
        addMouseListener(mouseListener);
    }

    /**
     * Update the dimensions of the panel.
     *
     * @param incident The incident to be represented by this panel.
     */
    public void update(ScriptIncident incident)
    {
        this.incident = incident;
        this.visible = incident != null;

        Dimension newSize;
        if (visible)
        {

            newSize = new Dimension(ScriptBuilderGuiConstants.INCIDENT_NUMBER_WIDTH,
                    ScriptBuilderGuiConstants.TIMELINE_COLLAPSED_HEIGHT
                    + ScriptBuilderGuiConstants.SCRIPT_EVENT_ICON_STEP * 2);

        }
        else
        {
            newSize = new Dimension(0, 0);
        }
        this.setSize(newSize);
        this.setPreferredSize(newSize);

        if (incident != null)
        {
            this.setToolTipText(incident.name);
        }

        invalidate();
    }

    /**
     * Refresh the panel. Choose button icon depending on collapsed state.
     *
     * @param g the graphics component
     */
    @Override
    public void paint(Graphics g)
    {
        super.paint(g);

        if (!visible)
        {
            return;
        }

        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(incident.color);
        g2d.setFont(ScriptBuilderGuiConstants.INCIDENT_NUMBER_FONT);
        g2d.drawString(Integer.toString(incident.number),
                ScriptBuilderGuiConstants.INCIDENT_NUMBER_LEFT_MARGIN,
                ScriptBuilderGuiConstants.INCIDENT_NUMBER_TOP_MARGIN);

        int y = ScriptBuilderGuiConstants.INCIDENT_NUMBER_TOP_MARGIN * 2 - 14;
        g2d.setFont(ScriptBuilderGuiConstants.INCIDENT_NAME_FONT);
        for (String line : incident.name.split(" "))
        {
            g2d.drawString(line, 20, y);
            y += 12;
        }

    }
}
