package scriptbuilder.gui.drawers;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
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;
import scriptbuilder.structures.ScriptEvent;
import scriptbuilder.structures.TimeSlice;
import scriptbuilder.structures.events.I_ScriptEvent;

/**
 * Draws the icons present in one timeslice, in one incident.
 *
 * @author Greg Eddington <geddingt@calpoly.edu>
 * @author Bryan McGuffin
 */
public class TimeSliceDrawer
{

    /**
     * The icon which indicates that there are events present in this timeSlice.
     */
    private static Image sliceEventImage = null;

    /**
     * The default zoom level.
     */
    public static int zoom = 20;

    /**
     * Get the icon to use whenever events are present in the slice. If we don't
     * already have a copy, harvest it from a file.
     *
     * @return The existing copy of Event.png
     */
    private static Image getSliceEventImage()
    {
        if (sliceEventImage == null)
        {
            try
            {
                sliceEventImage = ImageIO.read(images.Images.getImage("Event.png"));
            }
            catch (IOException ex)
            {
                Logger.getLogger(EventIconDrawer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return sliceEventImage;
    }

    /**
     * Draw the events in this timeslice. If any exist, draw the slice event
     * image. If the incident isn't collapsed, draw the event icons as well.
     *
     * @param g2d the graphics component
     * @param slice the timeSlice to draw
     * @param collapsed if true, don't draw the event icons
     */
    public static void DrawTimeSlice(Graphics2D g2d, TimeSlice slice,
            boolean collapsed)
    {
        // If this is an empty slice, return
        if (slice.events.size() == 0)
        {
            return;
        }
        //System.out.println("new slice");
        // Draw the slice event icon to show that there are events here
        g2d.drawImage(getSliceEventImage(), slice.getX()
                - ScriptBuilderGuiConstants.EVENT_ICON_WIDTH,
                ScriptBuilderGuiConstants.TIMELINE_EVENT_ICON_MARGIN, null);

        // If the incident isn't collapsed, draw the event icons
        if (!collapsed)
        {
            for (int i = 0; i < slice.events.size(); i++)
            {
                if (i >= ScriptBuilderGuiConstants.MAX_NUMBER_OF_SCRIPT_EVENT_ICON)
                {
                    break;
                }

                I_ScriptEvent event = slice.events.get(i);
                EventIconDrawer.DrawEventIcon(g2d, event, slice.getX()
                        - ScriptBuilderGuiConstants.EVENT_ICON_WIDTH,
                        ScriptBuilderGuiConstants.SCRIPT_EVENT_ICON_TOP_MARGIN
                        + i * ScriptBuilderGuiConstants.SCRIPT_EVENT_ICON_STEP);
            }
        }
    }
    
    /**
     * Draw the events in this timeslice. If any exist, draw the slice event
     * image. If the incident isn't collapsed, draw the event icons as well.
     *
     * @param g2d the graphics component
     * @param slice the timeSlice to draw
     */
    public static void DrawScriptBuilderTimeSlice(Graphics2D g2d, TimeSlice slice)
    {
        // If this is an empty slice, return
        if (slice.events.size() == 0)
        {
            return;
        }

        // Draw the slice event icon to show that there are events here
        g2d.drawImage(getSliceEventImage(), slice.getX()
                - ScriptBuilderGuiConstants.EVENT_ICON_WIDTH,
                ScriptBuilderGuiConstants.TIMELINE_EVENT_ICON_MARGIN, null);
    }
}
