import 'dart:async';
import 'dart:io';

import 'package:esc_pos_utils/esc_pos_utils.dart';
import 'package:flutter_pos_printer_platform_image_3/flutter_pos_printer_platform_image_3.dart';
import 'package:intl/intl.dart';
import 'package:path_provider/path_provider.dart';

class PrintTicket {
  // Future printTest() async {
  //   List<int> bytes = [];
  //   var profile = await CapabilityProfile.load();

  //   var generator = Generator(PaperSize.mm80, profile);
  //   bytes += generator.setGlobalCodeTable('CP1252');
  //   bytes += generator.text('TEST PRINT',
  //       styles: const PosStyles(align: PosAlign.center));
  //   bytes += generator.cut();
  //   PrinterManager.instance.send(type: PrinterType.usb, bytes: bytes);
  // }

  Future printTicket(
      {required String boothId,
      required String header,
      required String date,
      required String time,
      required String antrianNumb,
      required String name,
      required String location,
      required StringqrCode,
      required String isConsult,
      required String DoctorName,
      required String layananName,
      required String capabilityName,
      required String footer}) async {
    try {
      List<int> bytes = [];
      final dt = DateFormat('yyyy-MM-dd-HH-mm-ss').format(DateTime.now());
      final splited = dt.split('-');
      final date = splited[2].toString();
      final month = splited[1].toString();
      final year = splited[0].toString();
      final hour = splited[3].toString();
      final minute = splited[4].toString();

      final displayDate = "$date/$month/$year - $hour.$minute WIB";

      final profile = await CapabilityProfile.load(name: capabilityName);
      final avl = await CapabilityProfile.getAvailableProfiles();
      // final avl = await CapabilityProfile.getAvailableProfiles(name: "XP-N160I");
      // print(avl);

      var generator = Generator(PaperSize.mm80, profile);

      bytes += generator.text(
        "Booth :$boothId",
        styles: const PosStyles(
            align: PosAlign.right,
            bold: true,
            height: PosTextSize.size1,
            width: PosTextSize.size1),
      );
      // bytes += generator.text(
      //   "capability :$capabilityName",
      //   styles: const PosStyles(
      //       align: PosAlign.right,
      //       bold: true,
      //       height: PosTextSize.size1,
      //       width: PosTextSize.size1),
      // );
      bytes += generator.hr(ch: " ");

      bytes += generator.text(
        header,
        styles: const PosStyles(
            align: PosAlign.center,
            bold: true,
            height: PosTextSize.size1,
            width: PosTextSize.size1),
      );

      bytes += generator.hr(ch: " ");
      bytes += generator.text(
        displayDate,
        styles: const PosStyles(
            align: PosAlign.center,
            bold: false,
            height: PosTextSize.size1,
            width: PosTextSize.size1),
      );
      bytes += generator.hr(ch: " ");
      bytes += generator.text("NOMOR ANTRIAN",
          styles: const PosStyles(
              align: PosAlign.center,
              bold: true,
              height: PosTextSize.size1,
              width: PosTextSize.size1),
          linesAfter: 1);
      if (isConsult == 'N') {
        bytes += generator.text("${layananName}",
            styles: const PosStyles(
                align: PosAlign.center,
                bold: true,
                height: PosTextSize.size1,
                width: PosTextSize.size1),
            linesAfter: 1);
      }

      if (isConsult == 'Y') {
        bytes += generator.text("${layananName} ${DoctorName}",
            styles: const PosStyles(
                align: PosAlign.center,
                bold: true,
                height: PosTextSize.size1,
                width: PosTextSize.size1),
            linesAfter: 1);
      }

      bytes += generator.text(antrianNumb,
          styles: const PosStyles(
              align: PosAlign.center,
              bold: true,
              height: PosTextSize.size2,
              width: PosTextSize.size2),
          linesAfter: 1);
      // final List<dynamic> barData = "{A$antrianNumb".split("");
      // bytes += generator.barcode(Barcode.code128(barData),
      //     height: 60, textPos: BarcodeText.none);
      // bytes += generator.hr(ch: " ");
      bytes += generator.text(
        "SIMPAN TIKET INI HINGGA",
        styles: const PosStyles(
            align: PosAlign.center,
            bold: false,
            height: PosTextSize.size1,
            width: PosTextSize.size1),
      );
      bytes += generator.text(
        "SELURUH LAYANAN ANDA SELESAI",
        styles: const PosStyles(
            align: PosAlign.center,
            bold: false,
            height: PosTextSize.size1,
            width: PosTextSize.size1),
      );
      bytes += generator.hr(ch: " ");
      if (name != "") {
        bytes += generator.text(
          name,
          styles: const PosStyles(
              align: PosAlign.center,
              bold: false,
              height: PosTextSize.size1,
              width: PosTextSize.size1),
        );
      }
      // bytes += generator.text(
      //   location,
      //   linesAfter: 1,
      //   styles: const PosStyles(
      //       align: PosAlign.center,
      //       bold: false,
      //       height: PosTextSize.size1,
      //       width: PosTextSize.size1),
      // );
      // bytes += generator.text(
      //   "LAYANAN LABORATORIUM MEDIS",
      //   styles: const PosStyles(
      //       align: PosAlign.center,
      //       bold: false,
      //       height: PosTextSize.size1,
      //       width: PosTextSize.size1),
      // );

      // bytes +=
      //     generator.qrcode("$StringqrCode$antrianNumb", size: QRSize.Size8);
      // bytes += generator.hr(ch: " ");

      bytes += generator.text(
        footer,
        styles: const PosStyles(
            align: PosAlign.center,
            bold: false,
            height: PosTextSize.size1,
            width: PosTextSize.size1),
      );

      bytes += generator.cut();
      // final directory = await getApplicationDocumentsDirectory();

      // final file =
      //     File('${directory.path}/$antrianNumb-$date-$month-$year.txt');
      // await file.writeAsString(bytes.toString());

      PrinterManager.instance.send(type: PrinterType.usb, bytes: bytes);
    } catch (e) {
      print(e);
    }
  }

