import 'package:bluetooth_print/bluetooth_print.dart';
import 'package:bluetooth_print/bluetooth_print_model.dart';
import 'package:cashumit/models/transaction.dart';
import 'package:cashumit/utils/currency_format.dart';
import 'package:intl/intl.dart';
import 'package:flutter/foundation.dart';

class PrintService {
  final BluetoothPrint bluetoothPrint = BluetoothPrint.instance;

  Future<bool> printTransaction(
      Transaction transaction, String storeName, String storeAddress) async {
    try {
      // Membuat konten struk
      List<LineText> list = [];

      // Header struk
      list.add(LineText(
          type: LineText.TYPE_TEXT,
          content: storeName,
          weight: 2,
          align: LineText.ALIGN_CENTER,
          linefeed: 1));

      list.add(LineText(
          type: LineText.TYPE_TEXT,
          content: storeAddress,
          align: LineText.ALIGN_CENTER,
          linefeed: 1));

      list.add(LineText(
          type: LineText.TYPE_TEXT,
          content: '============================',
          align: LineText.ALIGN_CENTER,
          linefeed: 1));

      // Detail tanggal dan ID transaksi
      list.add(LineText(
          type: LineText.TYPE_TEXT,
          content:
              'Tanggal: ${DateFormat('dd/MM/yyyy HH:mm').format(transaction.timestamp)}',
          align: LineText.ALIGN_LEFT));

      list.add(LineText(
          type: LineText.TYPE_TEXT,
          content: 'ID: ${transaction.id.substring(0, 8)}',
          align: LineText.ALIGN_LEFT,
          linefeed: 1));

      list.add(LineText(
          type: LineText.TYPE_TEXT,
          content: '----------------------------',
          align: LineText.ALIGN_CENTER,
          linefeed: 1));

      // Item-item transaksi
      for (var item in transaction.items) {
        list.add(LineText(
            type: LineText.TYPE_TEXT,
            content: '${item.quantity}x ${item.name}',
            align: LineText.ALIGN_LEFT));

        final itemTotal = item.price * item.quantity;
        list.add(LineText(
            type: LineText.TYPE_TEXT,
            content:
                'Rp ${CurrencyFormat.formatRupiahWithoutSymbol(itemTotal)}',
            align: LineText.ALIGN_RIGHT,
            linefeed: 1));
      }

      list.add(LineText(
          type: LineText.TYPE_TEXT,
          content: '----------------------------',
          align: LineText.ALIGN_CENTER,
          linefeed: 1));

      // Total
      list.add(LineText(
          type: LineText.TYPE_TEXT,
          content: 'TOTAL',
          weight: 2,
          align: LineText.ALIGN_LEFT));

      list.add(LineText(
          type: LineText.TYPE_TEXT,
          content:
              'Rp ${CurrencyFormat.formatRupiahWithoutSymbol(transaction.total)}',
          weight: 2,
          align: LineText.ALIGN_RIGHT,
          linefeed: 1));

      // Pembayaran
      list.add(LineText(
          type: LineText.TYPE_TEXT,
          content: 'BAYAR',
          align: LineText.ALIGN_LEFT));

      list.add(LineText(
          type: LineText.TYPE_TEXT,
          content: transaction.paymentMethod,
          align: LineText.ALIGN_RIGHT,
          linefeed: 1));

      // Footer
      list.add(LineText(
          type: LineText.TYPE_TEXT,
          content: '============================',
          align: LineText.ALIGN_CENTER,
          linefeed: 1));

      list.add(LineText(
          type: LineText.TYPE_TEXT,
          content: 'Terima kasih atas kunjungan Anda!',
          align: LineText.ALIGN_CENTER,
          linefeed: 1));

      list.add(LineText(
          type: LineText.TYPE_TEXT,
          content: 'Barang yang sudah dibeli tidak dapat',
          align: LineText.ALIGN_CENTER));

      list.add(LineText(
          type: LineText.TYPE_TEXT,
          content: 'dikembalikan/ditukar',
          align: LineText.ALIGN_CENTER,
          linefeed: 2));

      // Konfigurasi printer
      Map<String, dynamic> config = {
        'width': 48, // Lebar struk (dalam karakter)
        'height': 200, // Tinggi struk (dalam karakter)
        'gap': 0, // Jarak antar struk
      };

      // Cetak struk
      await bluetoothPrint.printReceipt(config, list);
      return true;
    } catch (e) {
      debugPrint('Error printing transaction: $e');
      return false;
    }
  }
}

