/* 
 * 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 rssviewer.view;

import javafx.scene.input.MouseEvent;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.CustomNode;
import javafx.scene.text.Text;
import javafx.scene.text.TextOrigin;
import javafx.scene.text.Font;
import javafx.scene.image.ImageView;
import javafx.scene.image.Image;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;

import rssviewer.model.Channel;
import rssviewer.model.Item;
import rssviewer.Main;

/**
 * View to display channel
 */

public def height = 320;
public def width = 240;

public class RSSViewer extends CustomNode { 
        
    var bgRect = Rectangle {
        x: 2
        y: 2
        width: width - 2
        height: height - 2
        arcWidth: 20
        arcHeight: 20
        stroke: LinearGradient {
            startX: 0.0, startY: 0.0, endX: 0.0, endY: height
            proportional: false
            stops: [ 
                Stop { offset: 0.12 color: Color.BLACK },
                Stop { offset: 0.2 color: Color.GRAY }
            ]
        }
        fill: LinearGradient {
            startX: 0.0, startY: 0.0, endX: 0.0, endY: height
            proportional: false
            stops: [ 
                Stop { offset: 0.12 color: Color.WHITE },
                Stop { offset: 0.2 color: Color.BLACK }
            ]
        }
        strokeWidth: 3.0
    }
    
    var title = Text {
        x: 30
        y: 20
        font: Font { name: "Bitstream Vera Sans Bold" size: 12 }
        fill: Color.BLACK
        content: "    Loading RSS Feed..."
        wrappingWidth: width - 60
        textOrigin: TextOrigin.TOP
    };
    
    var logo = ImageView {
        x: 10
        y: 12
        fitHeight: 22
        preserveRatio: true
        smooth: true
        image: Image { url: "{__DIR__}images/rss.png" }
    }

    var listItem : ListItem[];
    var pageIndex = 0;
    public var channel : Channel on replace {
        if(channel != null) {
            if(channel.imageURL != null) {
                title.content = "";
                logo.image = Image { url: "{channel.imageURL}" }
            } else {
                title.content = channel.title;
            }
            updateListItems();
        }
    }
    
    var backButton : ImageButton = ImageButton {
        translateY: height - 38
        translateX: 76
        normalImage: Image { url: "{__DIR__}images/back_n.png" }
        disabledImage: Image { url: "{__DIR__}images/back_d.png" }
        enabled: bind isBackButtonEnabled(pageIndex, channel.item)
        onMousePressed: function(e) {
            if(not backButton.enabled) { return; }
            pageIndex--;
            updateListItems();    
        }
    }
    
    function isBackButtonEnabled(index : Integer, item: Item[]) : Boolean {
        return (index > 0);
    }
    
    var nextButton : ImageButton = ImageButton {
        translateY: height - 38
        translateX: 130
        normalImage: Image { url: "{__DIR__}images/next_n.png" }
        disabledImage: Image { url: "{__DIR__}images/next_d.png" }
        enabled: bind isNextButtonEnabled(pageIndex, channel.item)
        onMousePressed: function(e) {
            if(not nextButton.enabled) { return; }
            pageIndex++;
            updateListItems();
        }
    }
        
    function isNextButtonEnabled(index : Integer, item: Item[]) : Boolean {
        return ((sizeof item) > ((index + 1) * 3));
    }
    
    var closeButton = ImageButton {
        translateY: 0
        translateX: width - 35
        normalImage: Image { url: "{__DIR__}images/close.png" }
        onMousePressed: function(e) {
            FX.exit();
        }
        visible: (not Main.inBrowser)
    }
    
    var topHRLine = Line {
        startX: 5 startY: 45
        endX: width - 5 endY: 45
        strokeWidth: 2.0
        stroke: LinearGradient {
            startX: 0.0, startY: 0.0, endX: width, endY: 0.0
            proportional: false
            stops: [ 
                Stop { offset: 0.0 color: Color.WHITE },
                Stop { offset: 0.5 color: Color.GRAY },
                Stop { offset: 1.0 color: Color.WHITE }
            ]
        };
    }
    
    var bottomHRLine = Line {
        startX: 5 startY: height - 38
        endX: width - 5 endY: height - 38
        strokeWidth: 2.0
        stroke: LinearGradient {
            startX: 0.0, startY: 0.0, endX: width, endY: 0.0
            proportional: false
            stops: [ 
                Stop { offset: 0.0 color: Color.BLACK },
                Stop { offset: 0.5 color: Color.GRAY },
                Stop { offset: 1.0 color: Color.BLACK }
            ]
        };
    }
    
    override function create() : Node {

        for(i in [0..2]) {
            insert ListItem { 
                translateY: 61 + ((sizeof listItem) * ListItem.height)
            } into listItem;
        }
        
        var itemGroup = Group {
            content: listItem    
        };
        
        Group {
            content: [ 
                bgRect, title, logo, backButton, nextButton, 
                closeButton, itemGroup
            ]
        };
    }

    function updateListItems() {
        for(i in [0..2]) {
            var channelIndex = (pageIndex * 3) + i;
            listItem[i].item = channel.item[channelIndex];
        }
    }
}