using System.Drawing;
// We move the usings outside the IF for the Language Server,
// but keep the SupportedOSPlatform attribute to satisfy the linker.
using System.Drawing.Printing;
using System.IO;
using System.Runtime.Versioning;

[assembly: SupportedOSPlatform("windows")]

namespace FrymasterBadgeApp.Services;

[SupportedOSPlatform("windows")]
public class PrinterService
{
    public PrinterService()
    {
        AppLogger.Info("PrinterService: Constructor started.");
        try
        {
            var test = System.Drawing.Color.White;
            AppLogger.Info("PrinterService: GDI+ Compatibility check passed.");
        }
        catch (Exception ex)
        {
            AppLogger.Error("PrinterService: GDI+ Compatibility check FAILED.", ex);
        }
    }

    public void PrintBase64Badge(string frontBase64, string backBase64, string printerName)
    {
        // Wrap the actual logic in the WINDOWS check so it doesn't try to compile on other platforms
#if WINDOWS
        try
        {
            AppLogger.Info($"PrinterService: Attempting to print to {printerName}");

            using (PrintDocument pd = new PrintDocument())
            {
                pd.PrinterSettings.PrinterName = printerName;
                pd.DefaultPageSettings.Landscape = true;
                pd.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);

                bool frontPrinted = false;

                pd.PrintPage += (sender, e) =>
                {
                    try
                    {
                        string currentB64 = !frontPrinted ? frontBase64 : backBase64;

                        using (var ms = new MemoryStream(Convert.FromBase64String(currentB64)))
                        using (var img = System.Drawing.Image.FromStream(ms))
                        {
                            if (img.Height > img.Width)
                            {
                                e.Graphics.TranslateTransform(e.PageBounds.Width, 0);
                                e.Graphics.RotateTransform(90);
                                e.Graphics.DrawImage(
                                    img,
                                    0,
                                    0,
                                    e.PageBounds.Height,
                                    e.PageBounds.Width
                                );
                            }
                            else
                            {
                                e.Graphics.DrawImage(
                                    img,
                                    0,
                                    0,
                                    e.PageBounds.Width,
                                    e.PageBounds.Height
                                );
                            }
                        }

                        if (!frontPrinted && !string.IsNullOrEmpty(backBase64))
                        {
                            frontPrinted = true;
                            e.HasMorePages = true;
                        }
                        else
                        {
                            e.HasMorePages = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        AppLogger.Error("PrinterService: Error during PrintPage event", ex);
                        e.HasMorePages = false;
                    }
                };

                pd.Print();
                AppLogger.Info("PrinterService: Print job sent to spooler.");
            }
        }
        catch (Exception ex)
        {
            AppLogger.Error("PrinterService: Critical failure in PrintBase64Badge", ex);
        }
#else
        AppLogger.Warn("PrinterService: PrintBase64Badge called on non-Windows platform.");
#endif
    }
}
