/*
 * Main.fx
 *
 * Created on Mar 23, 2010, 12:32:58 PM
 */
package vismobile;

import javafx.stage.*;
import vismobile.MainScreen;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.animation.Timeline;
import javafx.animation.KeyFrame;
import com.sun.javafx.runtime.sequence.Sequence;
import javafx.scene.image.Image;
import vismobile.CommonFunctionsFX;

/**
 * @author Student
 */
public class Main {

       var myStage: Stage;


     var timeLine: Timeline;
     var allRects: Sequence;

     init{

     myStage = Stage {

            title:  "VIS Mobile"
            width: Screen.primary.visualBounds.width;
            height: Screen.primary.visualBounds.height;
            style: StageStyle.UNDECORATED
            scene: bind scene//MainScreen{width:400 height:400}
        }

        allRects = CommonFunctionsFX.getAllRects();

        var timeLine = Timeline {
                    repeatCount: Timeline.INDEFINITE
                    keyFrames: [
                        KeyFrame {
                            time: 500ms
                            canSkip: true
                            action: function () {
                                /*
                                //If TRUE, we should request the list of messages from TMC
                                if(VMSReqest == true and VMSRequestTimeCounter == 0)
                                {
                                //call to webservice
                                var time:XMLGregorianCalendar;
                                var cal:GregorianCalendar = new GregorianCalendar();
                                cal.setTime(cal.getTime());
                                time = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);

                                java.lang.System.out.println(time);
                                var VMSlist;

                                //Catch webservice exceptions
                                try{
                                debugPrinter.printLine("CALLING WEBSERVICE!");
                                VMSlist = client.getVMSList(Main.meString, time);
                                }
                                catch(Exception)
                                {
                                debugPrinter.printLine("TMC connection error - VMS.");
                                }

                                if(VMSlist != null){
                                for (tmpMsg:VMSMessage in VMSlist) {

                                //Create a new message
                                var myMessage: VMSMessages = VMSMessages{
                                message: tmpMsg.getMessage();
                                time: tmpMsg.getDisplayTime();
                                }

                                insert myMessage into VMSMessageSequence;
                                }}


                                VMSMessageQueueSize = VMSMessageSequence.size();

                                if(VMSMessageQueueSize != 0){
                                VMSReqest = false;}
                                }

                                //There are still signs to display
                                if(VMSReqest == false)
                                {
                                if(VMSMessageTimeCounter == VMSMessageTimeToDisplay)
                                {
                                //time is up, choose another sign
                                VMSMessageQueueSize = VMSMessageQueueSize - 1;

                                if(VMSMessageQueueSize < 0){
                                VMSMessage = "";
                                delete VMSMessageSequence;
                                VMSMessageQueueSize = 0;
                                VMSReqest = true;}
                                else{

                                //get the message from the list
                                //display the message in VMS area
                                var currentMessage:VMSMessages;
                                currentMessage = VMSMessageSequence[VMSMessageQueueSize];
                                VMSMessage = currentMessage.message;

                                //set the time to be displayed
                                VMSMessageTimeToDisplay = currentMessage.time;

                                //reset the counter to 0
                                VMSMessageTimeCounter = 0;
                                }
                                }
                                }

                                //Send VIN, LAT, LON to TMC
                                if(reportLocationCount == 0 and Main.getMe().GetObjectAsString(Constants.HASH_TRACKING).equals("Yes"))
                                {

                                client.reportVehicleLocation(Main.meString,
                                Main.getMe().GetObjectAsDouble(Constants.HASH_LATITUDE),
                                Main.getMe().GetObjectAsDouble(Constants.HASH_LONGITUDE));
                                }
                                 */

                                //Blink squares
                                var locationsToBlink: Sequence = CommonFunctionsFX.searchContextHashTable(activeScene);

                                if (locationsToBlink.size != 0) {
                                    animation.blink(locationsToBlink);
                                } else {
                                    animation.reset(allRects);
                                }

                                //Insert Signs
                                var seqSigns: Sequence = ImageSelectionCriteria.ReturnSigns();
                                //java.lang.System.out.println("------->{seqSigns}");
                                alertImage1 = CommonFunctionsFX.getImage(seqSigns[0] as String);
                                alertImage2 = CommonFunctionsFX.getImage(seqSigns[1] as String);
                                alertImage3 = CommonFunctionsFX.getImage(seqSigns[2] as String);
                                alertImage4 = CommonFunctionsFX.getImage(seqSigns[3] as String);

                                //reset counters
/*
                                reportLocationCount = ((reportLocationCount + 1) mod 5);
                                VMSMessageTimeCounter = (VMSMessageTimeCounter + 1) mod (VMSMessageTimeToDisplay + 1);
                                VMSRequestTimeCounter = ((VMSRequestTimeCounter + 1) mod 30);
                                 */
                                //java.lang.System.out.println("ON");

                            }
                        }
                        KeyFrame {
                            time: 1s
                            action: function () {
                                //java.lang.System.out.println("OFF")
                                animation.reset(allRects);
                            }
                        }
                    ]
                }

        timeLine.play();
        java.lang.System.out.println("W: {myStage.width}   H: {myStage.height}");

    }

}
public var stage: Stage;
public var scene: Scene;
public var activeScene: String;
public var scenelist: Scene[];

