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

using System;
using System.Collections;
using System.Data;
using CommunityServer.Components;
using CommunityServer.Configuration;

namespace CommunityServer.RollerBlogs.Components
{
    public abstract class RollerBlogsDataProvider
    {

        public static readonly string RollerBlogsDataProviderName = "RollerBlogsDataProvider";

        #region Instance

        private static RollerBlogsDataProvider _defaultInstance = null;

        static RollerBlogsDataProvider()
        {
            CreateDefaultCommonProvider();
        }

        /// <summary>
        /// Returns an instance of the user-specified data provider class.
        /// </summary>
        /// <returns>An instance of the user-specified data provider class.  This class must inherit the
        /// CommonDataProvider interface.</returns>
        public static RollerBlogsDataProvider Instance()
        {
            return _defaultInstance;
        }

        public static RollerBlogsDataProvider Instance(Provider dataProvider)
        {
            RollerBlogsDataProvider fdp = CSCache.Get(dataProvider.Name) as RollerBlogsDataProvider;
            if (fdp == null)
            {
                fdp = CommunityServer.Components.DataProviders.Invoke(dataProvider) as RollerBlogsDataProvider;
                CSCache.Max(dataProvider.Name, fdp);
            }
            return fdp;
        }

        /// <summary>
        /// Creates the Default CommonDataProvider
        /// </summary>
        private static void CreateDefaultCommonProvider()
        {
            // Get the names of the providers
            //
            CSConfiguration config = CSConfiguration.GetConfig();

            // Read the configuration specific information
            // for this provider
            //
            Provider sqlForumsProvider = (Provider)config.Providers[RollerBlogsDataProviderName];

            // Read the connection string for this provider
            //

            _defaultInstance = DataProviders.CreateInstance(sqlForumsProvider) as RollerBlogsDataProvider;
        }

        #endregion

        public abstract ArrayList GetFeeds(bool enabledOnly, bool updateNeededOnly, int SectionID);
        public abstract ArrayList GetRollerBlogPosts(RollerBlogFeed rbf);
        public abstract DateTime UpdateRollerBlogFeed(RollerBlogFeed rbf);
        public abstract int AddRollerBlogFeed(RollerBlogFeed rbf);
        public abstract void DeleteRollerBlogFeed(RollerBlogFeed rbf);
        public abstract void AddRollerBlogPost(RollerBlogPost rbp);
        public abstract void UpdateRollerBlogPost(RollerBlogPost rbp);

        #region Populate

        public static RollerBlogFeed PopulateRollerBlogFeedFromIDataReader(IDataReader dr)
        {
            RollerBlogFeed rbf = new RollerBlogFeed();
            rbf.SectionId = (int)dr["SectionID"];
            rbf.UrlId = (int)dr["UrlId"];
            rbf.Url = dr["Url"] as string;
            rbf.SiteUrl = dr["SiteUrl"] as string;
            rbf.SettingsID = (int)dr["SettingsID"];
            rbf.Enabled = (bool)dr["Enabled"];
            rbf.IntervalMinutes = (int)dr["IntervalMinutes"];
            rbf.Title = dr["Title"] as string;
            rbf.SubscribeDate = (DateTime)dr["SubscribeDate"];
            rbf.LastUpdateDate = (DateTime)dr["LastUpdateDate"];
            rbf.LastModifiedDate = (DateTime)dr["LastModifiedDate"];
            rbf.PostFullText = (bool)dr["PostFullArticle"];
            rbf.ExerptSize = (int)dr["ExerptSize"];
            rbf.Etag = dr["ETag"] as string;
            rbf.IsBlogAggregated = (bool)dr["IsBlogAggregated"];
            rbf.IsBlogRollAggregated = (bool)dr["IsBlogRollAggregated"];


            return rbf;
        }

        public static RollerBlogPost PopulateRollerBlogPostFromIDataReader(IDataReader dr)
        {
            RollerBlogPost rbp = new RollerBlogPost();
            rbp.PostID = (int)dr["PostID"];
            rbp.SectionId = (int)dr["SectionID"];
            rbp.UrlId = (int)dr["UrlId"];
            rbp.CommentCount = (int)dr["CommentCount"];
            rbp.CommentsUrl = dr["CommentUrl"] as string;
            rbp.GuidName = dr["GuidName"] as string;
            rbp.GuidIsPermaLink = (bool)dr["GuidIsPermaLink"];
            rbp.PermaLink = dr["PermaLink"] as string;

            return rbp;
        }
        #endregion
    }
}
