/*
 * AdvisoryAlert.fx
 *
 * Created on Feb 3, 2010, 12:21:43 PM
 */

package mdot_gui;

import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.paint.Color;
import javafx.scene.layout.VBox;
import javafx.scene.layout.HBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import com.sun.javafx.runtime.sequence.Sequence;
import javafx.scene.shape.Rectangle;
import javafx.scene.control.ScrollBar;
import javafx.scene.control.Slider;
import javafx.scene.text.TextAlignment;
import client.TMCWebServiceClient;
//import tmc.client.HazardType;
import edu.wmich.www.cs.mdot.tmc.msg.HazardType;
import javafx.scene.image.ImageView;

import mdot.Main;
import structures.Constants;

/**
 * @author Ali
 */

public class AdvisoryAlert extends CustomNode {

    //When application becomes visible, Reset() handles initializing environment
    override var visible = false on replace{if(visible == true){Reset();}};
    var alertTable: VBox =  VBox{
                                spacing:2
                                content: []
                            };

    public override function create(): Node {
        //visible = false;
        id = "AdvisoryAlert";

        //Slider
        var slider = Slider{
            height: (mdot_gui.Constants.APPLICATION_HEIGHT/3) - 25
            translateX: 500
            translateY: (mdot_gui.Constants.APPLICATION_HEIGHT/3) + 70
            width:20
            blockIncrement: 60
            clickToPosition: true
            min: 0
            max: 10
            vertical: true
            scaleX:2.5
            scaleY:2.5
            };

        var mainGroup = Group {
            content: [
                Text {
                    translateX: mdot_gui.Constants.WINDOW_WIDTH/6 + 40;
                    translateY: mdot_gui.Constants.WINDOW_HEIGHT/5
                    font : Font {size: 40}
                    fill: Color.WHITE
                    content: "Advisory Alert Application"
                }

                HBox {
                    translateX: mdot_gui.Constants.WINDOW_WIDTH/3 + 5;
                    translateY: mdot_gui.Constants.WINDOW_HEIGHT/5 + 20;
                    spacing: 50
                    content:[
                        txtCode,
                        txtDesc
                    ]
                }
            ]
        };

        //Group that handles translating the table
        var tableGroup = Group {
            translateY: bind 0 - slider.value * 30
            content: alertTable
        };

        //Group for clipping the display area
        var tableDisplay = Group {
            translateX: mdot_gui.Constants.WINDOW_WIDTH/3;
            translateY: mdot_gui.Constants.WINDOW_HEIGHT/5 + 60;
            content: [

                     
                tableGroup

            ]
            clip:Rectangle {
                width: mdot_gui.Constants.APPLICATION_WIDTH;
                height:mdot_gui.Constants.APPLICATION_HEIGHT - 160;
            }
        };

        //Group that is displayed
        Group {
            content: [
                mainGroup,
                //scroll,
                tableDisplay,
                slider
            ]
        }
    }
    
    var txtCode = Text {
                        font: Font {size: 20}
                        fill: Color.RED
                        content: "Alert"
                        }

    var txtDesc = Text {
                        font: Font {size: 20}
                        fill: Color.RED
                        content: "Description"
                        }


    /* Create the Grid. For each alert in the
    ** sequence (returned by ConvertFromList),
    ** create a row.
    */
    function Reset():Void{

        var result:Sequence;
        result = ConvertFromList();

        //Clear all the old signs from the table
        delete alertTable.content;

        for (alerts in result)
        {
            var myAlert = AlertRow{
                image: (alerts as Alert).code.toString();
                desc: (alerts as Alert).desc;
            }

            //Insert the new signs into the table
            insert myAlert into alertTable.content;
        }

        //redraw the table
        alertTable.layout();

    }
}

/* This function converts a List into a sequence.
** It is necessary because javafx works with
** sequences and NOT lists.
*/
public function ConvertFromList()
{
    var mySeq: Sequence;

    //Read the list of alerts from the web service
    //var client: TMCWebServiceClient = new TMCWebServiceClient();
    
    var lat = Main.getMe().GetObjectAsDouble(Constants.HASH_LATITUDE);
    var lon = Main.getMe().GetObjectAsDouble(Constants.HASH_LONGITUDE);


    var hazardList;
    if(lat != -1 and lon != -1){

        //THIS IS LONGITUDE, LATITUDE NOT LATITUDE, LONGITUDE (like the rest of the code)
        try{
        hazardList = MainScreen.client.getHazardList(lon, lat);
        }
        catch(Exception){
                            java.lang.System.out.println("ERROR - Can't retrieve hazard list from TMC.");
                            }
    }


    //Change data from List to type "Alert"
    for (alert: HazardType in hazardList) {
        var myAlert: Alert;

        //instantiate a new alert
        myAlert = Alert{
            code: alert.getCategory().getCode() as String;
            desc: alert.getCategory().getDescription() as String;
        };

        //Insert the alert into the returned sequence
        insert myAlert into mySeq;
    }

    //return all the alerts in a sequence
    return mySeq;
}


public class Alert
{
    public var code: String;
    public var desc: String;
}

//Defines an entry for the alert table
public class AlertRow extends CustomNode{

    var image:String;
    var desc:String = "HelloWorld";
    public override function create():Node{
       
        Group{
            content:[
                Rectangle{
                    width:200
                    height:60
                    fill:Color.BLACK
                }
                //Displays the image
                ImageView {
                    image: CommonFunctionsFX.getImage(image)
                }
                Text {
                    translateX:105
                    translateY:38
                    font :  Font {
                                size:16
                            }
                    fill:Color.WHITE
                    content: desc
                }
            ]
        }
    }
}