using FastReport.Export.Html;
using FastReport.Preview;
using FastReport.Table;
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using FastReport.Web.Blazor.Export;

namespace FastReport.Web
{
    public partial class WebReport
    {
#region Internal Methods

        internal bool ProcessClick(object sender, OnClickEventArgs args)
        {
            bool needUpdate = true;
            var clickType = args.Type;
            if(args.Report != null)
            {
                args.Object.SetReport(args.Report);
            }
            switch (clickType)
            {
                case ClickType.Click:
                    Report.FindClickedObject(args.Object, args.PageNumber, Click);
                    break;

                case ClickType.CheckboxClick:
                    Report.FindEditableObject<CheckBoxObject>(args.Object, args.PageNumber, CheckboxClick);
                    break;

                case ClickType.TextEdit:
                    needUpdate = false;
                    if(args.Object is TextObject textObject)
                    {
                        string text = textObject.Text;
                        Report.FindEditableObject<TextObject>(textObject, args.PageNumber,
                            (obj, reportPage, _pageN) =>
                            {
                                obj.Text = text;

                                Refresh(_pageN, reportPage);
                            });
                    }
                    break;

                case ClickType.DetailedReport:
                    DoDetailedReport(args.Object);
                    break;

                case ClickType.DetailedPage:
                    DoDetailedPage(args.Object);
                    break;

                case ClickType.Bookmark:
                    Bookmark(args.Object);
                    break;

                case ClickType.PageNumber:
                    string value = args.Object.Hyperlink.Value;
                    if(!string.IsNullOrWhiteSpace(value))
                        GoToPageNumber(value);
                    break;
                case ClickType.AdvancedMatrixClick:
   
                        if (args.Object is AdvMatrix.MatrixButton advMatrix)
                        {
                            DoClickAdvancedMatrixByParamID(advMatrix.Name, args.PageNumber, advMatrix.Index);
                        }
                       
                    break;
            }
            return needUpdate;
        }

#endregion

#region Private Methods

        private void Bookmark(ReportComponentBase obj)
        {
            int i = PageNByBookmark(obj.Hyperlink.Value);
            if (i != -1)
                GotoPage(i);

        }

        private void DoDetailedPage(ReportComponentBase obj)
        {
            Report currentReport = CurrentTab.NeedParent ? Tabs[0].Report : Report;
            if (obj != null)
            {
                ReportPage? reportPage = currentReport.FindObject(obj.Hyperlink.DetailPageName) as ReportPage;
                if (reportPage != null)
                {
                    string paramName = obj.Hyperlink.ReportParameter;
                    string paramValue = obj.Hyperlink.Value;
                    Data.Parameter param = currentReport.Parameters.FindByName(paramName);
                    if (param != null && param.ChildObjects.Count > 0)
                    {
                        string[] paramValues = paramValue.Split(obj.Hyperlink.ValuesSeparator[0]);
                        if (paramValues.Length > 0)
                        {
                            int i = 0;
                            foreach (Data.Parameter childParam in param.ChildObjects)
                            {
                                childParam.Value = paramValues[i++];
                                if (i >= paramValues.Length)
                                    break;
                            }
                        }
                    }
                    else
                        currentReport.SetParameterValue(paramName, paramValue);
                    PreparedPages oldPreparedPages = currentReport.PreparedPages;
                    PreparedPages pages = new PreparedPages(currentReport);
                    currentReport.SetPreparedPages(pages);
                    currentReport.PreparePage(reportPage);
                    Report tabReport = new Report();
                    tabReport.SetPreparedPages(currentReport.PreparedPages);
                    Tabs.Add(new ReportTab()
                    {
                        Name = paramValue,
                        Report = tabReport,
                        Closeable = true,
                        NeedParent = true
                    });

                    currentReport.SetPreparedPages(oldPreparedPages);
                    CurrentTabIndex = Tabs.Count - 1;
                }
            }
        }

        internal void CacheHtmlPictures(BlazorExport export, int pageN)
        {
            //WebReportCache cache = new WebReportCache(this.Context);
            for (int i = 0; export.PreparedPages.Count > pageN && i < export.PreparedPages[pageN].Pictures.Count; i++)
            {
                try
                {
                    Stream picStream = export.PreparedPages[pageN].Pictures[i];
                    byte[] image = new byte[picStream.Length];
                    picStream.Position = 0;
                    int n = picStream.Read(image, 0, (int)picStream.Length);
                    string guid = export.PreparedPages[pageN].Guids[i];
                    //cache.PutObject(guid, image);
                    PictureCache[guid] = image;
                }
                catch
                {
                    //Log.AppendFormat("Error with picture: {0}\n", i.ToString());
                }
            }
        }

        #endregion
    }
}
