using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Runtime.InteropServices;

using System.Drawing;
using System.Drawing.Printing;
using System.Drawing.Imaging;

/// <summary>
/// 
/// 测试可打印1200x1800的图片，页面参数{X=0,Y=0,Width=400,Height=600}
/// </summary>
public class PrinterManager : MonoBehaviour
{
  public static PrinterManager Instance;

  private PrinterSetting ps = new PrinterSetting();

  // ==================================================

  private void Awake() => Instance = this;
  private void Start() => Init();

  // ==================================================

  private void Init()
  {
    OperationalCheck();

    ps.PrinterName = "L805";
    ps.PaperSizeName = "6x4 Photo";
    ps.DPI = 100;
    ps.WidthInch = 4;
    ps.HeightInch = 6;

    SetPrinterName(ps.PrinterName);
  }

  // ==================================================
  #region 设置打印机

  public void SetPrinterName(string keyword)
  {
    foreach (string item in PrinterSettings.InstalledPrinters)
    {
      if (item.Contains(keyword))
      {
        ps.PrinterName = item;
        return;
      }
    }
    ps.PrinterName = new PrinterSettings().PrinterName;
  }

  #endregion
  // ==================================================

  /// <summary>
  /// 可运行性检查
  /// </summary>
  private void OperationalCheck()
  {
    if (!Application.isEditor && Application.platform != RuntimePlatform.WindowsPlayer)
    {
      Debug.LogWarning("[PM] 打印功能仅支持Windows平台");
    }
  }



  /// <summary>
  /// 打印照片
  /// </summary>
  /// <param name="texture"></param>
  public void Print(Texture2D texture)
  {
    try
    {
      Debug.Log($"[PM] 原始Texture2D尺寸: {texture.width}x{texture.height}");

      using (Bitmap bitmap = T2D2BitmapSafe(texture))
      {
        Debug.Log($"[PM] 转换后Bitmap尺寸: {bitmap.Width}x{bitmap.Height}");

        PrintBitmap(bitmap);
      }
    }
    catch (Exception e)
    {
      Debug.LogError("[PM] 打印失败: " + e.Message);
    }
  }



  /// <summary>
  /// 安全转换Texture2D到Bitmap
  /// </summary>
  /// <param name="texture"></param>
  /// <returns></returns>
  private Bitmap T2D2BitmapSafe(Texture2D texture)
  {
    Bitmap bitmap = new Bitmap(texture.width, texture.height, PixelFormat.Format32bppArgb);
    bitmap.SetResolution(ps.DPI, ps.DPI);
    BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, bitmap.PixelFormat);

    try
    {
      Color32[] pixels = texture.GetPixels32();
      byte[] bytes = new byte[pixels.Length * 4];

      for (int y = 0; y < texture.height; y++)
      {
        for (int x = 0; x < texture.width; x++)
        {
          int sourceIndex = y * texture.width + x;
          int destY = texture.height - 1 - y;
          int destIndex = destY * texture.width + x;

          bytes[destIndex * 4] = pixels[sourceIndex].b;
          bytes[destIndex * 4 + 1] = pixels[sourceIndex].g;
          bytes[destIndex * 4 + 2] = pixels[sourceIndex].r;
          bytes[destIndex * 4 + 3] = pixels[sourceIndex].a;
        }
      }

      Marshal.Copy(bytes, 0, bitmapData.Scan0, bytes.Length);
    }
    finally
    {
      bitmap.UnlockBits(bitmapData);
    }
    return bitmap;
  }



  /// <summary>
  /// 
  /// </summary>
  /// <param name="bitmap"></param>
  private void PrintBitmap(Bitmap bitmap)
  {
    using (PrintDocument pd = new PrintDocument())
    {
      pd.PrinterSettings.PrinterName = ps.PrinterName;
      pd.DefaultPageSettings.PrinterResolution = new PrinterResolution()
      {
        Kind = PrinterResolutionKind.Custom,
        X = ps.DPI,
        Y = ps.DPI
      };

      PaperSize photoSize = new PaperSize(ps.PaperSizeName, ps.WidthInch * ps.DPI, ps.HeightInch * ps.DPI);
      pd.DefaultPageSettings.PaperSize = photoSize;
      pd.DefaultPageSettings.Landscape = ps.Landscape;
      pd.DefaultPageSettings.Margins = new Margins((int)ps.Margin.x, (int)ps.Margin.y, (int)ps.Margin.z, (int)ps.Margin.w);

      pd.PrintPage += (sender, args) =>
      {
        try
        {
          Debug.Log($"[PM] 开始绘制: 图片{bitmap.Width}x{bitmap.Height} / 页面{args.PageBounds}");

          // args.Graphics.DrawImage(bitmap, args.PageBounds);
          args.Graphics.DrawImage(bitmap, 0, 0, args.PageBounds.Width, args.PageBounds.Height);

          Debug.Log("[PM] 绘制完成");
        }
        catch (Exception e)
        {
          Debug.LogError("[PM] 绘制图像时出错: " + e.Message);
          args.Cancel = true;
        }
      };

      try
      {
        pd.Print();
        Debug.Log("[PM] 打印任务已发送");
      }
      catch (Exception e)
      {
        Debug.LogError("[PM] 打印过程中出错: " + e.Message);
      }
    }
  }

  // ==================================================
  #region Config

  [Serializable]
  public class PrinterSetting
  {
    public string PrinterName = "L805";
    public string PaperSizeName = "6x4 Photo";
    public int WidthInch = 4;
    public int HeightInch = 6;
    public int DPI = 100; // 1200x1800是6英寸在300dpi下的像素尺寸 // 400x600是6英寸在100dpi下的像素尺寸

    public bool Landscape = false;
    public Vector4 Margin = Vector4.zero; // 边距 // x,y,z,w
  }

  #endregion
}