//------------------------------------------------------------------------------
// The contents of this file are title to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Data.Common;
using System.Globalization;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Web;
using NopSolutions.NopCommerce.Common.Audit;
using NopSolutions.NopCommerce.Common.Caching;
using NopSolutions.NopCommerce.Common.Configuration.Settings;
using NopSolutions.NopCommerce.Common.CustomerManagement;
using NopSolutions.NopCommerce.Common.Directory;
using NopSolutions.NopCommerce.Common.Localization;
using NopSolutions.NopCommerce.Common.Orders;
using NopSolutions.NopCommerce.Common.Payment;
using NopSolutions.NopCommerce.Common.Products;
using NopSolutions.NopCommerce.Common.Profile;
using NopSolutions.NopCommerce.Common.SEO;
using NopSolutions.NopCommerce.Common.Tax;
using NopSolutions.NopCommerce.Common.Utils;
using NopSolutions.NopCommerce.DataAccess.Content.Topics;

namespace NopSolutions.NopCommerce.Common.Content.Topics
{
    /// <summary>
    /// Message manager
    /// </summary>
    public class TopicManager
    {
        #region Utilities

        private static TopicCollection DBMapping(DBTopicCollection dbCollection)
        {
            if (dbCollection == null)
                return null;

            TopicCollection collection = new TopicCollection();
            foreach (DBTopic dbItem in dbCollection)
            {
                Topic item = DBMapping(dbItem);
                collection.Add(item);
            }

            return collection;
        }

        private static Topic DBMapping(DBTopic dbItem)
        {
            if (dbItem == null)
                return null;

            Topic item = new Topic();
            item.TopicID = dbItem.TopicID;
            item.Name = dbItem.Name;

            return item;
        }

        private static LocalizedTopicCollection DBMapping(DBLocalizedTopicCollection dbCollection)
        {
            if (dbCollection == null)
                return null;

            LocalizedTopicCollection collection = new LocalizedTopicCollection();
            foreach (DBLocalizedTopic dbItem in dbCollection)
            {
                LocalizedTopic item = DBMapping(dbItem);
                collection.Add(item);
            }

            return collection;
        }

