package scriptbuilder.structures;

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.Random;
import scriptbuilder.structures.ScriptEvent.ScriptEventType;
import scriptbuilder.structures.ScriptIncident.IncidentFocusedEvent;
import scriptbuilder.structures.ScriptIncident.SliceChangedEvent;

/**
 *
 * @author Greg Eddington <geddingt@calpoly.edu>
 */
public class SimulationScript extends Observable implements Observer
{
    public static final Color[] incidentColors = { Color.BLACK,
                                                    Color.BLUE,
                                                    Color.RED,
                                                    new Color(0x38, 0x5E, 0x0F),
                                                    new Color(128, 0, 128),
                                                    Color.MAGENTA,
                                                    new Color(0x23, 0x6B, 0x8E),
                                                    Color.ORANGE,
                                                    new Color(0x60, 0x33, 0x11),
                                                    Color.GRAY                  };
    public List<ScriptIncident> incidents;

    public SimulationScript()
    {
        incidents = new ArrayList<ScriptIncident>();

        // Create the media event
        incidents.add(new ScriptIncident(incidentColors[0], 100, "Media",
                "An incident for the media in CAD.", 10800, this));

        // Add some demo events
        incidents.add(new ScriptIncident(incidentColors[1], 174, "Blueberry Truck",
                "Blueberry truck crashed on the freeway.", 8800, this));
        incidents.add(new ScriptIncident(incidentColors[2], 175, "Construction Crash",
                "Crash at construction site on Red Road.", 6800, this));
        incidents.add(new ScriptIncident(incidentColors[3], 176, "Car Freeway Flip",
                "Car flipped across the lane divider on the freeway.", 7200, this));
        incidents.add(new ScriptIncident(incidentColors[4], 177, "Two Lane Crash",
                "Crash taking two lanes on Tree Road.", 4200, this));
        incidents.add(new ScriptIncident(incidentColors[5], 178, "Stalled Truck",
                "Truck stalled on the freeway.", 2800, this));
        //incidents.add(new ScriptIncident(incidentColors[6], 179, "Tomato Truck Spill",
        //        "Tomato trucked spilt tomatos all over the freeway.", 3200, this));
        //incidents.add(new ScriptIncident(incidentColors[7], 180, "Crash at Intersection",
        //        "Crash at the intersection of Tree Road and Red Road.", 4300, this));
        //incidents.add(new ScriptIncident(incidentColors[8], 181, "Bomb Threat",
        //        "Bomb threat and Road X.", 6000, this));*/
        incidents.add(null);
        incidents.add(null);
        incidents.add(null);

        // Create the "other" event
        incidents.add(new ScriptIncident(incidentColors[9], 999, "Other",
                "An incident for small-scale events, false events, "  +
                "and noise.  All events added to this incident will " +
                "receive a randomly generated incident number.",
                10800, this));

        Random rng = new Random();
        ScriptEventType[] eventTypes = ScriptEventType.values();

        for(int i = 0; i < 300; i++)
        {
            int n = rng.nextInt();
            int s = rng.nextInt();
            int e = rng.nextInt();
            if (n < 0) n *= -1;
            if (s < 0) s *= -1;
            if (e < 0) e *= -1;

            incidents.get(n % 6).slices.get(s % (incidents.get(n % 6)
                    .slices.size())).addEvent(new ScriptEvent(eventTypes[e % eventTypes.length]));
        }

        incidents.get(1).setOffset(200);
        incidents.get(2).setOffset(3400);
        incidents.get(3).setOffset(1400);
        incidents.get(4).setOffset(4400);
        incidents.get(5).setOffset(1400);
        //incidents.get(6).setOffset(7600);
        //incidents.get(7).setOffset(2400);
        //incidents.get(8).setOffset(4800);
    }

    public void update(Observable o, Object arg)
    {
        if (arg instanceof SliceChangedEvent )
        {
            // The slice focus has changed; pass the message
            setChanged();
            notifyObservers(arg);
        }
        else if (arg instanceof IncidentFocusedEvent)
        {
            // The slice focus has changed; pass the message
            setChanged();
            notifyObservers(arg);
        }
        else
        {
            // The script has changed, notify observers
            setChanged();
            notifyObservers(this);
        }
    }
}
