//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System.Collections;
using CommunityServer.Components;

namespace CommunityServer.RollerBlogs.Components
{
    public class RollerBlogFeeds
    {
        /// <summary>
        /// Private. Can not be instantiated
        /// </summary>
        private RollerBlogFeeds() { }
        
        /// <summary>
        /// Returns a FeedLinkSet consisting of all enabled feeds
        /// </summary>
        /// <returns></returns>
        public static FeedLinkSet GetEnabledFeedLinkSet()
        {
            ArrayList feeds = GetAllEnabledFeeds();
            return GetFeedLinkSet(feeds);
        }
        
        /// <summary>
        /// Returns a FeedLinkSet for all enabled feeds for the given SectionId
        /// </summary>
        /// <param name="blogSectionId"></param>
        /// <returns></returns>
        public static FeedLinkSet GetEnabledFeedLinkSet(int blogSectionId)
        {
            ArrayList feeds = GetEnabledFeedsBySectionID(blogSectionId);
            return GetFeedLinkSet(feeds);
        }

        /// <summary>
        /// Returns a FeedLinkSet from a RollerBlogFeed Arraylist
        /// </summary>
        /// <param name="feeds"></param>
        /// <returns></returns>
        private static FeedLinkSet GetFeedLinkSet(ArrayList feeds)
        {
            FeedLinkSet feedLinkSet = new FeedLinkSet();

            foreach(RollerBlogFeed feed in feeds)
            {
                FeedLinkItem i = GetFeedLinkItem(feed);
                if(!feedLinkSet.ContainsFeed(i))
                    feedLinkSet.Add(i);
            }
            
            return feedLinkSet;
        }
        
        /// <summary>
        /// Returns a FeedLinkItem from a RollerBlogFeed
        /// </summary>
        /// <param name="feed"></param>
        /// <returns></returns>
        private static FeedLinkItem GetFeedLinkItem(RollerBlogFeed feed)
        {
            return new FeedLinkItem(feed.Title, feed.Url, feed.SiteUrl);
        }

        /// <summary>
        /// gets all the feeds from the database for the current settingsid
        /// </summary>
        /// <returns></returns>
        public static ArrayList GetAllFeeds()
        {
            return RollerBlogsDataProvider.Instance().GetFeeds(false, false, 0);
        }

        /// <summary>
        /// gets all the enabled feeds from the database for the current settingsid
        /// </summary>
        /// <returns></returns>
        public static ArrayList GetAllEnabledFeeds()
        {
            return RollerBlogsDataProvider.Instance().GetFeeds(true, false, 0);
        }

        /// <summary>
        /// gets all the enabled feeds from the database for the current settingsid
        /// where the feed has not been updated within the feed settings duration
        /// </summary>
        /// <returns></returns>
        public static ArrayList GetAllOutOfDateEnabledFeeds()
        {
            return RollerBlogsDataProvider.Instance().GetFeeds(true, true, 0);
        }

        /// <summary>
        /// Gets all the enabled feeds for the specified sectionid
        /// </summary>
        /// <param name="SectionID"></param>
        /// <returns></returns>
        public static ArrayList GetEnabledFeedsBySectionID(int SectionID)
        {
            return RollerBlogsDataProvider.Instance().GetFeeds(true, false, SectionID);
        }
        
        /// <summary>
        /// Saves the feed state, and updates the LastUpdateDate (does not save other feed data)
        /// </summary>
        /// <param name="feed"></param>
        public static void UpdateFeed(RollerBlogFeed feed)
        {
            RollerBlogsDataProvider.Instance().UpdateRollerBlogFeed(feed);
        }

        /// <summary>
        /// Disables the specified feed (SectionID specific)
        /// </summary>
        /// <param name="feed"></param>
        public static void DeleteFeed(RollerBlogFeed feed)
        {
            RollerBlogsDataProvider.Instance().DeleteRollerBlogFeed(feed);
        }

        public static int AddFeed(RollerBlogFeed feed)
        {
            return RollerBlogsDataProvider.Instance().AddRollerBlogFeed(feed);
        }

    }
}