// Copyright (C) 2026 Jason M. Schwefel. All Rights Reserved.
package com.coldboreballisticsllc.scopedope.domain.printer

import android.graphics.Bitmap
import kotlinx.coroutines.flow.StateFlow

/** BLE device identity of the currently connected printer. */
data class ConnectedDeviceInfo(
    val mac:    String,
    val btName: String,   // BLE-advertised device name; falls back to MAC if unavailable
)

/** Connection and print job state. */
sealed class PrinterState {
    data object Disconnected                            : PrinterState()
    data object Scanning                                : PrinterState()
    data object Connecting                              : PrinterState()
    data object Ready                                   : PrinterState()
    data class  Printing(val progress: Float)           : PrinterState()  // 0.0–1.0
    data object Done                                    : PrinterState()
    data class  Error(val message: String)              : PrinterState()
}

/**
 * Abstraction over all label printers.
 * Implementations handle BLE pairing, data encoding, and packet transmission.
 * The print job always receives a [Bitmap] rendered at the printer's native
 * resolution — rendering is handled by [LabelRenderer], not the driver.
 */
interface PrinterSource {
    val displayName: String
    val state: StateFlow<PrinterState>

    /** Non-null while a printer is connected (Ready / Printing / Done); null otherwise. */
    val connectedDeviceInfo: StateFlow<ConnectedDeviceInfo?>

    /** Native print resolution in DPI (used by renderer to size the bitmap). */
    val nativeDpi: Int

    suspend fun connect(targetMac: String? = null)
    suspend fun disconnect()

    /**
     * Print a single label.
     * @param bitmap 1-bit (black/white) bitmap at [nativeDpi] resolution.
     */
    suspend fun print(bitmap: Bitmap)
}
