using System.IO;
using Microsoft.AspNetCore.Http;

namespace SelectPdf.Samples
{
    public class Helper
    {
        public static string WWWRoot(HttpRequest request)
        {
            string appPath = string.Empty;

            if (request != null)
            {
                //Formatting the fully qualified website url/name
                appPath = string.Format("{0}://{1}",
                            request.Scheme,
                            request.Host);
            }

            return appPath;
        }

        // App-root URL INCLUDING the PathBase and a trailing slash — the base a
        // converter must resolve a captured page's relative assets against
        // (e.g. ConvertHtmlString(html, baseUrl)). Under an IIS sub-application
        // (/demo-blazor) this is "https://host/demo-blazor/"; at the site root
        // "https://host/". Keep the trailing slash: the engine resolves relative
        // URLs per RFC 3986, so a missing slash would resolve assets at the wrong
        // (site-root) path. NOT a filesystem path (WebRootPath) — that loads no
        // relative stylesheets and the PDF comes out unstyled.
        public static string AppBaseUrl(HttpRequest request)
        {
            if (request == null) return "/";
            return $"{request.Scheme}://{request.Host}{request.PathBase}/";
        }

        // Reads a sample page's .cshtml.cs source off disk and pretty-prints it
        // via ColorCode for the "Sample code · C#" panel. Returns "" on any
        // error (missing file, etc.) so the panel hides cleanly. The page name
        // comes from RouteData["page"] (e.g. "/GettingStarted"); it is the
        // app-relative page path, so it carries no PathBase. NOTE the source
        // file must be shipped to the publish output (see the csproj
        // post-publish copy) or this is empty on a deployed app.
        public static string GetSampleCodeHtml(string serverPath, string pageName)
        {
            try
            {
                if (string.IsNullOrEmpty(pageName)) return string.Empty;
                string rel = pageName.Replace('/', System.IO.Path.DirectorySeparatorChar)
                                     .TrimStart(System.IO.Path.DirectorySeparatorChar);
                string path = Path.Combine(serverPath, "Pages", rel + ".cshtml.cs");
                if (!File.Exists(path)) return string.Empty;
                string code = File.ReadAllText(path);
                return new ColorCode.HtmlFormatter().GetHtmlString(code, ColorCode.Languages.CSharp);
            }
            catch
            {
                return string.Empty;
            }
        }

        public static string SomeShortText()
        {
            return @"This is some short text. This is some short text. This is some short text. This is some short text. This is some short text. This is some short text. This is some short text. This is some short text. This is some short text. This is some short text. This is some short text. This is some short text. This is some short text. This is some short text. This is some short text.";
        }

        public static string SomeText()
        {
            return @"This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. 

New paragraph. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. 

3rd paragraph. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. New paragraph. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. 

4th paragraph. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text.";
        }

        public static string SomeLongText()
        {
            return @"This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. 

New paragraph. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. 

3rd paragraph. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. New paragraph. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. 

4th paragraph. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. 

5th paragraph. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. 

6th paragraph. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. 

7th paragraph. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. 

8th paragraph. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. 

9th paragraph. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. 

10th paragraph. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text. This is some long text.";
        }

        public static string SomeVeryLongText()
        {
            return SomeLongText() + SomeLongText() + SomeLongText() + SomeLongText() + SomeLongText();
        }

        public static string PageBreaksText()
        {
            return @"<html>
    <body>
        <div style=""font-size: 28px; page-break-after: always"">
            This is a short paragraph of text. After it, a page break will be inserted. <br/>
            This is a short paragraph of text. After it, a page break will be inserted. <br/>
            This is a short paragraph of text. After it, a page break will be inserted. <br/>
            This is a short paragraph of text. After it, a page break will be inserted. <br/>
        </div>

        <div style=""font-size: 28px; "">
            This paragraph should appear in the second page (the first paragraph inserted a page break after it). <br/>
            This paragraph should appear in the second page (the first paragraph inserted a page break after it). <br/>
            This paragraph should appear in the second page (the first paragraph inserted a page break after it). <br/>
            This paragraph should appear in the second page (the first paragraph inserted a page break after it). <br/>
            This paragraph should appear in the second page (the first paragraph inserted a page break after it). <br/>
            This paragraph should appear in the second page (the first paragraph inserted a page break after it). <br/>
            This paragraph should appear in the second page (the first paragraph inserted a page break after it). <br/>
            This paragraph should appear in the second page (the first paragraph inserted a page break after it). <br/>
            This paragraph should appear in the second page (the first paragraph inserted a page break after it). <br/>
            This paragraph should appear in the second page (the first paragraph inserted a page break after it). <br/>
            This paragraph should appear in the second page (the first paragraph inserted a page break after it). <br/>
            This paragraph should appear in the second page (the first paragraph inserted a page break after it). <br/>
            This paragraph should appear in the second page (the first paragraph inserted a page break after it). <br/>
        </div>

        <div style=""font-size: 28px; page-break-inside: avoid; "">
            This paragraph should all be displayed in the same page (contains page-break-inside: avoid). Since it's too large to appear in the second page, it will all appear in the third page. <br/>
            This paragraph should all be displayed in the same page (contains page-break-inside: avoid). Since it's too large to appear in the second page, it will all appear in the third page. <br/>
            This paragraph should all be displayed in the same page (contains page-break-inside: avoid). Since it's too large to appear in the second page, it will all appear in the third page. <br/>
            This paragraph should all be displayed in the same page (contains page-break-inside: avoid). Since it's too large to appear in the second page, it will all appear in the third page. <br/>
            This paragraph should all be displayed in the same page (contains page-break-inside: avoid). Since it's too large to appear in the second page, it will all appear in the third page. <br/>
            This paragraph should all be displayed in the same page (contains page-break-inside: avoid). Since it's too large to appear in the second page, it will all appear in the third page. <br/>
            This paragraph should all be displayed in the same page (contains page-break-inside: avoid). Since it's too large to appear in the second page, it will all appear in the third page. <br/>
            This paragraph should all be displayed in the same page (contains page-break-inside: avoid). Since it's too large to appear in the second page, it will all appear in the third page. <br/>
            This paragraph should all be displayed in the same page (contains page-break-inside: avoid). Since it's too large to appear in the second page, it will all appear in the third page. <br/>
            This paragraph should all be displayed in the same page (contains page-break-inside: avoid). Since it's too large to appear in the second page, it will all appear in the third page. <br/>
        </div>
    </body>
</html>
";
        }

        public static float PixelsToPoints(float value)
        {
            return 96f * value / 72f;
        }

        public static float PointsToPixels(float value)
        {
            return 72f * value / 96f;
        }
    }
}
