/*
 * Gov20.fx
 *
 * Created on Feb 3, 2010, 6:03:42 PM
 */
package mdot_gui;

import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
import javafx.scene.layout.HBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.control.Slider;
import javafx.scene.input.MouseEvent;
import mdot.Main;
import structures.Constants;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import client.TMCWebServiceClient;

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

    override var visible = false on replace {
                if (visible == true) {
                    Reset();
                }
            };
    var TMCResponse: Text;
    var TMCPostData: Text;
    var OKButton: Button;
    var CancelButton: Button;
    //var client: TMCWebServiceClient;
    var lat: Double = -1;
    var lon: Double = -1;
    var type: String = "";
    var typeNum: String = "";
    var message: String = "";

    public override function create(): Node {
        //visible = false;
        id = "Gov20";
        //Variables
        //client = new TMCWebServiceClient();
        TMCPostData = Text {
            translateX: mdot_gui.Constants.WINDOW_WIDTH/5;
            layoutY: 250
            font: Font { size: 20 }
            fill: Color.WHITE
            content: bind "Sign Type: {type} \n""Latitude: {lat}\n""Longitude: {lon}"
            visible: false;
        };
        TMCResponse = Text {
            translateX: mdot_gui.Constants.WINDOW_WIDTH/5;
            layoutY: 250
            font: Font { size: 20 }
            fill: Color.WHITE
            content: bind message
            visible: false;
        };
        //Scrollbar
        var scroll = Slider{
            height: 20
            translateX: 310
            translateY: (mdot_gui.Constants.APPLICATION_HEIGHT/3) + 60
            width:(mdot_gui.Constants.APPLICATION_WIDTH)/3 
            blockIncrement: 60
            clickToPosition: true
            min: 0
            max: 10
            vertical: false
            scaleX:2.5
            scaleY:2.5
            };

        //OKButton
        OKButton = Button {

            btnIcon: CommonFunctionsFX.getImage("Ok");
            onClick: function (e: MouseEvent) {
                if (lat != -1 and lon != -1) {
                    var result: Boolean;
                    try{
                    result = MainScreen.client.postHazard(lon, lat, typeNum);}
                    catch(Exception){
                            java.lang.System.out.println("ERROR - Can't post hazard to TMC");
                            }
                    //client.postHazard(0, 0, "003");
                    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
        CancelButton = Button {

            btnIcon: CommonFunctionsFX.getImage("Cancel");
            onClick: function (e: MouseEvent) {
                TMCPostData.visible = false;
                OKButton.visible = false;
                CancelButton.visible = false;
            }
        }
        //Group for header text
        var headGroup = Group {
                    content: [
                        Text {
                            layoutX: mdot_gui.Constants.WINDOW_WIDTH/5;
                            layoutY: 50
                            font: Font { size: 40 }
                            fill: Color.WHITE
                            content: "Government 2.0 Application"
                        }
                    ]
                }
        //Group that contains signs
        var mainGroup = Group {
                    translateX: bind 0 - scroll.value * 30
                    content: [
                        HBox {
                            translateX: mdot_gui.Constants.WINDOW_WIDTH/5;
                            translateY: mdot_gui.Constants.WINDOW_HEIGHT/4;
                            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 that is displayed
        Group {
            //Clipping rectangle for scrolling
            clip: Rectangle {
                translateX: mdot_gui.Constants.WINDOW_WIDTH/5;
                width: mdot_gui.Constants.APPLICATION_WIDTH;
                height: mdot_gui.Constants.APPLICATION_HEIGHT;
                fill: Color.WHITE
            }
            content: [
                headGroup,
                scroll,
                mainGroup,
                TMCPostData,
                TMCResponse,
                HBox{translateX: mdot_gui.Constants.WINDOW_WIDTH/5;
                    translateY: (mdot_gui.Constants.WINDOW_HEIGHT/4)*3;
                    spacing: 10,
                    content:[OKButton, CancelButton]}
            ]
        }
    }

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

    function Reset(): Void {
        TMCResponse.visible = false;
        TMCPostData.visible = false;
        OKButton.visible = false;
        CancelButton.visible = false;
    }
}

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

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

    public override function create(): Node {
        Group {
            blocksMouse: true;
            layoutX: x
            layoutY: y
            var size = mdot_gui.Constants.BUTTON_SIZE * mdot_gui.Constants.BUTTON_SCALE;
            var displayimage: Image = btnIcon;
            content: [
                ImageView {
                    x: (size - displayimage.width) / 2;
                    y: (size - displayimage.height) / 2;
                    image: displayimage
                }
            ]
            onMousePressed: onClick;
        }
    }

}
