import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:sunmi_printer_plus/sunmi_printer_plus.dart';

/// Service for printing to Sunmi thermal printers
class SunmiService {
  /// Print warehouse export form to Sunmi printer
  static Future<void> printWarehouseExport({
    required BuildContext context,
    required String warehouseName,
    required int productId,
    required String productCode,
    required String productName,
    String? stageName,
    required double passedKg,
    required int passedPcs,
    required double issuedKg,
    required int issuedPcs,
    String? responsibleName,
    String? receiverName,
    String? barcodeData,
  }) async {
    try {
      // Format current date
      final dt = DateFormat('dd/MM/yyyy HH:mm').format(DateTime.now());

      // Title - PHIẾU XUẤT KHO
      await SunmiPrinter.printText(
        'PHIEU XUAT KHO',
        style: SunmiTextStyle(
          align: SunmiPrintAlign.CENTER,
          bold: true,
          fontSize: 48,
        ),
      );
      await SunmiPrinter.lineWrap(1);

      // Company name
      await SunmiPrinter.printText(
        'Cong ty TNHH Co Khi Chinh Xac Minh Thu',
        style: SunmiTextStyle(
          align: SunmiPrintAlign.CENTER,
          fontSize: 32,
        ),
      );
      await SunmiPrinter.lineWrap(1);

      // Warehouse name
      await SunmiPrinter.printText(
        warehouseName,
        style: SunmiTextStyle(align: SunmiPrintAlign.CENTER),
      );
      await SunmiPrinter.lineWrap(1);

      // Date
      await SunmiPrinter.printText(
        'Ngay: $dt',
        style: SunmiTextStyle(align: SunmiPrintAlign.CENTER),
      );
      await SunmiPrinter.lineWrap(2);

      // Separator line
      await SunmiPrinter.line();
      await SunmiPrinter.lineWrap(1);

      // Product information
      await SunmiPrinter.printText(
        'THONG TIN SAN PHAM',
        style: SunmiTextStyle(
          align: SunmiPrintAlign.LEFT,
          bold: true,
        ),
      );
      await SunmiPrinter.lineWrap(1);

      // ProductId
      await SunmiPrinter.printText(
        'ProductId: $productId',
        style: SunmiTextStyle(align: SunmiPrintAlign.LEFT),
      );
      await SunmiPrinter.lineWrap(1);

      // Product Code
      await SunmiPrinter.printText(
        'Ma san pham: $productCode',
        style: SunmiTextStyle(align: SunmiPrintAlign.LEFT),
      );
      await SunmiPrinter.lineWrap(1);

      // Product Name
      await SunmiPrinter.printText(
        'Ten san pham: $productName',
        style: SunmiTextStyle(align: SunmiPrintAlign.LEFT),
      );
      await SunmiPrinter.lineWrap(1);

      // Stage Name
      await SunmiPrinter.printText(
        'Cong doan: ${stageName ?? '-'}',
        style: SunmiTextStyle(align: SunmiPrintAlign.LEFT),
      );
      await SunmiPrinter.lineWrap(2);

      // Separator line
      await SunmiPrinter.line();
      await SunmiPrinter.lineWrap(1);

      // Quantities
      await SunmiPrinter.printText(
        'SO LUONG',
        style: SunmiTextStyle(
          align: SunmiPrintAlign.LEFT,
          bold: true,
        ),
      );
      await SunmiPrinter.lineWrap(1);

      // Table header
      await SunmiPrinter.printText(
        'Loai          KG         PCS',
        style: SunmiTextStyle(align: SunmiPrintAlign.LEFT),
      );
      await SunmiPrinter.line();

      // Passed quantity (Hàng đạt)
      final passedLine =
          'Hang dat      ${passedKg.toStringAsFixed(2).padLeft(7)}     ${passedPcs.toString().padLeft(5)}';
      await SunmiPrinter.printText(
        passedLine,
        style: SunmiTextStyle(align: SunmiPrintAlign.LEFT),
      );

      // Issued quantity (Hàng lỗi)
      final issuedLine =
          'Hang loi      ${issuedKg.toStringAsFixed(2).padLeft(7)}     ${issuedPcs.toString().padLeft(5)}';
      await SunmiPrinter.printText(
        issuedLine,
        style: SunmiTextStyle(align: SunmiPrintAlign.LEFT),
      );
      await SunmiPrinter.lineWrap(2);

      // Separator line
      await SunmiPrinter.line();
      await SunmiPrinter.lineWrap(1);

      // Responsible person
      await SunmiPrinter.printText(
        'Nhan vien kho: ${responsibleName ?? '-'}',
        style: SunmiTextStyle(align: SunmiPrintAlign.LEFT),
      );
      await SunmiPrinter.lineWrap(1);

      // Receiver
      await SunmiPrinter.printText(
        'Nhan vien tiep nhan: ${receiverName ?? '-'}',
        style: SunmiTextStyle(align: SunmiPrintAlign.LEFT),
      );
      await SunmiPrinter.lineWrap(2);

      // Barcode
      if (barcodeData != null && barcodeData.isNotEmpty) {
        await SunmiPrinter.line();
        await SunmiPrinter.printBarCode(
          barcodeData,
          style: SunmiBarcodeStyle(
            type: SunmiBarcodeType.CODE128,
            textPos: SunmiBarcodeTextPos.TEXT_UNDER,
            height: 100,
            align: SunmiPrintAlign.CENTER,
          ),
        );
        await SunmiPrinter.lineWrap(2);
      }

      // Footer
      await SunmiPrinter.line();
      await SunmiPrinter.printText(
        'Nguoi nhan',
        style: SunmiTextStyle(align: SunmiPrintAlign.CENTER),
      );
      await SunmiPrinter.lineWrap(4);

      // Cut paper
      await SunmiPrinter.cutPaper();

      // Show success message
      if (context.mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(
            content: Text('Đã in thành công!'),
            backgroundColor: Colors.green,
            duration: Duration(seconds: 2),
          ),
        );
      }
    } catch (e) {
      // Show error message
      if (context.mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(
            content: Text('Lỗi khi in: ${e.toString()}'),
            backgroundColor: Colors.red,
            duration: const Duration(seconds: 3),
          ),
        );
      }
      rethrow;
    }
  }
}
