/*
 * 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 Wallet App
 */
public class Wallet extends CustomNode {

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

    var chargeTable: VBox =  VBox{
    spacing:2
    content: []
    };
    public override  function create()  : Node

        {
        visible = false;
        id = "Wallet";
        //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 chargeGrid: VBox = VBox {
                    spacing: 2
                    content: createGrid();
                }*/
        var mainGroup = Group {
                    content: [
                        Text {
                            translateX: Constants.WINDOW_WIDTH / 2 - 150
                            translateY: Constants.WINDOW_HEIGHT / 5
                            font: Font { size: 40 }
                            fill: Color.WHITE
                            content: "Wallet Application"
                        }
                        HBox {
                            translateX: Constants.WINDOW_WIDTH / 2 - 25
                            translateY: Constants.WINDOW_HEIGHT / 5 + 15
                            spacing: 25
                            content: [
                                txtCharge,
                                txtDesc
                            ]
                        }
                    ]
                };
        //Group that handles translating the grid
        var gridGroup = Group {
                    translateY: bind 0 - scroll.value * 30
                    content: [
                        chargeTable
                    ]
                }
        //Group for clipping the display area
        var gridDisplay = Group {
                    translateX: Constants.WINDOW_WIDTH / 5
                    translateY: Constants.WINDOW_HEIGHT / 3
                    content: [
                        gridGroup
                    ]
                    clip: Rectangle {
                        width: Constants.APPLICATION_WIDTH;
                        height: Constants.APPLICATION_HEIGHT - 175;
                    }
                }
        //Group that is displayed
        Group {
            content: [
                mainGroup,
                scroll,
                gridDisplay
            ]
        }
    }

    var txtCharge = Text {
                translateY: 20
                textAlignment: TextAlignment.CENTER;
                wrappingWidth: 65
                font: Font { size: 16 }
                fill: Color.WHITE
                content: "Charge" }
    var txtDesc = Text {
                translateY: 20
                textAlignment: TextAlignment.CENTER;
                wrappingWidth: 100
                font: Font { size: 16 }
                fill: Color.WHITE
                content: "Description" }



    function createGrid(): Void {
        var result: Sequence;
        var grid: Sequence;
        result = ConvertFromVector();
        //Clear all the old signs from the table
        delete chargeTable.content;

        for (charge in result) {

            var myCharge = DataRow {
                        date: (charge as structures.ChargesEntry).getDate()
                        price: (charge as structures.ChargesEntry).getPrice();
                        type: (charge as structures.ChargesEntry).getType();
                    }

            //Insert the new trip into the table
            insert myCharge into chargeTable.content;
        }
        //redraw the table
        chargeTable.layout();


    }

}

//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

    var size = database.Context.chargedVector.size();
    //Change data from Vector format to type "ChargesEntry"
    for (num in [1..size]) {
        //var myVect:java.util.Vector;
        var myCharge: structures.ChargesEntry;

        myCharge = database.Context.chargedVector.elementAt(num - 1);

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

    }
    //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 type: String = "";
    var price: String = "";

    public override function create(): Node {
        var rectDate: Rectangle;
        var rectPrice: Rectangle;
        var rectType: Rectangle;
        var textDate: Text;
        var textPrice: Text;
        var textType: Text;
        var grid: HBox;
        Group {
            content: [
                grid = HBox {
                    spacing: 2
                    content: [
                        rectDate = Rectangle {
                            width: 200
                            height: 40
                            fill: Color.GREEN
                        }
                        rectPrice = Rectangle {
                            width: 80
                            height: 40
                            fill: Color.GREEN
                        }
                        rectType = Rectangle {
                            width: 200
                            height: 40
                            fill: Color.GREEN
                        }
                    ]
                }
                textDate = Text {
                    translateX: rectDate.boundsInParent.minX + 5;
                    translateY: rectDate.boundsInParent.minY + 25;
                    font: Font {
                        size: 16
                    }
                    fill: Color.WHITE
                    content: date
                }
                textPrice = Text {
                    translateX: 210;
                    translateY: rectPrice.boundsInParent.minY + 25;
                    font: Font {
                        size: 16
                    }
                    fill: Color.WHITE
                    content: price
                }
                textType = Text {
                    translateX: 290
                    translateY: rectType.boundsInParent.minY + 25;
                    font: Font {
                        size: 16
                    }
                    fill: Color.WHITE
                    content: type
                }
            ]
        }
    }
}
