using PdfSharp.Drawing;
using PdfSharp.Pdf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using zocWpfCharts;

namespace OTDR
{
    public class PDFExport
    {
        public PDFExport()
        {

        }

        public void GenToPDF(string sFileName, Charts charts)
        {
            // Vytvoří nový PDF dokument
            PdfDocument doc = new PdfDocument();
            // Vytvoří prázdnou stránku
            PdfPage page = doc.AddPage();

            XGraphics gfx = XGraphics.FromPdfPage(page);

            XFont font15b = new XFont("Verdana", 15, XFontStyle.Bold);
            XFont font12 = new XFont("Verdana", 12, XFontStyle.Regular);
            XFont font12b = new XFont("Verdana", 12, XFontStyle.Bold);
            XFont font10 = new XFont("Verdana", 10, XFontStyle.Regular);
            XFont font10b = new XFont("Verdana", 10, XFontStyle.Bold);

            int nTop = 10;
            gfx.DrawString("Report", font15b, XBrushes.Black, new XRect(0, nTop, page.Width, 30),  XStringFormats.Center);
            nTop += 30;
            foreach (Chart chart in charts.lstCharts)
            {
                gfx.DrawString(chart.fileName, font12, BrushesToXBrush(chart.color), new XRect(10, nTop, page.Width, 20), XStringFormats.CenterLeft);
                nTop += 20;
            }
            int nColWidth = (int)Math.Round((page.Width - 20) / 6);
            XRect rect = new XRect(10, nTop, page.Width - 20, page.Height / 2);
            MemoryStream strmImg = charts.GetAsPNG();
            PdfSharp.Drawing.XImage ximg = PdfSharp.Drawing.XImage.FromStream(strmImg);

            gfx.DrawImage(ximg, rect);
            strmImg.Close();

            nTop += (int)Math.Round(page.Height / 2) + 20;

            DrawTableRow(gfx, page, nTop, nColWidth, font10b, "Event/Section;Distance (Km);Value (dB);Length (Km);Loss (dB);Att (dB/Km)".Split(';'), XBrushes.LightGray, new XStringFormat[] { XStringFormats.CenterLeft, XStringFormats.CenterLeft, XStringFormats.CenterLeft, XStringFormats.CenterLeft, XStringFormats.CenterLeft, XStringFormats.CenterLeft });
            nTop += 20;
            double dLength, dLoss;
            LineWithText lwt;
            for (int i = 0; i < charts.lstCharts[0].lstEdgeLines.Count - 1; i++)
            {
                lwt = charts.lstCharts[0].lstEdgeLines[i];
                DrawTableRow(gfx, page, nTop, nColWidth, font10, string.Format("Event {0};{1};{2};-;-;-", lwt.txtblock.Text, charts.lstCharts[0].lstPoints[lwt.index].Item1.ToString("F4"), charts.lstCharts[0].lstPoints[lwt.index].Item2.ToString("F4")).Split(';'), XBrushes.LightBlue, new XStringFormat[] { XStringFormats.CenterLeft, XStringFormats.CenterRight, XStringFormats.CenterRight, XStringFormats.Center, XStringFormats.Center, XStringFormats.Center });
                nTop += 20;

                dLength = (charts.lstCharts[0].lstPoints[charts.lstCharts[0].lstEdgeLines[i + 1].index].Item1 - charts.lstCharts[0].lstPoints[charts.lstCharts[0].lstEdgeLines[i].index].Item1);
                dLoss = (charts.lstCharts[0].lstPoints[charts.lstCharts[0].lstEdgeLines[i].index].Item2 - charts.lstCharts[0].lstPoints[charts.lstCharts[0].lstEdgeLines[i + 1].index].Item2);
                // GenTextToGridCell(grdEvents, (dLoss / dLength).ToString("F4"), 6, i * 2 + 1, HorizontalAlignment.Right);

                DrawTableRow(gfx, page, nTop, nColWidth, font10, string.Format("Section;-;-;{0};{1};{2}", dLength.ToString("F4"), dLoss.ToString("F4"), (dLoss / dLength).ToString("F4")).Split(';'), XBrushes.Orange, new XStringFormat[] { XStringFormats.CenterLeft, XStringFormats.Center, XStringFormats.Center, XStringFormats.CenterRight, XStringFormats.CenterRight, XStringFormats.CenterRight });
                nTop += 20;
                if (nTop + 50 > page.Height)
                {
                    // konec stránky, přepnutí na novou
                    gfx.DrawLine(XPens.Black, 10, nTop, page.Width - 20, nTop); // vodorovná spodní čára
                    page = doc.AddPage();
                    gfx = XGraphics.FromPdfPage(page);
                    nTop = 10;
                    DrawTableRow(gfx, page, nTop, nColWidth, font10b, "Event/Section;Distance (Km);Value (dB);Length (Km);Loss (dB);Att (dB/Km)".Split(';'), XBrushes.LightGray, new XStringFormat[] { XStringFormats.CenterLeft, XStringFormats.CenterLeft, XStringFormats.CenterLeft, XStringFormats.CenterLeft, XStringFormats.CenterLeft, XStringFormats.CenterLeft });
                    nTop += 20;
                }
            }
            lwt = charts.lstCharts[0].lstEdgeLines[charts.lstCharts[0].lstEdgeLines.Count - 1];
            DrawTableRow(gfx, page, nTop, nColWidth, font10, string.Format("Event {0};{1};{2};-;-;-", lwt.txtblock.Text, charts.lstCharts[0].lstPoints[lwt.index].Item1.ToString("F4"), charts.lstCharts[0].lstPoints[lwt.index].Item2.ToString("F4")).Split(';'), XBrushes.LightBlue, new XStringFormat[] { XStringFormats.CenterLeft, XStringFormats.CenterRight, XStringFormats.CenterRight, XStringFormats.Center, XStringFormats.Center, XStringFormats.Center });
            nTop += 20;
            gfx.DrawLine(XPens.Black, 10, nTop, page.Width - 20, nTop); // vodorovná spodní čára

            // uloží dokument do souboru
            doc.Save(sFileName);
        }

        private void DrawTableRow(XGraphics gfx, PdfPage page, int nTop, int nColWidth, XFont font, string[] texts, XBrush bckclr, XStringFormat[] aligns)
        {
            if (bckclr != XBrushes.White)
            {
                XRect rect = new XRect(10, nTop, page.Width - 30, 20);
                gfx.DrawRectangle(bckclr, rect);
            }
            gfx.DrawLine(XPens.Black, 10, nTop, page.Width - 20, nTop); // vodorovná horní čára
            for (int nColNum = 0; nColNum < texts.Length; nColNum++)
            {
                gfx.DrawString(texts[nColNum], font, XBrushes.Black, new XRect(15 + nColNum * nColWidth, nTop, nColWidth-20, 20), aligns[nColNum]);
                gfx.DrawLine(XPens.Black, 10 + nColNum * nColWidth, nTop, 10 + nColNum * nColWidth, nTop + 20); // svislá čára
            }
            gfx.DrawLine(XPens.Black, texts.Length * nColWidth, nTop, texts.Length * nColWidth, nTop + 20); // svislá čára na konci
        }

        private XBrush BrushesToXBrush(System.Windows.Media.Brush color)
        {
            System.Windows.Media.Color tmpColor = (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString(color.ToString());
            XBrush resColor = new XSolidBrush(new XColor { R = tmpColor.R, G = tmpColor.G, B = tmpColor.B, A = tmpColor.A });

            return resColor;
        }
    }
}
