/* 
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms. 
 * 
 * This file is available and licensed under the following license:
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright notice, 
 *     this list of conditions and the following disclaimer.
 *
 *   * Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *
 *   * Neither the name of Sun Microsystems nor the names of its contributors 
 *     may be used to endorse or promote products derived from this software 
 *     without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package podcastfeedviewer;

import javafx.scene.CustomNode;
import javafx.scene.paint.Color;
import javafx.scene.Node;
import javafx.scene.Group;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.shape.Rectangle;
import javafx.scene.layout.VBox;
import javafx.scene.input.MouseEvent;
import javafx.scene.image.ImageView;
import javafx.scene.image.Image;
import javafx.util.Sequences;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.TextBox;
import javafx.animation.Timeline;
import javafx.animation.KeyFrame;
import javafx.animation.Interpolator;
import podcastfeedviewer.PodcastParser;
import podcastfeedviewer.PersistenceHandler;

/**
 * @author Raghu Nair
 */
public class LocationControll extends CustomNode {

    //width of the location toolbar.
    public var width = 640.0;

    //Hight of the location toolbar
    public var height = 50.0;

    //Feed Location URL.
    public-read var location:String;

    //method to set text - remember to commit after set text
    public function setText(text:String){
        textBox.text = text;
        textBox.commit();
    }

    //text box for entering feed location url.
    var textBox:TextBox  = TextBox {
        translateX: bind (Main.iconSize + Main.gap)
        translateY: bind (5 * Main.widthRatio1)
        width : bind Main.textBoxSize
        height: bind (Main.iconSize - 5*Main.heightRatio1)
        font: Font {
            size: (24 * Main.widthRatio1) as Integer
        }
        editable:false
        text: ""
        action: function(){
                textBox.editable = false;
                textBox.commit();
                location = textBox.text;
                Main.location = location.trim();
                Main.log("Start clicked - Location changed {location}");
        }
    }

    //button for navigating through the persisted feed urls.
    var leftArrow:ImageView =
        ImageView{
            fitHeight:bind Main.iconSize
            fitWidth:bind Main.iconSize
            image:Image{
                url:"{__DIR__}resources/arrow_left.png"
            }
            
            onMousePressed: function(e:MouseEvent){

                if ( sizeof Main.locations == 0){
                    textBox.text = "";
                    location = "";
                    Main.location = "";
                    return;
                }
                var index = Sequences.indexOf(Main.locations,location);
                if ( index == 0){
                    index = sizeof Main.locations - 1;
                }else index --;
                Main.location = Main.locations[index];
                setText(Main.locations[index]);
                location =Main.location;
                Main.log("Left arrow clicked - {location}");
            }
        }

    //button for navigating through the persisted feed urls.
     var rightArrow:ImageView =
        ImageView{
            translateX: bind (Main.iconSize + Main.gap + Main.textBoxSize + Main.gap);
            fitHeight:bind Main.iconSize
            fitWidth:bind Main.iconSize
            image:Image{
                url:"{__DIR__}resources/arrow_right.png"
            }
            
            onMousePressed: function(e:MouseEvent){
                if ( sizeof Main.locations == 0){
                    setText("");
                    location = "";
                    return;
                }

                var index = Sequences.indexOf(Main.locations,location);
                if ( index == (sizeof Main.locations - 1) or index < 0 ){
                    index = 0;
                }else {index ++;}
                Main.location = Main.locations[index];
                setText(Main.locations[index]);
                location =Main.location;
                Main.log("Righ arrow clicked - {location}");
            }
        }

    //button for adding the feed url - action: URL textbox will be editable
    var plus:ImageView =
        ImageView{
            translateX:bind (Main.iconSize + Main.gap + Main.textBoxSize + Main.gap + Main.iconSize + Main.gap + Main.iconSize + Main.gap);
            fitHeight:bind Main.iconSize
            fitWidth:bind Main.iconSize

            image:Image{
                url:"{__DIR__}resources/feed_plus.PNG"
            }
            
            onMousePressed: function(e:MouseEvent){
                textBox.editable = true;
                setText("");
                Main.log("Add clicked");
            }
    }


    //Start button will add and persist the feed url if it is a valid one.
    var start:ImageView =
        ImageView{
            translateX: bind  (Main.iconSize + Main.gap + Main.textBoxSize +  Main.gap + Main.iconSize +Main.gap);
            fitHeight:bind Main.iconSize
            fitWidth:bind Main.iconSize

            image:Image{
                url:"{__DIR__}resources/start.PNG"
            }
            

            onMousePressed: function(e:MouseEvent){
                textBox.editable = false;
                textBox.commit();
                location = textBox.text;
                Main.location = location.trim();
                Main.log("Start clicked - Location changed {location}");
            }
    }

    //remove buton - will remove the feed url from memory and persisted data .
    var remove:ImageView =
        ImageView{
            translateX: bind (4*Main.iconSize + 5*Main.gap + Main.textBoxSize );
            fitHeight:bind Main.iconSize
            fitWidth:bind Main.iconSize

            image:Image{
                url:"{__DIR__}resources/remove.png"
            }

            onMousePressed: function(e:MouseEvent){
                textBox.editable = false;
                PodcastParser.removePodcast(location);
                //Need to store the locations again.
                PersistenceHandler.store(Main.locations);
                if ( sizeof Main.locations > 0 ){
                    setText(Main.locations[0]);
                    location = Main.locations[0];
                    Main.location = Main.locations[0];
                }else {
                    setText("");
                    location = "";
                    Main.location = "";
                }
            }
    }


    public override function create(): Node {

        return Group {            
            content: [
                        leftArrow,
                        TooltipNode {
                            sourceNode:leftArrow
                            tooltip:"        Previous Feed"
                            width:120
                            
                        },
                        textBox,
                        TooltipNode {
                            sourceNode:textBox
                            tooltip:" Clik on Add to change feed"
                            width:140
                        },
                        rightArrow,
                        TooltipNode {
                            sourceNode:rightArrow
                            tooltip:"Next Feed"
                            width:50
                        },
                        plus,
                        TooltipNode {
                            sourceNode: plus
                            tooltip:"Add a new podcast feed"
                            width :120
                        },
                        start,
                        TooltipNode {
                            sourceNode:start
                            tooltip:"Start polling"
                            width:60
                        },
                        remove,
                        TooltipNode {
                            sourceNode:remove
                            tooltip:"Remove the Feed from list"
                            width:160
                        }
                    ]
        };
    }
}


function run(){
    //insert "http://www.azcentral.com/vodcasts/vod.xml" into Main.locations;
    //insert "http://audiorpc.weblogs.com/rss100.xml" into Main.locations;
    Stage {
        title: "Location Tool bar Test"
        scene: Scene {
            width: 500
            height: 200
            content: [ LocationControll{} ]
        }
    }
}