package scriptbuilder.gui.drawers;

import java.awt.Graphics2D;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import scriptbuilder.structures.ScriptEvent;
import scriptbuilder.structures.ScriptEvent.ScriptEventType;
import images.*;
import scriptbuilder.structures.events.I_ScriptEvent;

/**
 * Draws the icon for one event. Maintains a map of all previously used images
 * for faster lookup.
 *
 * @author Greg Eddington <geddingt@calpoly.edu>
 * @author Bryan McGuffin
 */
public class EventIconDrawer
{

    /**
     * Mapping of all previously used icon images. Keys: The string name of the
     * image. Values: The image object.
     */
    private static Map<String, Image> images = null;

    private static Image radioEventImage = null;
    private static Image paramicsEventImage = null;
    private static Image cadEventImage = null;
    private static Image evaluationEventImage = null;

    /**
     * Get the image icon which corresponds to the given name. If the image map
     * hasn't been initialized, do so and give it a copy of each image.
     *
     * @param name Name of the image to find
     * @return Image object corresponding to that name
     */
    private static Image getImage(String name)
    {
        if (images == null)
        {
            try
            {
                images = new HashMap<String, Image>();
                for (ScriptEventType t : ScriptEventType.values())
                {
                    images.put(t.IMAGE_NAME, ImageIO.read(Images.getImage(t.IMAGE_NAME + ".png")));
                }
            }
            catch (IOException ex)
            {
                Logger.getLogger(EventIconDrawer.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
            }
        }

        return images.get(name);
    }

    private static Image getRadioEventImage()
    {
        if (radioEventImage == null)
        {
            try
            {
                radioEventImage = ImageIO.read(Images.getImage("Radio.png"));
            }
            catch (IOException ex)
            {
                Logger.getLogger(EventIconDrawer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return radioEventImage;
    }

    private static Image getParamicsEventImage()
    {
        if (paramicsEventImage == null)
        {
            try
            {
                paramicsEventImage = ImageIO.read(Images.getImage("Paramics.png"));
            }
            catch (IOException ex)
            {
                Logger.getLogger(EventIconDrawer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return paramicsEventImage;
    }

    private static Image getCadEventImage()
    {
        if (cadEventImage == null)
        {
            try
            {
                cadEventImage = ImageIO.read(Images.getImage("CAD.png"));
            }
            catch (IOException ex)
            {
                Logger.getLogger(EventIconDrawer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return cadEventImage;
    }

    private static Image getEvaluationEventImage()
    {
        if (evaluationEventImage == null)
        {
            try
            {
                evaluationEventImage = ImageIO.read(Images.getImage("Evaluation.png"));
            }
            catch (IOException ex)
            {
                Logger.getLogger(EventIconDrawer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return evaluationEventImage;
    }

    /**
     * Given an event, draw its icon.
     *
     * @see DrawEventIcon(Graphics2D g2d, ScriptEventType eventType, int x, int
     * y)
     */
    public static void DrawEventIcon(Graphics2D g2d, I_ScriptEvent event,
            int x, int y)
    {
        DrawEventIcon(g2d, event.getScriptEventType(), x, y);
    }

    /**
     * Given an event type, draw the icon relevant to that type.
     *
     * @param g2d the graphics component
     * @param eventType The type of event to draw
     * @param x X position of the icon
     * @param y Y position of the icon
     */
    public static void DrawEventIcon(Graphics2D g2d, ScriptEventType eventType,
            int x, int y)
    {
        g2d.drawImage(getImage(eventType.IMAGE_NAME), x, y, null);
        /**
         * switch(eventType) { case CHP_RADIO_EVENT: break; case PARAMICS_EVENT:
         * g2d.drawImage(getParamicsEventImage(), x, y, null); break; case
         * CAD_EVENT: g2d.drawImage(getCadEventImage(), x, y, null); break; case
         * CAD_EVAL_EVENT: g2d.drawImage(getEvaluationEventImage(), x, y, null);
         * break; }
         */
    }
}