        private static LocalizedTopic DBMapping(DBLocalizedTopic dbItem)
        {
            if (dbItem == null)
                return null;

            LocalizedTopic item = new LocalizedTopic();
            item.TopicLocalizedID = dbItem.TopicLocalizedID;
            item.TopicID = dbItem.TopicID;
            item.LanguageID = dbItem.LanguageID;
            item.Title = dbItem.Title;
            item.Body = dbItem.Body;
            item.CreatedOn = dbItem.CreatedOn;
            item.UpdatedOn = dbItem.UpdatedOn;

            return item;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Deletes a topic
        /// </summary>
        /// <param name="TopicID">Topic identifier</param>
        public static void DeleteTopic(int TopicID)
        {
            DBTopicProvider.Provider.DeleteTopic(TopicID);
        }

        /// <summary>
        /// Inserts a topic
        /// </summary>
        /// <param name="Name">The name</param>
        /// <returns>Topic</returns>
        public static Topic InsertTopic(string Name)
        {
            DBTopic dbItem = DBTopicProvider.Provider.InsertTopic(Name);
            Topic topic = DBMapping(dbItem);
            return topic;
        }

        /// <summary>
        /// Updates the topic
        /// </summary>
        /// <param name="TopicID">The topic identifier</param>
        /// <param name="Name">The name</param>
        /// <returns>Topic</returns>
        public static Topic UpdateTopic(int TopicID, string Name)
        {
            DBTopic dbItem = DBTopicProvider.Provider.UpdateTopic(TopicID, Name);
            Topic topic = DBMapping(dbItem);
            return topic;
        }

        /// <summary>
        /// Gets a topic by template identifier
        /// </summary>
        /// <param name="TopicID">topic identifier</param>
        /// <returns>topic</returns>
        public static Topic GetTopicByID(int TopicID)
        {
            DBTopic dbItem = DBTopicProvider.Provider.GetTopicByID(TopicID);
            Topic Topic = DBMapping(dbItem);
            return Topic;
        }

        /// <summary>
        /// Gets all topics
        /// </summary>
        /// <returns>topic collection</returns>
        public static TopicCollection GetAllTopics()
        {
            DBTopicCollection dbCollection = DBTopicProvider.Provider.GetAllTopics();
            TopicCollection collection = DBMapping(dbCollection);
            return collection;
        }

        /// <summary>
        /// Gets a localized topic by identifier
        /// </summary>
        /// <param name="LocalizedTopicID">Localized topic identifier</param>
        /// <returns>Localized topic</returns>
        public static LocalizedTopic GetLocalizedTopicByID(int LocalizedTopicID)
        {
            DBLocalizedTopic dbItem = DBTopicProvider.Provider.GetLocalizedTopicByID(LocalizedTopicID);
            LocalizedTopic localizedTopic = DBMapping(dbItem);
            return localizedTopic;
        }

        /// <summary>
        /// Gets a localized topic by parent TopicID and language identifier
        /// </summary>
        /// <param name="TopicID">The topic identifier</param>
        /// <param name="LanguageID">Language identifier</param>
        /// <returns>Localized topic</returns>
        public static LocalizedTopic GetLocalizedTopic(int TopicID, int LanguageID)
        {
            DBLocalizedTopic dbItem = DBTopicProvider.Provider.GetLocalizedTopic(TopicID, LanguageID);
            LocalizedTopic localizedTopic = DBMapping(dbItem);
            return localizedTopic;
        }
        
        /// <summary>
        /// Gets a localized topic by name and language identifier
        /// </summary>
        /// <param name="Name">topic name</param>
        /// <param name="LanguageID">Language identifier</param>
        /// <returns>Localized topic</returns>
        public static LocalizedTopic GetLocalizedTopic(string Name, int LanguageID)
        {
            DBLocalizedTopic dbItem = DBTopicProvider.Provider.GetLocalizedTopic(Name, LanguageID);
            LocalizedTopic localizedTopic = DBMapping(dbItem);
            return localizedTopic;
        }

        /// <summary>
        /// Deletes a localized topic
        /// </summary>
        /// <param name="LocalizedTopicID">topic identifier</param>
        public static void DeleteLocalizedTopic(int LocalizedTopicID)
        {
            DBTopicProvider.Provider.DeleteLocalizedTopic(LocalizedTopicID);
        }

        /// <summary>
        /// Gets all localized topics
        /// </summary>
        /// <param name="TopicName">topic name</param>
        /// <returns>Localized topic collection</returns>
        public static LocalizedTopicCollection GetAllLocalizedTopics(string TopicName)
        {
            DBLocalizedTopicCollection dbCollection = DBTopicProvider.Provider.GetAllLocalizedTopics(TopicName);
            LocalizedTopicCollection localizedTopics = DBMapping(dbCollection);
            return localizedTopics;
        }

        /// <summary>
        /// Inserts a localized topic
        /// </summary>
        /// <param name="TopicID">The topic identifier</param>
        /// <param name="LanguageID">The language identifier</param>
        /// <param name="Title">The title</param>
        /// <param name="Body">The body</param>
        /// <param name="CreatedOn">The date and time of instance creation</param>
        /// <param name="UpdatedOn">The date and time of instance update</param>
        /// <returns>Localized topic</returns>
        public static LocalizedTopic InsertLocalizedTopic(int TopicID,
            int LanguageID, string Title, string Body, 
            DateTime CreatedOn, DateTime UpdatedOn)
        {
            CreatedOn = DateTimeHelper.ConvertToUtcTime(CreatedOn);
            UpdatedOn = DateTimeHelper.ConvertToUtcTime(UpdatedOn);

            DBLocalizedTopic dbItem = DBTopicProvider.Provider.InsertLocalizedTopic(TopicID,
                LanguageID, Title, Body, CreatedOn,UpdatedOn);
            LocalizedTopic localizedTopic = DBMapping(dbItem);
            return localizedTopic;
        }

        /// <summary>
        /// Updates the localized topic
        /// </summary>
        /// <param name="TopicLocalizedID">The localized topic identifier</param>
        /// <param name="TopicID">The topic identifier</param>
        /// <param name="LanguageID">The language identifier</param>
        /// <param name="Title">The title</param>
        /// <param name="Body">The body</param>
        /// <param name="CreatedOn">The date and time of instance creation</param>
        /// <param name="UpdatedOn">The date and time of instance update</param>
        /// <returns>Localized topic</returns>
        public static LocalizedTopic UpdateLocalizedTopic(int TopicLocalizedID,
            int TopicID, int LanguageID,
            string Title, string Body, 
            DateTime CreatedOn, DateTime UpdatedOn)
        {
            CreatedOn = DateTimeHelper.ConvertToUtcTime(CreatedOn);
            UpdatedOn = DateTimeHelper.ConvertToUtcTime(UpdatedOn);

            DBLocalizedTopic dbItem = DBTopicProvider.Provider.UpdateLocalizedTopic(TopicLocalizedID,
                TopicID, LanguageID, Title, Body, CreatedOn, UpdatedOn);
            LocalizedTopic localizedTopic = DBMapping(dbItem);
            return localizedTopic;
        }

        #endregion
    }
}