package scriptbuilder.gui.drawers;

import java.awt.Graphics2D;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import scriptbuilder.gui.ScriptBuilderGuiConstants;

/**
 * Draws the cursor which marks the X position on the currently focused incident
 * timeline.
 *
 * @author Greg Eddington <geddingt@calpoly.edu>
 * @author Bryan McGuffin
 */
public class CursorDrawer
{

    /**
     * The working copy of the cursor icon to use.
     */
    private static Image cursorIconImage = null;

    /**
     * Gets the image to use for the cursor and stores it for faster subsequent
     * lookups.
     *
     * @return Cursor.png
     */
    private static Image getCursorIconImage()
    {
        if (cursorIconImage == null)
        {
            try
            {
                cursorIconImage = ImageIO.read(images.Images.getImage("Cursor.png"));
            }
            catch (IOException ex)
            {
                Logger.getLogger(EventIconDrawer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return cursorIconImage;
    }

    /**
     * Sets up the box for the cursor timestamp text, then draws the cursor at
     * the given position.
     *
     * @param g2d the graphics component
     * @param x current offset of the mouse from the center of the cursor
     * @param locked if true, timestamp is a different color
     */
    public static void DrawCursor(Graphics2D g2d, int x,
            boolean locked)
    {
        // Timestamp Border
        g2d.setColor(ScriptBuilderGuiConstants.CURSOR_TIMESTAMP_BORDER_COLOR);
        g2d.fillRect(x - ScriptBuilderGuiConstants.CURSOR_TIMESTAMP_BORDER_HORIZONTAL_CENTER,
                ScriptBuilderGuiConstants.CURSOR_TIMESTAMP_BORDER_TOP_MARGIN,
                2 * ScriptBuilderGuiConstants.CURSOR_TIMESTAMP_BORDER_HORIZONTAL_CENTER,
                ScriptBuilderGuiConstants.CURSOR_TIMESTAMP_BORDER_HEIGHT);

        // Timestamp Center
        g2d.setColor(locked ? ScriptBuilderGuiConstants.CURSOR_TIMESTAMP_LOCKED_COLOR
                : ScriptBuilderGuiConstants.CURSOR_TIMESTAMP_ACTIVE_COLOR);
        g2d.fillRect(x - ScriptBuilderGuiConstants.CURSOR_TIMESTAMP_HORIZONTAL_CENTER,
                ScriptBuilderGuiConstants.CURSOR_TIMESTAMP_TOP_MARGIN,
                2 * ScriptBuilderGuiConstants.CURSOR_TIMESTAMP_HORIZONTAL_CENTER,
                ScriptBuilderGuiConstants.CURSOR_TIMESTAMP_HEIGHT);

        // Timestamp Text
        g2d.setColor(ScriptBuilderGuiConstants.CURSOR_TIMESTAMP_TEXT_COLOR);
        int seconds = x / ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK
                * ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION;
        g2d.drawString(seconds / 3600 + ":"
                + (seconds / 60 % 60 > 9 ? seconds / 60 % 60
                : "0" + seconds / 60 % 60)
                + ":" + (seconds % 60)/10  + "0",
                x - ScriptBuilderGuiConstants.CURSOR_TIMESTAMP_TEXT_CENTER,
                ScriptBuilderGuiConstants.CURSOR_TIMESTAMP_TEXT_TOP_MARGIN);

        // Timestamp Cursor
        g2d.drawImage(getCursorIconImage(),
                x - ScriptBuilderGuiConstants.CURSOR_TIMESTAMP_ICON_CENTER,
                ScriptBuilderGuiConstants.CURSOR_TIMESTAMP_ICON_TOP_MARGIN,
                null);
    }
}
