/*
 * 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.HBox;
import javafx.scene.layout.VBox;
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 java.util.GregorianCalendar;
import java.util.Calendar;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import client.TMCWebServiceClient;
//import tmc.client.WeatherForecastResponseType;
import edu.wmich.www.cs.mdot.tmc.msg.WeatherForecastResponseType;
import edu.wmich.www.cs.mdot.tmc.msg.WeatherForecastResponseTypeWeather;
//import tmc.client.WeatherConditionType;
import edu.wmich.www.cs.mdot.tmc.msg.WeatherConditionType;

/**
 * @author Ali
 */

public class LocalWeather extends CustomNode {

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

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

        //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//mdot_gui.Constants.WINDOW_HEIGHT/6;
                    font : Font {size: 40}
                    fill: Color.WHITE
                    content: "Local Weather Application"
                }
            ]
        };

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

        //Group for clipping the display area
        var tableDisplay = Group {
            //translateX: mdot_gui.Constants.WINDOW_WIDTH/2 - 250//mdot_gui.Constants.WINDOW_WIDTH/6;
            translateX: mdot_gui.Constants.WINDOW_WIDTH/2 - 252;
            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 = WeatherRow{
                                date: "Date";
                                minTemp: "Min. Temp";
                                maxTemp: "Max. Temp";
                                summary:"Summary";
                                fill: Color.BLACK
                                }

                        translateY: 100
                        translateX: mdot_gui.Constants.WINDOW_WIDTH/2 - 252
                        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 weatherTable.content;

        for (weather in result)
        {
            var myWeather = WeatherRow{
                minTemp: (weather as WeatherData).minTemp;
                maxTemp: (weather as WeatherData).maxTemp;
                summary: (weather as WeatherData).summary;
                date: (weather as WeatherData).date;
                fill:Color.BLUE
            }

            //Insert the new signs into the table
            insert myWeather into weatherTable.content;
        }

        java.lang.System.out.println("......................{weatherTable.content}");

        //redraw the table
        weatherTable.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 weatherList;
    if(lat != -1 and lon != -1){

         //var date:XMLGregorianCalendar;

         var date:Calendar = Calendar.getInstance();

         //var cal:GregorianCalendar = new GregorianCalendar();
         //cal.setTime(cal.getTime());
         //date = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);

         java.lang.System.out.println("THE DATE-----------------{date}");
         try{
        weatherList = MainScreen.client.getWeatherForecast(lat, lon, date);}
        catch(Exception){
                            java.lang.System.out.println("ERROR - Can't retreive weather from TMC");
                            }
    }

    //var weather: WeatherForecastResponseType.Weather;

    //Change data from List to type "WeatherData"
    for (weather in weatherList) {
        var myWeather: WeatherData;

        var tmpSummary = (weather as WeatherForecastResponseTypeWeather).getWeatherCondition();
        var summaryStr = (tmpSummary as WeatherConditionType).getWeatherSummary();
        var tmpDate:Calendar = (weather as WeatherForecastResponseTypeWeather).getStartTime();
        var dateFormat:SimpleDateFormat = new SimpleDateFormat("dd-MMM-yy");
        var finalDate:String = dateFormat.format(tmpDate.getTime());

        var min:Integer = (weather as WeatherForecastResponseTypeWeather).getMinTemperature();
        var max:Integer = (weather as WeatherForecastResponseTypeWeather).getMaxTemperature();

        //instantiate a new alert
        myWeather = WeatherData{
            minTemp: "{String.valueOf(min)}°";
            maxTemp: "{String.valueOf(max)}°";
            date: finalDate;
            summary: summaryStr;
        };

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

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


public class WeatherData
{
    public var minTemp: String;
    public var maxTemp: String;
    public var summary: String;
    public var date: String;
}

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


    var minTemp:String = "MinTemp";
    var maxTemp:String = "MaxTemp";
    var summary:String = "Summary";
    var date:String = "Date";
    var fill:Color = Color.BLACK;

    public override function create():Node{
       
        Group{

                var dateRect: Rectangle = Rectangle{
                    width:100
                    height:50
                    fill:fill
                }
                var minTempRect: Rectangle = Rectangle{
                    width:90
                    height:50
                    fill:fill
                }
                var maxTempRect: Rectangle = Rectangle{
                    width:90
                    height:50
                    fill:fill
                }

                 var summaryRect: Rectangle = Rectangle{
                    width:225
                    height:50
                    fill:fill
                }

                var minTempText =  Text {
                    font :  Font {
                                size:16
                            }
                    fill:Color.WHITE
                    content: minTemp
                }
                 var maxTempText =  Text {
                    font :  Font {
                                size:16
                            }
                    fill:Color.WHITE
                    content: maxTemp
                }
                 var summaryText =  Text {
                    font :  Font {
                                size:16
                            }
                    fill:Color.WHITE
                    content: summary
                    wrappingWidth:summaryRect.width;
                }

                 var dateText =  Text {
                    font :  Font {
                                size:16
                            }
                    fill:Color.WHITE
                    content: date
                }

  
                 var myHBox =  HBox{
                                spacing:1
                                content:[dateRect, minTempRect, maxTempRect, summaryRect]
                            }


            content:[

               myHBox,


               Group{
                     translateX: 5;
                        translateY: 20
                        content:[dateText]

                   }
               Group{
                       translateX: dateRect.layoutBounds.width + 5;
                        translateY: 20
                        content:[minTempText]
               }
                Group{
                       translateX: dateRect.layoutBounds.width + minTempRect.layoutBounds.width + 5;
                       translateY: 20
                        content:[maxTempText]
               }

                Group{
                       translateX: dateRect.layoutBounds.width + minTempRect.layoutBounds.width + maxTempRect.layoutBounds.width + 5;
                        translateY: 20
                        content:[summaryText]
               }
            ]
        }
    }
}