/*
 * 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 javafx.scene.shape.Rectangle;
import javafx.scene.control.Slider;
import com.sun.javafx.runtime.sequence.Sequence;
import client.TMCWebServiceClient;
import edu.wmich.www.cs.mdot.tmc.msg.GasPriceType;
import java.text.DecimalFormat;
import java.text.DecimalFormat;
//import tmc.client.GasPriceType;



/**
 * @author Ali
 */

public class Fuel extends CustomNode {

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

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

        //Slider
        var slider = Slider{
            height: (mdot_gui.Constants.APPLICATION_HEIGHT/3) - 25
            translateX: 655;
            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: 75
                    font : Font {size: 40}
                    fill: Color.WHITE
                    content: "Fuel Location Application"
                }
            ]
        };

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

        //Group for clipping the display area
        var tableDisplay = Group {
            translateX: mdot_gui.Constants.WINDOW_WIDTH/2 - 250
            translateY: mdot_gui.Constants.WINDOW_HEIGHT/5 + 30;
            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,

                Group{
                        var myHeader = GasStationRow{
                                name: "Name";
                                address1: "Address";
                                address2:"";
                                price: "Price";
                                fill: Color.BLACK
                                }

                        translateY: 100
                        translateX: mdot_gui.Constants.WINDOW_WIDTH/2 - 250
                        content:[
                                myHeader
                                ]

                        }

                tableDisplay,
                slider
            ]
        }
    }
    
    /* 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 fuelTable.content;


        for (stations in result)
        {
            var myStation = GasStationRow{
                name: (stations as GasStation).name;
                address1: (stations as GasStation).address1;
                address2: (stations as GasStation).address2;
                price: (stations as GasStation).price;
                fill: Color.BLUE
                
            }

            //Insert the new signs into the table
            insert myStation into fuelTable.content;
        }

        //redraw the table
        fuelTable.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 = mdot.Main.getMe().GetObjectAsDouble(structures.Constants.HASH_LATITUDE);
    var lon = mdot.Main.getMe().GetObjectAsDouble(structures.Constants.HASH_LONGITUDE);


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

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


    //Change data from List to type "Alert"
    for (stations: GasPriceType in gasStationList) {
        var myGasStation: GasStation;

        var twoDForm:DecimalFormat = new DecimalFormat("0.00");
        var formatedPrice:String = twoDForm.format(stations.getPrice());

        //instantiate a new alert
        myGasStation = GasStation{
            name: stations.getStationName();
            address1: stations.getAddressLine01();
            address2: stations.getAddressLine02();
            price: formatedPrice;
        };

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

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


public class GasStation
{

    public var name:String;
    public var address1:String;
    public var address2:String;
    public var price:String;
}

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

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

    public override function create():Node{
       
        Group{

                var priceRect: Rectangle = Rectangle{
                    width:50
                    height:50
                    fill:fill
                }
                var nameRect: Rectangle = Rectangle{
                    width:150
                    height:50
                    fill:fill
                }
                var addressRect: Rectangle = Rectangle{
                    width:225
                    height:50
                    fill:fill
                }
             
              
               var priceText =  Text {
                    font :  Font {
                                size:16
                            }
                    fill:Color.WHITE
                    content: price
                }

                var nameText =  Text {
                    font :  Font {
                                size:16
                            }
                    fill:Color.WHITE
                    wrappingWidth: nameRect.layoutBounds.width
                  content: name
                }

                 var addressText =  Text {
                    font :  Font {
                                size:16
                            }
                    fill:Color.WHITE
                    wrappingWidth:addressRect.layoutBounds.width
                    content: "{address1}\n{address2}"
                }
   
               
                
                var myHBox =  HBox{
                                spacing:1
                                content:[priceRect, nameRect, addressRect]
                            }

            content:[

               myHBox,

               Group{
                       translateX: 5;
                        translateY: 20
                        content:[priceText]
               }
                Group{
                       translateX: priceRect.layoutBounds.width + 5;
                       translateY: 20
                        content:[nameText]
               }
              
                Group{
                       translateX: priceRect.layoutBounds.width +  nameRect.layoutBounds.width + 5;
                        translateY: 20
                        content:[addressText]
               }
               
                

            ]
        }
    }
}