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

//import client.TMCWebServiceClient;
//import tmc.client.HazardType;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.control.Slider;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.shape.Rectangle;
import javafx.scene.transform.Scale;
import structures.Constants;

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

    //When application becomes visible, Reset() handles initializing environment
    //override var visible = false on replace{if(visible == true){Reset();}};
    public function Reset(): Void {
    }

    var lat: Double = -1;
    var lon: Double = -1;
    var type: String = "";
    var typeNum: String = "";
    var message: String = "";
    var TMCPostData = Text {
                font: Font { size: 14 }
                fill: Color.WHITE
                content: bind "Sign Type: {type} \n""Latitude: {lat}\n""Longitude: {lon}"
                visible: false;
                wrappingWidth: Main.stage.width - 8;
            };
    var TMCResponse = Text {
                font: Font { size: 14
                }
                fill: Color.WHITE
                content: bind message
                visible: false;
                wrappingWidth: Main.stage.width - 8;
            };
    //OKButton
    var OKButton: Button = Button {
                btnIcon: CommonFunctionsFX.getImage("Ok");
                onClick: function (e: MouseEvent) {
                    if (lat != -1 and lon != -1) {
                        var result: Boolean;
                        result = webservices.WebServiceCalls.postHazard(lat, lon, typeNum);
                        TMCPostData.visible = false;
                        OKButton.visible = false;
                        CancelButton.visible = false;

                        //successful submission
                        if (result == true) {
                            message = "Hazard successfully submitted to TMC.";
                            
                        } else {
                            message = "Hazard submission failed. Try again later.";
                            
                        }

                        TMCResponse.visible = true;
                    }
                }
            }
    //Cancel Button
    var CancelButton: Button = Button {
                btnIcon: CommonFunctionsFX.getImage("Cancel");
                onClick: function (e: MouseEvent) {
                    TMCPostData.visible = false;
                    OKButton.visible = false;
                    CancelButton.visible = false;
                }
            }

    function setTMCVariables(tmpType: String, tmpTypeNum: String): Void {
        type = tmpType;
        typeNum = tmpTypeNum;
        TMCResponse.visible = false;
        lat = mdot.Main.getMe().GetObjectAsDouble(Constants.HASH_LATITUDE);
        lon = mdot.Main.getMe().GetObjectAsDouble(Constants.HASH_LONGITUDE);
        TMCPostData.visible = true;
        OKButton.visible = true;
        CancelButton.visible = true;
        java.lang.System.out.println("BUTTON: {tmpType}");
    }

    override var content = [
                Group {
                    var title: Text = Text {
                                font: Font {
                                    size: 24
                                }
                                fill: Color.WHITE
                                content: "Gov 2.0"
                            };
                    var homeButton: HomeButton =
                            HomeButton {
                                desc: "HOME"
                            }
                    //Slider
                    var slider = Slider {
                                width: Main.stage.width / 2 - 8
                                transforms: [
                                    Scale { x: 2 y: 2 }
                                ]
                                blockIncrement: 60
                                clickToPosition: true
                                min: 0
                                max: 10
                                vertical: false
                            };
                    content: [
                        Group {
                            translateX: Main.stage.width / 2 - title.boundsInLocal.width / 2;
                            translateY: (Main.stage.height / 10)
                            content: [
                                title
                            ]
                        }
                        Group {
                            content: [
                                Group {
                                    translateY: (Main.stage.height / 8) * 2.5
                                    translateX: 8
                                    content: slider
                                }
                                Group {
                                    translateX: 8
                                    translateY: 5
                                    clip: Rectangle {
                                        width: Main.stage.width
                                        height: Main.stage.height / 3;
                                    }
                                    content: [
                                        Group {
                                            translateY: (Main.stage.height / 8)
                                            translateX: bind 0 - slider.value * 30
                                            content: [
                                                HBox {
                                                    spacing: 10
                                                    content: [
                                                        GenericAppButton {
                                                            id: "pothole"
                                                            index: 0
                                                            displayimage: CommonFunctionsFX.getImage("000")
                                                            onclickAction: function (e: MouseEvent) {
                                                                setTMCVariables("Pot Hole", "000");

                                                            }
                                                        }
                                                        GenericAppButton {
                                                            id: "icy"
                                                            index: 1
                                                            displayimage: CommonFunctionsFX.getImage("001")
                                                            onclickAction: function (e: MouseEvent) {
                                                                setTMCVariables("Ice", "001");
                                                            }
                                                        }
                                                        GenericAppButton {
                                                            id: "roadkill"
                                                            index: 1
                                                            displayimage: CommonFunctionsFX.getImage("002")
                                                            onclickAction: function (e: MouseEvent) {
                                                                setTMCVariables("Road Kill", "002");
                                                            }
                                                        }
                                                        GenericAppButton {
                                                            id: "foggy"
                                                            index: 1
                                                            displayimage: CommonFunctionsFX.getImage("003")
                                                            onclickAction: function (e: MouseEvent) {
                                                                setTMCVariables("Foggy", "003");
                                                            }
                                                        }
                                                        GenericAppButton {
                                                            id: "animal"
                                                            index: 0
                                                            displayimage: CommonFunctionsFX.getImage("004")
                                                            onclickAction: function (e: MouseEvent) {
                                                                setTMCVariables("Animal", "004");
                                                            }
                                                        }
                                                        GenericAppButton {
                                                            id: "accident"
                                                            index: 0
                                                            displayimage: CommonFunctionsFX.getImage("005")
                                                            onclickAction: function (e: MouseEvent) {
                                                                setTMCVariables("Accident", "005");
                                                            }
                                                        }
                                                    ]
                                                }
                                            ]
                                        }
                                    ]
                                }
                            ]
                        }
                        Group {
                            translateX: 5
                            translateY: (Main.stage.height / 8) * 4
                            content: bind TMCPostData
                        }
                        Group {
                            translateX: 5
                            translateY: (Main.stage.height / 8) * 4
                            content: bind TMCResponse
                        }
                        Group {
                            translateX: 5
                            translateY: (Main.stage.height / 8) * 5.5
                            content: bind HBox {
                                spacing: 10
                                content: [OKButton, CancelButton]
                            }
                        }
                        Group {
                            translateX: 1
                            translateY: Main.stage.height - homeButton.boundsInLocal.height;
                            content: [
                                homeButton
                            ]
                        }
                    ]
                }
            ];

    init {
        Reset();
    }

}

//Class for OK and CANCEL buttons
public class Button extends CustomNode {

    var btnIcon: Image;
    var onClick: function(e: MouseEvent): Void;
    override var visible = false;

    public override function create(): Node {
        Group {
            blocksMouse: true;
            var size = Main.stage.width / 5;
            content: [
                ImageView {
                    fitWidth: size
                    preserveRatio: true
                    smooth: true
                    cache: true
                    image: btnIcon
                }
            ]
            onMousePressed: onClick;
        }
    }

}
