import 'package:esc_pos_utils_plus/esc_pos_utils_plus.dart';
import 'package:flutter/services.dart';
import 'package:http/http.dart' as http;
import 'package:image/image.dart' as IMG;
import 'package:image/image.dart';
import 'package:internet_connection_checker_plus/internet_connection_checker_plus.dart';
import 'package:print_bluetooth_thermal/print_bluetooth_thermal.dart';

import '../constant.dart';
import '../model/print_transaction_model.dart';

class CustomInvoicePrinting {
  Future<bool> printCustomTicket(
      {required PrintPurchaseTransactionModel printTransactionModel, required String data}) async {
    bool isPrinted = false;
    bool? isConnected = await PrintBluetoothThermal.connectionStatus;
    if (isConnected == true) {
      List<int> bytes =
          await customPrintTicket(printTransactionModel: printTransactionModel, data: data);
      await PrintBluetoothThermal.writeBytes(bytes);
      isPrinted = true;
    } else {
      isPrinted = false;
    }

    return isPrinted;
  }

  Future<List<int>> customPrintTicket(
      {required PrintPurchaseTransactionModel printTransactionModel, required String data}) async {
    List<int> bytes = [];
    bool is80mm = (printTransactionModel.personalInformationModel.thermalPrinterPrintSize ==
            '3 Inch (80 mm)' &&
        printTransactionModel.personalInformationModel.thermalPrinterPrintSize != null);

    CapabilityProfile profile = await CapabilityProfile.load();
    final generator = Generator(is80mm ? PaperSize.mm80 : PaperSize.mm58, profile);

    ///__________Shop_logo_________________________________________
    bool result = await InternetConnection().hasInternetAccess;
    if (result) {
      http.Response response = await http.get(
        Uri.parse(printTransactionModel.personalInformationModel.pictureUrl ?? ''),
      );
      response.bodyBytes;
      final decodedImage = IMG.decodeImage(response.bodyBytes);
      final resizedImage = IMG.copyResize(decodedImage!, width: 120, height: 120); //Uint8List
      final encodedImage = IMG.encodeJpg(resizedImage);
      Uint8List byteList = Uint8List.fromList(encodedImage);
      final Uint8List imageBytes = byteList;
      bytes += generator.image(decodeImage(imageBytes)!);
    }
    bytes += generator.text(printTransactionModel.personalInformationModel.companyName ?? '',
        styles: const PosStyles(
          align: PosAlign.center,
          height: PosTextSize.size2,
          width: PosTextSize.size2,
        ),
        linesAfter: 1);

    bytes += generator.text(printTransactionModel.personalInformationModel.countryName ?? '',
        styles: const PosStyles(align: PosAlign.center));
    bytes += generator.text(
        'Tel: ${printTransactionModel.personalInformationModel.phoneNumber ?? ''}',
        styles: const PosStyles(align: PosAlign.center),
        linesAfter: printTransactionModel.personalInformationModel.gst.trim().isNotEmpty ? 0 : 1);
    printTransactionModel.personalInformationModel.gst.trim().isNotEmpty
        ? bytes += generator.text(
            'Shop GST: ${printTransactionModel.personalInformationModel.gst ?? ''}',
            styles: const PosStyles(align: PosAlign.center),
            linesAfter: 1)
        : null;
    bytes += generator.text(
        'Name: ${printTransactionModel.purchaseTransitionModel?.customerName ?? 'Guest'}',
        styles: const PosStyles(align: PosAlign.left));
    (printTransactionModel.purchaseTransitionModel?.customerGst.trim().isNotEmpty ?? false)
        ? bytes += generator.text(
            'Party GST: ${printTransactionModel.purchaseTransitionModel?.customerGst ?? 'Not Provided'}',
            styles: const PosStyles(align: PosAlign.left))
        : null;
    bytes += generator.text(
        'mobile: ${printTransactionModel.purchaseTransitionModel?.customerPhone ?? 'Not Provided'}',
        styles: const PosStyles(align: PosAlign.left),
        linesAfter: 1);
    bytes += generator.hr();
    bytes += generator.text(data, styles: const PosStyles(align: PosAlign.center));
    bytes +=
        generator.text('Thank you!', styles: const PosStyles(align: PosAlign.center, bold: true));

    bytes += generator.text(printTransactionModel.purchaseTransitionModel!.purchaseDate,
        styles: const PosStyles(align: PosAlign.center), linesAfter: 1);

    bytes += generator.text('Note: Goods once sold will not be taken back or exchanged.',
        styles: const PosStyles(align: PosAlign.center, bold: false), linesAfter: 1);

    bytes += generator.text('Developed By: $invoiceName',
        styles: const PosStyles(align: PosAlign.center), linesAfter: 1);
    if (is80mm) bytes += generator.emptyLines(1);
    bytes += generator.cut();
    return bytes;
  }
}
