/*
 * CarbonEmissions.fx
 *
 * Created on Nov 30, 2009, 7:52:07 PM
 */

package mdot_gui;

import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.paint.Color;
import com.sun.javafx.runtime.sequence.Sequence;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.TextAlignment;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.control.Slider;

/**
 * @author Jamie Lynn
 * Main page for the Carbon Emissions App
 */

public class CarbonEmissions extends CustomNode{

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

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

//Scrollbar
var scroll = Slider {
                    height: (Constants.APPLICATION_HEIGHT / 4) + 10
                    width: 20
                    translateX: 658
                    translateY: (Constants.APPLICATION_HEIGHT / 2)
                    blockIncrement: 60
                    clickToPosition: true
                    min: 0
                    max: 10
                    vertical: true
                    scaleX: 2.5
                    scaleY: 2.5
                };

var tripGrid: VBox = VBox{
                            spacing:2
                            content: createGrid();
                        }

var mainGroup =  Group{

            content:[

                Text {
                translateX: Constants.WINDOW_WIDTH/2 - 260
                translateY: Constants.WINDOW_HEIGHT/5
                font : Font {size: 40}
                fill: Color.WHITE
                content: "Carbon Emissions Application"
                }

                HBox{
                translateX: Constants.WINDOW_WIDTH/2 - 50
                translateY: Constants.WINDOW_HEIGHT/5 + 15
                spacing: 75
                content:[
                            txtFuel,
                            txtMiles,
                            txtFootPrint

                        ]
                }
            ]
    };

//Group that handles translating the grid
var gridGroup = Group{
    translateY: bind 0 - scroll.value * 30

    content:[
                tripGrid
            ]
}

//Group for clipping the display area
var gridDisplay = Group{
    translateX: Constants.WINDOW_WIDTH/6
    translateY: Constants.WINDOW_HEIGHT/3
    content:[
                gridGroup
            ]
    clip:Rectangle{
            width:Constants.APPLICATION_WIDTH;
            height:Constants.APPLICATION_HEIGHT;
            }
}

//Group that is displayed
Group{
        content:[
                mainGroup,
                scroll,
                gridDisplay
                ]
    }

}

var txtMiles = Text {
                    translateY:20
                    textAlignment: TextAlignment.CENTER;
                    wrappingWidth:65
                    font : Font {size: 16}
                    fill: Color.WHITE
                    content: "Miles"}

var txtFuel = Text {
                    translateY:20
                    textAlignment: TextAlignment.CENTER;
                    wrappingWidth:50
                    font : Font {size: 16}
                    fill: Color.WHITE
                    content: "Fuel"}

var txtFootPrint = Text {
                    textAlignment: TextAlignment.CENTER;
                    wrappingWidth:65
                    font : Font {size: 16}
                    fill: Color.WHITE
                    content: "Carbon Footprint"}



function createGrid():Sequence{

        var result:Sequence;
        var grid:Sequence;
        result = ConvertFromVector();

        //Clear all the old signs from the table
        //delete tripTable.content;

        for (trips in result)
        {

            var myTrip = DataRow{
                    date: (trips as applications.Trip).getDate()
                    gallons: (trips as applications.Trip).getGallonsused().toString();
                    miles: (trips as applications.Trip).getMilesTraveled().toString();
                    emissions: (trips as applications.Trip).getCarbonemission().toString();
                }

                //Insert the new trip into the table
                insert myTrip into grid;
        }


        //redraw the table
        //tripTable.layout();

        return grid;

    }

}

//This function converts a vector into a sequence.
//It is necessary because javafx works with sequences and NOT vectors.
public function ConvertFromVector()
{

    var mySeq:Sequence; //sequence of trips

    //Read the CarbonEmissions XML file and populate the trip database
    xmlpackage.CarbonEmissionsReader.ExtractFromFile();
    

    var size = applications.CarbonControl.tripsDatabase.size();

    //Change data from Vector format to type "Trip"
    for(num in [1..size])
    {
        //var myVect:java.util.Vector;
        var myTrip: applications.Trip;


        myTrip = applications.CarbonControl.tripsDatabase.elementAt(num - 1);


        //Insert the trip into the returned sequence
        insert myTrip into mySeq;

    }
java.lang.System.out.println((mySeq[3] as applications.Trip).getGallonsused().toString());
    //return all the trips in a sequence
    return mySeq;


}




    //row of data from the carbon app
public class DataRow extends CustomNode{

    var date:String = "";
    var gallons:String = "35.4";
    var miles:String = "";
    var emissions:String = "";

    public override function create():Node{
        var rectDate:Rectangle;
        var rectGallons:Rectangle;
        var rectMiles:Rectangle;
        var rectEmissions:Rectangle;
        var textDate:Text;
        var textGallons:Text;
        var textMiles:Text;
        var textEmissions:Text;
        var grid:HBox;

        Group{

            content:[
            grid = HBox{
                spacing: 2
                content:[
                    rectDate = Rectangle{
                        width:200
                        height:40
                        fill:Color.BLUE
                        }

                    rectGallons = Rectangle{
                        width:105
                        height:40
                        fill:Color.BLUE
                        }
                    rectMiles = Rectangle{
                        width:105
                        height:40
                        fill:Color.BLUE
                        }
                    rectEmissions = Rectangle{
                        width:105
                        height:40
                        fill:Color.BLUE
                        }
                ]
            }

                textDate = Text {
                    translateX: rectDate.boundsInParent.minX + 5;
                    translateY: rectDate.boundsInParent.minY + 25;
                    font :  Font {
                                size:16
                            }
                    fill:Color.WHITE
                    content: date
                }
                textGallons = Text {
                    translateX: 210;
                    translateY: rectGallons.boundsInParent.minY + 25;
                    font :  Font {
                                size:16
                            }
                    fill:Color.WHITE
                    content: gallons
                }
                textMiles = Text {
                    translateX: 315
                    translateY: rectMiles.boundsInParent.minY + 25;
                    font :  Font {
                                size:16
                            }
                    fill:Color.WHITE
                    content: miles
                }
                textEmissions = Text {
                    translateX: 420
                    translateY: rectEmissions.boundsInParent.minY + 25;
                    font :  Font {
                                size:16
                            }
                    fill:Color.WHITE
                    content: emissions
                }
            ]
        }
    }

}
