package scriptbuilder.gui.panels;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
import scriptbuilder.gui.ScriptBuilderGuiConstants;
import scriptbuilder.structures.ScriptIncident;
import scriptbuilder.structures.SimulationScript;

/**
 * Represents the timestamp bar at the top of the GUI timeline. Displays
 * timestamps at significant intervals. Updates based on zoom level and scroll
 * position.
 *
 * @author Greg Eddington
 * @author Bryan McGuffin
 */
public class TimeStampPanel extends JPanel
{

    private int longestLength = ScriptBuilderGuiConstants.TICK_TIMELINE_SMALLEST_LENGTH;

    /**
     * Just refresh the panel.
     *
     * @param zoom UNUSED
     */
    public void setZoom(float zoom)
    {
        repaint();
    }

    /**
     * Constructor.
     */
    public TimeStampPanel()
    {
        super();
    }

    /**
     * Update the length interval and the dimensions of the panel. This version
     * of the method is used in the main script builder window.
     *
     * @param script The simulation script model
     */
    public void update(SimulationScript script)
    {
        longestLength = ScriptBuilderGuiConstants.TICK_TIMELINE_SMALLEST_LENGTH;

        // Get the stats on the incidents
        int height = ScriptBuilderGuiConstants.TICK_TOP_MARGIN * 4;
        for (ScriptIncident incident : script.incidents)
        {
            if (incident != null)
            {
                height += incident.collapsed
                        ? ScriptBuilderGuiConstants.TIMELINE_OPENED_HEIGHT
                        : ScriptBuilderGuiConstants.TIMELINE_OPENED_HEIGHT;
                if ((incident.length + incident.offset) > longestLength)
                {
                    longestLength = incident.length + incident.offset;
                }
            }
        }
        for (ScriptIncident i : script.incidents)
        {
            if (i != null && i.number == 100)
            {
                i.length = longestLength;
                i.offset = 0;
            }

        }

        Dimension newSize = new Dimension(longestLength
                / ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION
                * ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK
                + ScriptBuilderGuiConstants.TICK_TIMELINE_LEFT_MARGIN + 50,
                height);
        this.setPreferredSize(newSize);
        this.setSize(newSize);

        this.invalidate();
    }

    /**
     * Update the length interval and the dimensions of the panel. This version
     * of the method is used in the individual incident editor window.
     *
     * @param incident the incident which this window is editing.
     */
    public void update(ScriptIncident incident)
    {
        longestLength = ScriptBuilderGuiConstants.TICK_TIMELINE_SMALLEST_LENGTH;

        // Get the stats on the incidents
        int height = ScriptBuilderGuiConstants.TICK_TOP_MARGIN * 4;
        if (incident != null)
        {
            height += incident.collapsed
                    ? ScriptBuilderGuiConstants.TIMELINE_OPENED_HEIGHT
                    : ScriptBuilderGuiConstants.TIMELINE_OPENED_HEIGHT;
            if ((incident.length + incident.offset) > longestLength)
            {
                longestLength = incident.length + incident.offset;
            }
        }

        if (incident != null && incident.number == 100)
        {
            incident.length = longestLength;
            incident.offset = 0;
        }

        Dimension newSize = new Dimension(longestLength
                / ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION
                * ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK
                + ScriptBuilderGuiConstants.TICK_TIMELINE_LEFT_MARGIN + 50,
                height);
        this.setPreferredSize(newSize);
        this.setSize(newSize);

        this.invalidate();
    }

    /**
     * Refresh the panel. Draw the timestamps for the appropriate intervals
     * based on zoom level.
     *
     * @param g The graphics component
     */
    @Override
    public void paint(Graphics g)
    {
        super.paint(g);

        Graphics2D g2d = (Graphics2D) g;
        g2d.setFont(ScriptBuilderGuiConstants.TIMELINE_TICK_TIME_FONT);
        g2d.setColor(ScriptBuilderGuiConstants.TIMELINE_TICK_COLOR);
        int longestLengthPlusMargin = longestLength
                / ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION
                * ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK
                + ScriptBuilderGuiConstants.TICK_TIMELINE_LEFT_MARGIN;
        int seconds = 0;

        // Major Ticks
        g2d.setColor(ScriptBuilderGuiConstants.TIMELINE_TICK_COLOR);
        seconds = 0;
        for (int i = ScriptBuilderGuiConstants.TICK_TIMELINE_LEFT_MARGIN;
                i <= longestLengthPlusMargin;
                i += ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK
                * ScriptBuilderGuiConstants.TICKS_PER_MAJOR_TICK, seconds += ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION
                * ScriptBuilderGuiConstants.TICKS_PER_MAJOR_TICK)
        {
            g2d.drawString(seconds / 3600 + ":"
                    + (seconds / 60 % 60 > 9 ? seconds / 60 % 60
                    : "0" + seconds / 60 % 60)
                    + ":00", i - 25, 18);
        }
    }
}
