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

import javafx.scene.Group;
import javafx.scene.layout.VBox;
import javafx.scene.control.Slider;
import javafx.scene.transform.Scale;
import javafx.scene.Scene;
import javafx.scene.CustomNode;
import javafx.scene.Node;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import com.sun.javafx.runtime.sequence.Sequence;
import structures.Constants;
import webservices.WebServiceCalls;
import java.util.List;

/**
 * @author Ali
 */
public class AdvisoryAlert extends Scene {

    var alertTable: VBox = VBox {
                spacing: 2
                content: []
            }

    //When application becomes visible, Reset() handles initializing environment
    //override var visible = false on replace{if(visible == true){Reset();}};
    public function Reset(): Void {
        var result: Sequence;
        result = ConvertFromList();
        //Clear all the old signs from the table
        delete  alertTable.content;
        //java.lang.System.out.println(result.size());
        for (alerts in result) {
            var myAlert = AlertRow {
                        image: (alerts as Alert).code.toString();
                        desc: (alerts as Alert).desc;
                        
                    }
            java.lang.System.out.println("###############{myAlert.desc}##############");
            //Insert the new signs into the table
            insert myAlert into alertTable.content;
        }
        //redraw the table
        alertTable.layout();
        java.lang.System.out.println("Content: {alertTable.content.size()}");
    }

    function ConvertFromList() {
        var mySeq: Sequence;      
        
        //Read the list of alerts from the web service
        var lat = mdot.Main.getMe().GetObjectAsDouble(Constants.HASH_LATITUDE);
        var lon = mdot.Main.getMe().GetObjectAsDouble(Constants.HASH_LONGITUDE);
        
        var hazards:List;
        var response: String;

       
        if(lat != -1 and lon != -1){
            //THIS IS LONGITUDE, LATITUDE NOT LATITUDE, LONGITUDE (like the rest of the code)
            //hazards = WebServiceCalls.getHazardList(lat, lon);
            hazards = WebServiceCalls.getHazardList(Double.parseDouble("0.0"), Double.parseDouble("0.0"));
        
        }        
        //Change data from List to type "Alert"
        for (alert in hazards) {
        var myAlert: Alert;
        alert as tmc.ws.expose2.HazardType;
        //instantiate a new alert
        myAlert = Alert{
        code: ((alert as tmc.ws.expose2.HazardType).getCategory() as tmc.ws.expose2.HazardCategoryType).getCode() as String;
        desc: ((alert as tmc.ws.expose2.HazardType).getCategory() as tmc.ws.expose2.HazardCategoryType).getDescription() as String;
        };

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

        //var txt:String = WebServiceCalls.HelloWorld();
       //java.lang.System.out.println("###############{txt}##############");

        /*
            var tmp: Alert = Alert {
                        code: "000_small"
                        desc: "Pothole"
                    }

           var tmp2: Alert = Alert {
                        code: "001_small"
                        desc: "Icy"
                    }

            var tmp3: Alert = Alert {
                        code: "005_small"
                        desc: "Accident"
                    }

            insert tmp into mySeq;
            insert tmp2 into mySeq;
            insert tmp3 into mySeq;
        */
        //return all the alerts in a sequence
        return mySeq
    }

    override var content = [
                Group {
                    var title: Text = Text {
                                font: Font {
                                    size: 24
                                }
                                fill: Color.WHITE
                                content: "Advisory Alert"
                            };
                    var homeButton: HomeButton =
                            HomeButton {
                                desc: "HOME"
                            }
                    //Slider
                    var slider = Slider {
                                height: ((Main.stage.height) / 2 - homeButton.boundsInLocal.height / 2 - title.boundsInLocal.height / 2) - 16
                                transforms: [
                                    Scale { x: 2 y: 2 }
                                ]
                                blockIncrement: 60
                                clickToPosition: true
                                min: 0
                                max: 10
                                vertical: true
                            }
                    content: [
                        Group {
                            translateX: Main.stage.width / 2 - title.boundsInLocal.width / 2;
                            translateY: (Main.stage.height / 10)
                            content: [
                                title
                            ]
                        }
                        Group {
                            translateY: Main.stage.height / 8
                            translateX: 10
                            content: [
                                Group {
                                    translateX: (Main.stage.width / 5) * 4
                                    translateY: 8
                                    content: slider
                                }
                                Group {
                                    translateX: 5
                                    translateY: 5
                                    clip: Rectangle {
                                        width: Main.stage.width
                                        height: Main.stage.height - homeButton.boundsInLocal.height - title.boundsInLocal.height - Main.stage.height / 8;
                                    }
                                    content: [
                                        Group {
                                            translateY: bind 0 - slider.value * 30
                                            content: bind alertTable
                                        }
                                    ]
                                }
                            ]
                        }
                        Group {
                            translateX: 1//Main.stage.width/2 - toggle.boundsInLocal.width/2;
                            translateY: Main.stage.height - homeButton.boundsInLocal.height;
                            content: [
                                homeButton
                            ]
                        }
                    ]
                }
            ];

    init {
        Reset();
    }

}


/*
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.
 */
/* This function converts a List into a sequence.
 ** It is necessary because javafx works with
 ** sequences and NOT lists.
 */
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 {
            var myRect: Rectangle = Rectangle {
                        width: (Main.stage.width / 4) * 3
                        height: Main.stage.height / 8
                        fill: Color.DARKGRAY
                    }
            var imgView = ImageView {
                        image: CommonFunctionsFX.getImage(image)
                        fitWidth: (myRect.height / 3) * 2
                        preserveRatio: true
                        smooth: true
                        cache: true
                    }
            var txt = Text {
                        font: Font {
                            size: 16
                        }
                        fill: Color.WHITE
                        content: desc
                    }
            content: [
                myRect,
                //Displays the image

                Group {
                    translateX: myRect.boundsInLocal.minX + 5;
                    translateY: myRect.height / 2 - imgView.fitWidth / 2
                    content: imgView
                }
                Group {
                    translateX: imgView.boundsInLocal.maxX + 10
                    translateY: myRect.height / 2 + txt.boundsInLocal.height / 2
                    content: txt
                }
            ]
        }
    }

}

