﻿using System;
using System.Globalization;
using Composite.Data;
using Composite.Data.Types;


namespace Demo.C1_5_1
{
    /// <summary>
    /// A demo class that allows you to create pages programmatically
    /// </summary>
    public static class PageCreationDemo
    {
        /// <summary>
        /// Adds a new page to the system and publishes it. This sample expects that 
        /// the Layout Template used has a &lt;rendering:placeholder /&gt; with id="contentplaceholder" 
        /// </summary>
        /// <param name="parentId">ID of the parent page for structural placement. Use Guid.Empty for the top-level page.</param>
        /// <param name="templateId">ID of a template to use</param>
        /// <param name="pageTypeId">ID of a page type to use</param>
        /// <param name="title">Page title</param>
        /// <param name="pageDescription">Page description</param>
        /// <param name="pageCulture">Language (culture) of the page</param>
        /// <param name="menuTitle">String to use in menues. Use empty string to keep this page out of navigations.</param>
        /// <param name="urlTitle">A single folder name to use in URLs</param>
        /// <param name="pageContentXhtml">Content of the page. This demo only supports single content</param>
        /// <returns></returns>
        public static Guid CreateNewPage(
            Guid parentId,
            Guid pageTypeId,
            Guid templateId,
            string title,
            string pageDescription,
            CultureInfo pageCulture,
            string menuTitle,
            string urlTitle,
            string pageContentXhtml)
        {
            using (var connection = new DataConnection(PublicationScope.Unpublished, pageCulture))
            {
                ////// *** PAGE *** //////

                IPage page = DataConnection.New<IPage>();

                page.Id = Guid.NewGuid();
                page.PageTypeId = pageTypeId;
                page.TemplateId = templateId;
                page.Title = title;
                page.Description = pageDescription;
                page.MenuTitle = menuTitle;
                page.UrlTitle = urlTitle;

                // The page.Add(...) method will add the default page placeholder contents, and other data
                // based on a page type referenced in the page.PageTypeId property
                page = page.Add(parentId, PageInsertPosition.Bottom);

                ////// *** PLACEHOLDER *** //////

                /* 
              
                IPagePlaceholderContent content = DataConnection.New<IPagePlaceholderContent>();

                content.Content = pageContentXhtml;
                content.PlaceHolderId = "contentplaceholder";
                content.PageId = page.Id;

                content = connection.Add(content);*/

                ////// *** PUBLISHING *** ///////

                page.PublicationStatus = "published";
                connection.Update(page);
                
                return page.Id;
            }
        }
    }
}