using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;

namespace Inspectron.Epson;

/// <summary>
/// Interface for Epson printer operations, allowing swappable implementations
/// (e.g., real printer vs HTML output for testing/preview)
/// </summary>
public interface IEpsonPrinter : IAsyncDisposable
{
    /// <summary>
    /// Returns true if connected to the printer
    /// </summary>
    bool IsConnected { get; }

    /// <summary>
    /// Connect to the printer
    /// </summary>
    /// <param name="ip">Printer IP address (or identifier for non-network implementations)</param>
    /// <param name="port">Printer port (default 9100)</param>
    /// <param name="timeoutSeconds">Connection timeout in seconds (default 10)</param>
    Task ConnectAsync(string ip, int port = 9100, int timeoutSeconds = 10);

    /// <summary>
    /// Disconnect from the printer
    /// </summary>
    void Disconnect();

    /// <summary>
    /// Get general printer status
    /// </summary>
    Task<PrinterStatus> GetPrinterStatusAsync();

    /// <summary>
    /// Get offline status (reasons for offline)
    /// </summary>
    Task<OfflineStatus> GetOfflineStatusAsync();

    /// <summary>
    /// Get error status
    /// </summary>
    Task<ErrorStatus> GetErrorStatusAsync();

    /// <summary>
    /// Get paper sensor status
    /// </summary>
    Task<PaperSensorStatus> GetPaperSensorStatusAsync();

    /// <summary>
    /// Get TM-T20/220 specific status byte
    /// </summary>
    Task<byte> GetTM220StatusAsync();

    /// <summary>
    /// Get printer model/ID
    /// </summary>
    Task<byte?> GetPrinterIdAsync();

    /// <summary>
    /// Get comprehensive overall status with health assessment
    /// </summary>
    Task<OverallStatus> GetOverallStatusAsync();

    /// <summary>
    /// Initialize the printer
    /// </summary>
    Task InitAsync();

    /// <summary>
    /// Enable or disable quadruple size mode
    /// </summary>
    Task SetQuadrupleMode(bool enable);

    /// <summary>
    /// Enable or disable red color printing
    /// </summary>
    Task SetRedColor(bool enable);

    /// <summary>
    /// Enable or disable emphasized (bold) mode
    /// </summary>
    Task SetEmphasized(bool enable);

    /// <summary>
    /// Select printer font
    /// </summary>
    Task SelectFont(EpsonCommands.PrinterFont font);

    /// <summary>
    /// Enable or disable bigger font mode for TM-T20/220
    /// </summary>
    Task SetBiggerFontTM220(bool biggerWidth, bool biggerHeight = false, bool secondaryFont = true);

    /// <summary>
    /// Print text to the printer
    /// </summary>
    Task PrintTextAsync(string text);

    /// <summary>
    /// Set font size (magnification)
    /// </summary>
    /// <param name="width">Width magnification (1-8)</param>
    /// <param name="height">Height magnification (1-8)</param>
    Task SetFontSizeAsync(int width, int height);

    /// <summary>
    /// Print text, feed paper, and cut
    /// </summary>
    Task PrintTextAndCutAsync(string text, int feedLines = 5);

    /// <summary>
    /// Print the current buffer
    /// </summary>
    Task PrintBuffer();

    /// <summary>
    /// Feed specified number of lines
    /// </summary>
    Task FeedLinesAsync(int lines);

    /// <summary>
    /// Cut the paper
    /// </summary>
    Task CutAsync(bool fullCut=true);

    /// <summary>
    /// Load an image from a file path into the printer buffer
    /// </summary>
    Task LoadImageAsync(string imagePath, int maxWidth = 384);

    /// <summary>
    /// Load an image from a stream into the printer buffer
    /// </summary>
    Task LoadImageAsync(Stream imageStream, int maxWidth = 384);

    /// <summary>
    /// Load an image from an ImageSharp Image object into the printer buffer
    /// </summary>
    Task LoadImageAsync(Image<Rgba32> image, int maxWidth = 384);

    /// <summary>
    /// Print the previously loaded image
    /// </summary>
    Task PrintLoadedImage();

    /// <summary>
    /// Print an image using bit-image mode from a file path
    /// </summary>
    Task PrintImageBitModeAsync(string imagePath, EpsonCommands.BitImageMode mode = EpsonCommands.BitImageMode.SingleDensity8Dot, int maxWidth = 10);

    /// <summary>
    /// Print an image using bit-image mode from a stream
    /// </summary>
    Task PrintImageBitModeAsync(Stream imageStream, EpsonCommands.BitImageMode mode = EpsonCommands.BitImageMode.DoubleDensity24Dot, int maxWidth = 384);

    /// <summary>
    /// Print an image using bit-image mode from an ImageSharp Image object
    /// </summary>
    Task PrintImageBitModeAsync(Image<Rgba32> image, EpsonCommands.BitImageMode mode, int maxWidth);

    /// <summary>
    /// Send raw ESC/POS command bytes to the printer
    /// </summary>
    Task SendRawCommandAsync(byte[] command);

    /// <summary>
    /// Set the absolute horizontal print position
    /// </summary>
    Task SetAbsolutePrintPosition(int x);

    /// <summary>
    /// Set default line spacing
    /// </summary>
    Task SetDefaultLineSpacing();

    /// <summary>
    /// Set custom line spacing
    /// </summary>
    Task SetCustomLineSpacing(int spacing = 24);
}
