using FastReport.Table;
using System.Collections.Generic;

namespace FastReport.Engine
{
    partial class ReportEngine
    {
        private void InitializePages()
        {
            for (int i = 0; i < Report.Pages.Count; i++)
            {
                ReportPage page = Report.Pages[i] as ReportPage;
                if (page != null)
                {
                    PreparedPages.AddSourcePage(page);
                }
            }
        }

        /// <summary>
        ///  Translate RichObject to series of ReportComponentBase objects
        /// </summary>
        /// <param name="rich"></param>
        internal float Translate_Rich(RichObject rich)
        {
            BandBase parentBand = rich.Parent as BandBase;
            float height = 0;
            bool isRich = rich.Text.StartsWith(@"{\rtf");

            if (rich.localVisibleStorage && rich.ConvertRichText)
            {
                height = rich.Convert2ReportObjects();
            }
            return height;
        }

        private void TranslateTableObject(TableBase table)
        {
            foreach(ComponentBase obj in table.AllObjects)
            {
                if(obj is RichObject)
                {
                    RichObject rich = obj as RichObject;
                    if (rich.Visible && rich.ConvertRichText)
                    {
                        float height = rich.Convert2ReportObjects();
                        TableCell cell = rich.Parent as TableCell;

                        foreach(ComponentBase trnslated_ojbect in rich.TransltedObjects)
                        {
                            trnslated_ojbect.Parent = cell;
                        }
                        rich.Visible = false;
                    }
                }
            }
        }

        /// <summary>
        /// This code splits RichObject to report objects 
        /// </summary>
        /// <param name="parentBand"></param>
        public void TranslateObjects(BandBase parentBand)
        {
            int originalObjectsCount = parentBand.Objects.Count;
            float shift = 0;

            for (int i = 0; i < originalObjectsCount; i++)
            {
                ComponentBase obj = parentBand.Objects[i];
                obj.Top += shift;
                if (obj is RichObject)
                {
                    RichObject rich = obj as RichObject;
                    if(rich.ConvertRichText)
                    {
                        Translate_Rich(rich);
                        rich.SecondStageTranslation();
                    }
                }
                if(obj is TableObject)
                {
                    TranslateTableObject(obj as TableBase);
                }

            }
        }

        internal void RestoreRich(RichObject rich)
        {
            BandBase parentBand = rich.Parent as BandBase;
            foreach(ComponentBase obj in rich.TransltedObjects)
            {
                if(parentBand != null)
                {
                    parentBand.RemoveChild(obj);
                    parentBand.Objects.Remove(obj);
                }
                else if(rich.Parent is TableCell)
                {
                    TableCell cell = rich.Parent as TableCell;
                    cell.RemoveChild(obj);
                    cell.Objects.Remove(obj);
                }
                obj.SetParent(null);
            }
            rich.TransltedObjects.Clear();
        }
    }
}
