//------------------------------------------------------------------------------
// The contents of this file are subject 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.Globalization;
using System.Linq;
using System.Text;
using System.Xml;
using NopSolutions.NopCommerce.Common;
using NopSolutions.NopCommerce.Common.Configuration.Settings;
using NopSolutions.NopCommerce.Common.Directory;
using NopSolutions.NopCommerce.Common.Media;
using NopSolutions.NopCommerce.Common.Products;
using NopSolutions.NopCommerce.Common.SEO;
using NopSolutions.NopCommerce.Common.Utils;

namespace NopSolutions.NopCommerce.Froogle
{
    /// <summary>
    /// Froogle service
    /// </summary>
    public static class FroogleService
    {
        /// <summary>
        /// Generate froogle feed
        /// </summary>
        /// <returns></returns>
        public static string GenerateFeed()
        {
            string googleBaseNamespace = "http://base.google.com/ns/1.0";
            XmlDocument document = new XmlDocument();
            document.LoadXml(string.Format("<?xml version=\"1.0\"?><rss version=\"2.0\" xmlns:g=\"{0}\"><channel/></rss>", googleBaseNamespace));
            document.SelectSingleNode(".//rss/channel").AppendChild(document.CreateNode(XmlNodeType.Element, "title", null)).InnerText = string.Format("{0} Google Base", SettingManager.StoreName);
            document.SelectSingleNode(".//rss/channel").AppendChild(document.CreateNode(XmlNodeType.Element, "link", null)).InnerText = "http://base.google.com/base/";
            document.SelectSingleNode(".//rss/channel").AppendChild(document.CreateNode(XmlNodeType.Element, "description", null)).InnerText = "Information about products";

            XmlNode node = document.SelectSingleNode(".//rss/channel");
            ProductCollection products = ProductManager.GetAllProducts(false);
            foreach (Product product in products)
            {
                ProductVariantCollection productVariants = ProductManager.GetProductVariantsByProductID(product.ProductID, false);

                foreach (ProductVariant productVariant in productVariants)
                {
                    XmlNode newChild = document.CreateNode(XmlNodeType.Element, "item", null);
                    newChild.AppendChild(document.CreateNode(XmlNodeType.Element, "link", null)).InnerText = SEOHelper.GetProductURL(product);
                    newChild.AppendChild(document.CreateNode(XmlNodeType.Element, "title", null)).InnerText = productVariant.FullProductName;
                    XmlNode productDescriptionNode = document.CreateNode(XmlNodeType.Element, "description", null);
                    string description = productVariant.Description;
                    if (String.IsNullOrEmpty(description))
                        description = product.FullDescription;
                    if (String.IsNullOrEmpty(description))
                        description = product.ShortDescription;
                    if (String.IsNullOrEmpty(description))
                        description = product.Name;

                    productDescriptionNode.AppendChild(document.CreateCDataSection(description));
                    newChild.AppendChild(productDescriptionNode);
                    newChild.AppendChild(document.CreateNode(XmlNodeType.Element, "g", "brand", googleBaseNamespace)).InnerText = string.Empty;
                    newChild.AppendChild(document.CreateNode(XmlNodeType.Element, "g", "condition", googleBaseNamespace)).InnerText = "new";
                    newChild.AppendChild(document.CreateNode(XmlNodeType.Element, "g", "expiration_date", googleBaseNamespace)).InnerText = DateTime.MaxValue.ToString("yyyy-MM-dd");
                    newChild.AppendChild(document.CreateNode(XmlNodeType.Element, "g", "id", googleBaseNamespace)).InnerText = productVariant.ProductVariantID.ToString();
                    string imageUrl = string.Empty;
                    ProductPictureCollection productPictures = product.ProductPictures;
                    if (productPictures.Count > 0)
                        imageUrl = PictureManager.GetPictureUrl(productPictures[0].Picture, SettingManager.GetSettingValueInteger("Media.Product.ThumbnailImageSize"), true);
                    newChild.AppendChild(document.CreateNode(XmlNodeType.Element, "g", "image_link", googleBaseNamespace)).InnerText = imageUrl;
                    decimal price = productVariant.Price;
                    newChild.AppendChild(document.CreateNode(XmlNodeType.Element, "g", "price", googleBaseNamespace)).InnerText = price.ToString(new CultureInfo("en-US", false).NumberFormat);
                    newChild.AppendChild(document.CreateNode(XmlNodeType.Element, "g", "product_type", googleBaseNamespace)).InnerText = string.Empty;
                    node.AppendChild(newChild);
                }
            }
            return document.InnerXml;
        }
    }
}
