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

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.Events
{
    public partial class EventsSnippets
    {
        public static void CreateEventNativeAPI(Guid eventId, string title, string content, DateTime startDate, DateTime endDate, string city, string country)
        {
            EventsManager eventsManager = new EventsManager();

            // Create the event
            Event eventItem = eventsManager.CreateEvent(eventId);

            // Set the event properties
            eventItem.Title = title;
            eventItem.Content = content;
            eventItem.UrlName = Regex.Replace(title.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
            eventItem.City = city;
            eventItem.Country = country;
            eventItem.EventStart = startDate;
            eventItem.EventEnd = endDate;

            eventItem.PublicationDate = DateTime.Today;

            //Recompiles and validates the url of the event item.
            eventsManager.RecompileAndValidateUrls(eventItem);

            // Save the changes
            eventsManager.SaveChanges();

            // Publish
            var bag = new Dictionary<string, string>();
            bag.Add("ContentType", typeof(Event).FullName);
            WorkflowManager.MessageWorkflow(eventId, typeof(Event), null, "Publish", false, bag);
        }
    }
}