using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Telerik.Sitefinity.Ecommerce.Catalog.Model;
using Telerik.Sitefinity.Modules.Ecommerce.Catalog;
using Telerik.Sitefinity.Workflow;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.SitefinityEssentials.Modules.Ecommerce.Products
{
    public partial class ProductsCodeSnippets
    {
        public static void ModifyProduct(Guid productId, string newTitle, string newDescription)
        {
            CatalogManager catalogManager = CatalogManager.GetManager();

            Product product = catalogManager.GetProduct(productId);
            if (product != null)
            {
                product.Description = newDescription;
                product.Title = newTitle;
                product.UrlName = Regex.Replace(newTitle.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");

                catalogManager.RecompileItemUrls<Product>(product);
                catalogManager.SaveChanges();

                var contextBag = new Dictionary<string, string>();
                contextBag.Add("ContentType", product.GetType().FullName);

                string workflowOperation = "Publish";

                WorkflowManager.MessageWorkflow(product.Id,
                    product.GetType(),
                    "OpenAccessDataProvider",
                    workflowOperation,
                    false,
                    contextBag);
            }
        }
    }
}