/*
 * Pedestrian.fx
 *
 * Created on Mar 23, 2010, 9:54:57 PM
 */
package vismobile;

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

/**
 * @author Student
 */
public class Fuel extends Scene {

    var myRect: Rectangle;

    var fuelTable: VBox = VBox {
                spacing: 5
                content: []
            }

    public function Reset(): Void {
        var result: Sequence;
        result = ConvertFromList();
        //Clear all the old signs from the table
        delete  fuelTable.content;

        
        for (stations in result) {
            var myFuel = FuelRow {

                        price: (stations as FuelClass).price;
                        name: (stations as FuelClass).name;
                        address1: (stations as FuelClass).address1;
                        address2: (stations as FuelClass).address2;


                    }
            //java.lang.System.out.println("###############{myAlert.desc}##############");
            //Insert the new signs into the table
            insert myFuel into fuelTable.content;
        }
        //redraw the table
        fuelTable.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 fuel:List;
        //var response: String;


        if(lat != -1 and lon != -1){
            java.lang.System.out.println("FUEL BEFORE");
            fuel = webservices.WebServiceCalls.getGasPrices(lat, lon);
            java.lang.System.out.println("FUEL AFTER");
        }
        //Change data from List to type "Alert"
        for (station in fuel) {
        var myFuel: FuelClass;
        java.lang.System.out.println("MAKING STATIONS");
        //alert as tmc.ws.expose2.HazardType;
        //instantiate a new alert
        myFuel = FuelClass{
       
            price: String.valueOf((station as webservices.FuelDTO).getPrice());
            name: (station as webservices.FuelDTO).getStationName();
            address1: (station as webservices.FuelDTO).getAddress1();
            address2: (station as webservices.FuelDTO).getAddress2();
             //price: "0.00"
            //name: "Holiday"
            //address1: "Street"
            //address2: "City"


        }
        java.lang.System.out.println("AFTER MAKING STATIONS");
        
        //Insert the alert into the returned sequence
        insert myFuel into mySeq;
        }
        
        return mySeq
    }
    override var content = [
                Group {
                    var title: Text = Text {
                                font: Font {
                                    size: 24
                                }
                                fill: Color.WHITE
                                content: "Gas Prices"
                            };
                   
                    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 fuelTable
                                        }
                                    ]
                                }
                            ]
                        }

                        Group {
                            translateX: 1//Main.stage.width/2 - toggle.boundsInLocal.width/2;
                            translateY: Main.stage.height - homeButton.boundsInLocal.height;
                            content: [
                                homeButton
                            ]
                        }
                    ]
                }
            ];


    init {
        Reset();
    }


}

public class FuelClass {

    var name:String = "name";
    var address1:String = "address1";
    var address2:String = "address2";
    var price:String = "2.61";
}

public class FuelRow extends CustomNode {

    var name:String = "name";
    var address1:String = "address1";
    var address2:String = "address2";
    var price:String = "2.61";
    var fill:Color = Color.BLACK;

    public override function create(): Node {
        Group {
            
         

                var txtDate   = Text {
                        //translateY: (rectDate.height / 2 - txtDate.boundsInLocal.height/2) - 2
                        font: Font {
                            size: 13
                        }
                        fill: Color.WHITE
                        wrappingWidth: (Main.stage.width / 4) * 3
                        content: "Price: ${price}\n{name}\n{address1}\n{address2}";

                    }

                var      rectDate = Rectangle {
                        width: (Main.stage.width / 4) * 3
                        height: txtDate.boundsInLocal.height + 5
                        fill: Color.BLUE
                    }

            content: [

                    rectDate,
                   Group{
                           translateY: rectDate.boundsInLocal.minY - txtDate.boundsInLocal.minY + 2
                           content: txtDate

                           }
  
            ]
        }
    }
    }