function run()   {
    //Start the Core

    var args: String[];
    //run the core code
    mdot.Main.main(args);
    //start the GUI
    stage = Main { }.myStage;
    //set initial scene
    insert MainScreen { fill: Color.BLACK } into scenelist;
    insert CarContextCourse { fill: Color.BLACK } into scenelist;
    insert CarContextFine { fill: Color.ORANGE } into scenelist;
    insert Pedestrian { fill: Color.BLACK } into scenelist;
    insert Gov20 { fill: Color.BLACK } into scenelist;
    insert AdvisoryAlert { fill: Color.BLACK } into scenelist;
    insert VMS { fill: Color.BLACK } into scenelist;
    insert Weather { fill: Color.BLACK } into scenelist;
    insert Configuration { fill: Color.BLACK } into scenelist;
    insert ConstructionZone { fill: Color.BLACK } into scenelist;
    insert PoliceCar { fill: Color.BLACK } into scenelist;
    insert Ambulance { fill: Color.BLACK } into scenelist;
    insert FireTruck { fill: Color.BLACK } into scenelist;
    insert TowTruck { fill: Color.BLACK } into scenelist;
    insert Fuel { fill: Color.BLACK } into scenelist;
    scene = scenelist[0];
    activeScene = "CarContextCourse";
}

public var courseRect1 = GUIControllerFlagStructure { };
public var courseRect2 = GUIControllerFlagStructure { };
public var courseRect3 = GUIControllerFlagStructure { };
public var courseRect4 = GUIControllerFlagStructure { };
public var courseRect5 = GUIControllerFlagStructure { };
public var courseRect6 = GUIControllerFlagStructure { };
public var courseRect7 = GUIControllerFlagStructure { };
public var courseRect8 = GUIControllerFlagStructure { };
//Flags for FINE Context
public var fineRect1 = GUIControllerFlagStructure { };
public var fineRect2 = GUIControllerFlagStructure { };
public var fineRect3 = GUIControllerFlagStructure { };
public var fineRect4 = GUIControllerFlagStructure { };
public var fineRect5 = GUIControllerFlagStructure { };
public var fineRect6 = GUIControllerFlagStructure { };
public var fineRect7 = GUIControllerFlagStructure { };
public var fineRect8 = GUIControllerFlagStructure { };
public var fineRect9 = GUIControllerFlagStructure { };
public var fineRect10 = GUIControllerFlagStructure { };
public var fineRect11 = GUIControllerFlagStructure { };
public var fineRect12 = GUIControllerFlagStructure { };
public var fineRect13 = GUIControllerFlagStructure { };
public var fineRect14 = GUIControllerFlagStructure { };
public var fineRect15 = GUIControllerFlagStructure { };
public var fineRect16 = GUIControllerFlagStructure { };
public var fineRect17 = GUIControllerFlagStructure { };
public var fineRect18 = GUIControllerFlagStructure { };
public var fineRect19 = GUIControllerFlagStructure { };
public var fineRect20 = GUIControllerFlagStructure { };
public var fineRect21 = GUIControllerFlagStructure { };
public var fineRect22 = GUIControllerFlagStructure { };
public var fineRect23 = GUIControllerFlagStructure { };
public var fineRect24 = GUIControllerFlagStructure { };
//Dynamic sign area
public var alertImage1: Image;
public var alertImage2: Image;
public var alertImage3: Image;
public var alertImage4: Image;
