import 'dart:developer';

import 'package:enaklo_pos/core/extensions/string_ext.dart';
import 'package:enaklo_pos/core/utils/printer_service.dart';
import 'package:enaklo_pos/data/dataoutputs/print_dataoutputs.dart';
import 'package:enaklo_pos/data/datasources/auth_local_datasource.dart';
import 'package:enaklo_pos/data/datasources/product_local_datasource.dart';
import 'package:enaklo_pos/data/models/response/order_response_model.dart';
import 'package:enaklo_pos/presentation/home/models/product_quantity.dart';
import 'package:flutter/material.dart';

Future<void> onPrint(
  BuildContext context, {
  required List<ProductQuantity> productQuantity,
  required Order order,
}) async {
  final checkerPrinter =
      await ProductLocalDatasource.instance.getPrinterByCode('checker');
  final kitchenPrinter =
      await ProductLocalDatasource.instance.getPrinterByCode('kitchen');
  final barPrinter =
      await ProductLocalDatasource.instance.getPrinterByCode('bar');

  // Checker printer
  if (checkerPrinter != null) {
    try {
      final printValue = await PrintDataoutputs.instance.printChecker(
        productQuantity,
        order.tableNumber ?? "",
        order.orderNumber ?? "",
        'kasir',
        checkerPrinter.paper.toIntegerFromText,
        order.orderType ?? "",
      );

      await PrinterService()
          // ignore: use_build_context_synchronously
          .printWithPrinter(checkerPrinter, printValue, context);
    } catch (e) {
      log("Error printing checker: $e");
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('Error printing checker: $e')),
      );
    }
  }

  // Kitchen printer
  if (kitchenPrinter != null) {
    try {
      final printValue = await PrintDataoutputs.instance.printKitchen(
        productQuantity,
        order.tableNumber!,
        order.orderNumber ?? "",
        'kasir',
        kitchenPrinter.paper.toIntegerFromText,
        order.orderType ?? "",
      );

      await PrinterService()
          .printWithPrinter(kitchenPrinter, printValue, context);
    } catch (e) {
      log("Error printing kitchen order: $e");
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('Error printing kitchen order: $e')),
      );
    }
  }

  // Bar printer
  if (barPrinter != null) {
    try {
      final printValue = await PrintDataoutputs.instance.printBar(
        productQuantity,
        order.tableNumber ?? "",
        order.orderNumber ?? "",
        'kasir',
        barPrinter.paper.toIntegerFromText,
        order.orderType ?? "",
      );

      await PrinterService().printWithPrinter(barPrinter, printValue, context);
    } catch (e) {
      log("Error printing bar order: $e");
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('Error printing bar order: $e')),
      );
    }
  }
}

Future<void> onPrintRecipt(
  context, {
  required Order order,
  required String paymentMethod,
  required int nominalBayar,
  required int kembalian,
  required int taxPercentage,
}) async {
  final receiptPrinter =
      await ProductLocalDatasource.instance.getPrinterByCode('receipt');
  final authData = await AuthLocalDataSource().getAuthData();

  if (receiptPrinter != null) {
    try {
      final printValue = await PrintDataoutputs.instance.printOrderV4(
        order,
        authData.user?.name ?? "",
        paymentMethod,
        nominalBayar,
        kembalian,
        taxPercentage,
        receiptPrinter.paper.toIntegerFromText,
      );
      await PrinterService()
          .printWithPrinter(receiptPrinter, printValue, context);
    } catch (e) {
      log("Error printing receipt order: $e");
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('Error printing receipt order: $e')),
      );
    }
  }
}
