/*
 *  T-Mass simulation calendar event class
 *
 *  2004 - 2011 Frantisek Zboril jr.
 *  Faculty of Information Technology
 *  zborilf@fit.vutbr.cz
 *  http://www.fit.vutbr.cz/~zborilf
 *
 * (C) Brno University of Technology
 * Freeware licence VUT v Brně
 * https://wis.fit.vutbr.cz/FIT/db/dir.php/dr/VZ2007/externiInformace/softwareNaWebu/Priloha2_rozh23.pdf

 */

package jTMass.core.jTMassSimulator.jTCalendar;


import jTMass.core.jTMassModel.TModelElement;
import jTMass.core.jTMassSimulator.TSimulator;

public class TCalendarEvent {

    public static final int __EventDimension=6;

    //  X          X        X          X        X       X
    //READII   INP_PROC   EVOLVE    OUTP_GEN  MOVE  TERMINATE

    public static final int _EventWait=0;
    public static final int _EventReadInputs=1;
    public static final int _EventProcessInputs=2;
    public static final int _EventReceiveInputs=3;
    public static final int _EventEvolve=4;
    public static final int _EventProcess=7;
    public static final int _EventSetOutputs=8;
    public static final int _EventExecute=15;
    public static final int _EventReconnect=16;
    public static final int _EventTerminate=32;

    public static final String _EVENTSYMBOLS="RIEOMT";

    private int PEventType;
    private TModelElement PElement;
    private TModelElement PEventGeneratedBy;
    private Long PEventGeneratedTime, PEventExecutionTime;

    // ===== READERS/WRITERS =====

    public String getElementS(){
        return(PElement.getElementName());
    }

    public String getGenTimeS(){
        return(PEventGeneratedTime.toString());
    }

    public String getFireTimeS(){
        return(PEventExecutionTime.toString());
    }

    public String getEventCode(int eventType){
        String eventCode="";
        int mask=1;

        for(int i=0; i<__EventDimension; i++){
            if((eventType&mask)>0)
                eventCode=eventCode.concat(_EVENTSYMBOLS.substring(i,i+1));
            mask=mask*2;
        }
        return(eventCode);
    }

    public String getEventTypeS(){
        switch(PEventType){
            case TCalendarEvent._EventWait: return("Wait");
            case TCalendarEvent._EventEvolve: return("Evolve");
            case TCalendarEvent._EventProcess: return("Process");
            case TCalendarEvent._EventReadInputs: return("Read Inputs");
            case TCalendarEvent._EventSetOutputs: return("Read Outputs");
            case TCalendarEvent._EventExecute: return("Execute");
            case TCalendarEvent._EventTerminate: return("Terminate");
            case TCalendarEvent._EventReconnect: return("Reconnect");
        }
        return(getEventCode(PEventType));
    }

    public TModelElement getElement(){
        return(PElement);
    }

    public String getGeneratedBy(){
        if(PEventGeneratedBy==null)
            return("T-Mass");
        return(PEventGeneratedBy.getElementName());
    }

    public long getFireTime(){
        return(PEventExecutionTime);
    }

    public int getEventType(){
        return PEventType;
    }

    public void setEventType(int type){
        PEventType=type;
    }

    public void fireEvent(TSimulator simulator){
        simulator.setModelTime(PEventExecutionTime);
        if(PEventType==0)
            PElement.waitEvent();
        if((PEventType&_EventReadInputs)>0)
            PElement.readInputs();
        if((PEventType&_EventProcessInputs)>0)
            PElement.processInputs();
        if((PEventType&_EventEvolve)>0)
            PElement.evolveElement();
        if((PEventType&_EventSetOutputs)>0)
            PElement.setOutputs();
    }

    // ===== CONSTRUCTOR =====

    public TCalendarEvent(TModelElement element, int eventType, Long genTime, Long exeTime,
                                    TModelElement source){
        PElement=element;
        PEventType=eventType;
        PEventGeneratedBy=source;
        PEventGeneratedTime=genTime;
        PEventExecutionTime=exeTime;
    }

}