public void BeginWriting(CompostedContentMetdata metadata)
 {
     _current = new CompositedHtml
     {
         Metadata = metadata,
     };
     CompositedContent.Add(_current);
 }
        private void Render(PageCompositionElement pp, RenderAttributes attributes, ComposedHtmlWriter local)
        {
            if (!_page.RenderingResults.ContainsKey(pp.ContentId))
            {
                throw new Exception("There is no rendering fragment available for " + pp.FriendlyName + " with content id " + pp.ContentId);
            }

            var metadata = new CompostedContentMetdata
            {
                ContentId = pp.ContentId,
                NodeType  = FragmentType.Element
            };

            local.BeginWriting(metadata);

            var renderingResultForElement = _page.RenderingResults[pp.ContentId];

            for (var index = 0; index < renderingResultForElement.InlineRenderingFragments.Count; index++)
            {
                var part    = renderingResultForElement.InlineRenderingFragments[index];
                var isFirst = index == 0;
                var isLast  = index == renderingResultForElement.InlineRenderingFragments.Count - 1;


                if (part is HtmlOutput)
                {
                    var htmlOutput       = (HtmlOutput)part;
                    var renderAttributes = new RenderAttributes
                    {
                        Mode = attributes.Mode,
                        IsFirstContentPart = isFirst,
                        IsLastContentPart  = isLast
                    };

                    local.Write(htmlOutput.Html);


                    continue;
                }

                if (part is GlobalSubstitutionOutput)
                {
                    var global = (GlobalSubstitutionOutput)part;
                    local.BeginWriting(new CompostedContentMetdata
                    {
                        NodeType = FragmentType.GlobalSubstitution
                    });

                    foreach (var s in _globals[global.Id])
                    {
                        local.Write(string.Join("", s));
                    }

                    local.EndWriting();



                    //if (attributes.Mode == FragmentRenderMode.PageDesigner)
                    //{
                    //    if (global.Id == GlobalLayoutPlaceHolderIds.Head)
                    //    {

                    //    }

                    //    if (global.Id == GlobalLayoutPlaceHolderIds.Scripts)
                    //    {
                    //        var head = _pageDesignerHtmlFactory.LoadPageDesignerHead();
                    //        var style = _pageDesignerHtmlFactory.LoadPageDesignerStyle();

                    //        local.Append(head);
                    //        local.Append(style);

                    //        var scripts = _pageDesignerHtmlFactory.LoadPageDesignerScripts();
                    //        local.Append(scripts);
                    //    }

                    //    if (global.Id == GlobalLayoutPlaceHolderIds.InternalStateTracking)
                    //    {


                    //        var tags = _pageDesignerHtmlFactory.CreatePageDesignerStateTags();
                    //        local.Append(string.Join(string.Empty,tags));
                    //    }


                    //}

                    continue;
                }

                if (part is LayoutSubstitutionOutput)
                {
                    var subPart             = (LayoutSubstitutionOutput)part;
                    var relevantPlaceHolder = pp.PlaceHolders[subPart.Id];
                    var renderAttributes    = new RenderAttributes
                    {
                        Mode = attributes.Mode,
                        IsFirstContentPart = isFirst,
                        IsLastContentPart  = isLast
                    };


                    local.BeginWriting(new CompostedContentMetdata
                    {
                        PlaceHolderId = subPart.Id,
                        NodeType      = FragmentType.LayoutSubtitution,
                    });

                    foreach (var item in relevantPlaceHolder.Renderings)
                    {
                        Render(item, renderAttributes, local);
                    }

                    local.EndWriting();

                    continue;
                }

                throw new ArgumentException();
            }

            local.EndWriting();
        }