/* 
 * 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 colorchooser;

import javafx.data.feed.rss.Channel;
import javafx.data.feed.rss.Item;
import javafx.data.feed.rss.RssTask;

/**
 * @author Rakesh Menon
 */

/**
 * <item>
 *     <title><![CDATA[090423_1]]></title>
 *     <link><![CDATA[http://www.colourlovers.com/palette/794462/090423_1]]></link>
 *     <description><![CDATA[<img src="http://www.colourlovers.com/paletteRSS/6B4040/B8967B/D6B989/D1B2C2/66354E/090423_1.png" style="width: 168px; height: 67px; border: 1px solid #d0d0d0; padding: 2px;" alt="6B4040/B8967B/D6B989/D1B2C2/66354E" />]]></description>
 *     <comments><![CDATA[http://www.colourlovers.com/palette/794462/090423_1#comments]]></comments>
 *     <guid isPermaLink="false">f8f606201dec3d762db8061ae75376c1</guid>
 * </item>
 */

public class FeedUtil {
    
    public var url : String;
    public var updatePalette: function(ttl : String[], clr : String[]) = null;

    var titles : String[];
    var colors : String[];
    var paletteFeedTask : RssTask;

    public function start() : Void {
        
        if(paletteFeedTask.started) { return; }

        paletteFeedTask = RssTask {

            location: url
            interval: 60s

            onChannel: function (channel : Channel):Void {
                delete titles;
                delete colors;
            }
            
            onItem: function(item : Item): Void {
                var title = getTitle("{item.title}");
                var color = getColor("{item.description}");
                insert title into titles;
                insert color into colors;
            }

            onDone: function(): Void {
                if(updatePalette != null) {
                    updatePalette(titles, colors);
                }                
            }            
        }
        
        paletteFeedTask.start();
    }
}

function getTitle(cdata : String) : String {
    var startIndex = cdata.indexOf("<![CDATA[") + 9;
    cdata.substring(startIndex, cdata.lastIndexOf("]]>"));
}

function getColor(cdata : String) : String {
    var startIndex = cdata.indexOf("alt=\"") + 5;
    var endIndex = cdata.indexOf("\"", startIndex);
    cdata.substring(startIndex, endIndex);
}