/*
 * 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 Weather extends Scene {

    var myRect: Rectangle;

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

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

   

        //java.lang.System.out.println(result.size());
       
        for (days in result) {
            var myWeather = WeatherRow {


                    date:  (days as WeatherClass).date;
                    minTemp: (days as WeatherClass).minTemp;
                    maxTemp: (days as WeatherClass).maxTemp;
                    summary: (days as WeatherClass).summary;
                    }
           
            //Insert the new signs into the table
            insert myWeather into weatherTable.content;
        }
        //redraw the table
        weatherTable.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 weather:List;
        //var response: String;

        
        if(lat != -1 and lon != -1){
            //THIS IS LONGITUDE, LATITUDE NOT LATITUDE, LONGITUDE (like the rest of the code)
            //hazards = WebServiceCalls.getHazardList(lat, lon);
            //weather = webservices.WebServiceCalls.getWeatherForecast(Double.parseDouble("42.251972"), Double.parseDouble("-85.641148"));
            java.lang.System.out.println("WEATHER BEFORE");
            weather = webservices.WebServiceCalls.getWeatherForecast(lat, lon);
            java.lang.System.out.println("WEATHER AFTER");
           

        }
        //Change data from List to type "Alert"
        for (day in weather) {
        var myWeather: WeatherClass;

        //alert as tmc.ws.expose2.HazardType;
        //instantiate a new alert
        var myString:String = "HI";



        myWeather = WeatherClass{
        //code: ((alert as tmc.ws.expose2.HazardType).getCategory() as tmc.ws.expose2.HazardCategoryType).getCode() as String;
        //desc: ((alert as tmc.ws.expose2.HazardType).getCategory() as tmc.ws.expose2.HazardCategoryType).getDescription() as String;
            date: (day as webservices.WeatherDTO).getStartTime();
            minTemp: String.valueOf((day as webservices.WeatherDTO).getMinTemperature());
            maxTemp: String.valueOf((day as webservices.WeatherDTO).getMaxTemperature());
            summary: (day as webservices.WeatherDTO).getWeatherSummary();



        };

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

        return mySeq
    }
    override var content = [
                Group {
                    var title: Text = Text {
                                font: Font {
                                    size: 24
                                }
                                fill: Color.WHITE
                                content: "Weather"
                            };
                   
                    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 weatherTable
                                        }
                                    ]
                                }
                            ]
                        }

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


    init {
        Reset();
    }


}

public class WeatherClass {

    var date:String = "2010-06-10";
    var minTemp:String = "70";
    var maxTemp:String = "101";
    var summary:String = "Cloudy with a chance of meatballs.";
}

public class WeatherRow extends CustomNode {

    var date:String = "2010-06-10";
    var minTemp:String = "70";
    var maxTemp:String = "101";
    var summary:String = "Cloudy with a chance of meatballs.";

    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: "Date: {date}\nLo: {minTemp}\nHi: {maxTemp}\nSummary: {summary}";

                    }

                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

                           }
            ]
        }
    }
    }