  Future printTest(
      {String boothId = "Test Booth",
      String header = "Test header",
      String date = "Test Date",
      String time = "Test time",
      String antrianNumb = "Test01",
      String name = "Test name",
      String location = "Test location",
      StringqrCode = "Test01",
      String isConsult = "N",
      String DoctorName = "-",
      String layananName = "Test Layanan",
      String footer = "Test footer"}) async {
    try {
      List<int> bytes = [];
      final dt = DateFormat('yyyy-MM-dd-HH-mm-ss').format(DateTime.now());
      final splited = dt.split('-');
      final date = splited[2].toString();
      final month = splited[1].toString();
      final year = splited[0].toString();
      final hour = splited[3].toString();
      final minute = splited[4].toString();

      final displayDate = "$date/$month/$year - $hour.$minute WIB";

      final profile = await CapabilityProfile.load(name: "XP-N160I");

      var generator = Generator(PaperSize.mm80, profile);

      bytes += generator.text(
        "Booth :$boothId",
        styles: const PosStyles(
            align: PosAlign.right,
            bold: true,
            height: PosTextSize.size1,
            width: PosTextSize.size1),
      );
      bytes += generator.hr(ch: " ");

      bytes += generator.text(
        header,
        styles: const PosStyles(
            align: PosAlign.center,
            bold: true,
            height: PosTextSize.size1,
            width: PosTextSize.size1),
      );

      bytes += generator.hr(ch: " ");
      bytes += generator.text(
        displayDate,
        styles: const PosStyles(
            align: PosAlign.center,
            bold: false,
            height: PosTextSize.size1,
            width: PosTextSize.size1),
      );
      bytes += generator.hr(ch: " ");
      bytes += generator.text("NOMOR ANTRIAN",
          styles: const PosStyles(
              align: PosAlign.center,
              bold: true,
              height: PosTextSize.size1,
              width: PosTextSize.size1),
          linesAfter: 1);
      if (isConsult == 'N') {
        bytes += generator.text("${layananName}",
            styles: const PosStyles(
                align: PosAlign.center,
                bold: true,
                height: PosTextSize.size1,
                width: PosTextSize.size1),
            linesAfter: 1);
      }

      if (isConsult == 'Y') {
        bytes += generator.text("${layananName} ${DoctorName}",
            styles: const PosStyles(
                align: PosAlign.center,
                bold: true,
                height: PosTextSize.size1,
                width: PosTextSize.size1),
            linesAfter: 1);
      }

      bytes += generator.text(antrianNumb,
          styles: const PosStyles(
              align: PosAlign.center,
              bold: true,
              height: PosTextSize.size8,
              width: PosTextSize.size8),
          linesAfter: 1);
      final List<dynamic> barData = "{A$antrianNumb".split("");
      bytes += generator.barcode(Barcode.code128(barData),
          height: 60, textPos: BarcodeText.none);
      bytes += generator.hr(ch: " ");
      bytes += generator.text(
        "SIMPAN TIKET INI HINGGA",
        styles: const PosStyles(
            align: PosAlign.center,
            bold: false,
            height: PosTextSize.size1,
            width: PosTextSize.size1),
      );
      bytes += generator.text(
        "SELURUH LAYANAN ANDA SELESAI",
        styles: const PosStyles(
            align: PosAlign.center,
            bold: false,
            height: PosTextSize.size1,
            width: PosTextSize.size1),
      );
      bytes += generator.hr(ch: " ");
      if (name != "") {
        bytes += generator.text(
          name,
          styles: const PosStyles(
              align: PosAlign.center,
              bold: false,
              height: PosTextSize.size1,
              width: PosTextSize.size1),
        );
      }
      bytes += generator.text(
        location,
        linesAfter: 1,
        styles: const PosStyles(
            align: PosAlign.center,
            bold: false,
            height: PosTextSize.size1,
            width: PosTextSize.size1),
      );
      // bytes += generator.text(
      //   "LAYANAN LABORATORIUM MEDIS",
      //   styles: const PosStyles(
      //       align: PosAlign.center,
      //       bold: false,
      //       height: PosTextSize.size1,
      //       width: PosTextSize.size1),
      // );

      // bytes +=
      //     generator.qrcode("$StringqrCode$antrianNumb", size: QRSize.Size8);
      // bytes += generator.hr(ch: " ");

      bytes += generator.text(
        footer,
        styles: const PosStyles(
            align: PosAlign.center,
            bold: false,
            height: PosTextSize.size1,
            width: PosTextSize.size1),
      );

      bytes += generator.cut();
      // final directory = await getApplicationDocumentsDirectory();

      // final file =
      //     File('${directory.path}/$antrianNumb-$date-$month-$year.txt');
      // await file.writeAsString(bytes.toString());

      await PrinterManager.instance.send(type: PrinterType.usb, bytes: bytes);
    } catch (e) {
      print(e);
    }
  }
}
