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

namespace FastReport.Web
{
    public static partial class Extensions
    {
        public static void FindClickedObject(
            this Report Report,
            ReportComponentBase obj,
            int pageN,
            Action<ReportComponentBase, ReportPage, int> action)
        {
            if (Report.PreparedPages == null)
                return;

            ReportPage page = Report.PreparedPages.GetPage(pageN);
            if (page != null)
            {
                action(obj, page, pageN);
                page.Dispose();
            }
        }

        public static void FindEditableObject<T>(
        this Report Report,
        ReportComponentBase obj,
        int pageN,
        Action<T, ReportPage, int> action
    )
    where T : ReportComponentBase
        {
            if (Report.PreparedPages == null)
                return;

            bool found = false;
            string objectName = obj.Name;
            while (pageN < Report.PreparedPages.Count && !found)
            {
                ReportPage page = Report.PreparedPages.GetPage(pageN);
                if (page != null)
                {
                    ObjectCollection allObjects = page.AllObjects;
                    foreach (Base report_obj in allObjects)
                    {
                        if (report_obj is ReportComponentBase c)
                        {
                            if (c is TableBase table)
                            {
                                for (int i = 0; i < table.RowCount; i++)
                                {
                                    for (int j = 0; j < table.ColumnCount; j++)
                                    {
                                        TableCell textcell = table[j, i];
                                        if (textcell.Name == objectName)
                                        {
                                            action(textcell as T, page, pageN);
                                            found = true;
                                            break;
                                        }
                                    }
                                    if (found)
                                        break;
                                }
                            }
                            else if (c is T reportComponent)
                            {
                                if (c.Name == objectName)
                                {
                                    action(reportComponent, page, pageN);
                                    found = true;
                                    break;
                                }
                            }
                            if (found)
                                break;
                        }
                    }
                    page.Dispose();
                    pageN++;
                }
            }
        }

    }
}
