import { TransactionEntity } from 'src/modules/transaction/transaction/domain/entities/transaction.entity';
import {
  mappingBody,
  mappingBodyBottom,
  mappingFooter,
  mappingHeader,
  mappingItem,
  mappingPrice,
} from './helpers/invoice-mapping.helper';
import { InvoiceType } from '../../constants';
import { PaymentMethodEntity } from 'src/modules/transaction/payment-method/domain/entities/payment-method.entity';
import * as fs from 'fs';

export function InvoiceTemplate(
  transaction: TransactionEntity,
  invoiceType: InvoiceType,
  banks: PaymentMethodEntity[],
) {
  const filePath = './assets/image/logo.jpeg';
  const imageBase64 = fs.readFileSync(filePath).toString('base64');
  const imageUrl = `data:image/png;base64,${imageBase64}`;

  return {
    content: [
      {
        alignment: 'justify',
        columns: mappingHeader(transaction, invoiceType),
      },

      '\n\n',

      // tipe booking date change tidak ada
      {
        columns: [
          {
            text: `Dear, \n ${transaction.customer_name} \n ${
              transaction.customer_phone
            } \n Booking Date: ${new Date(
              transaction.booking_date,
            ).toDateString()}`,
            bold: true,
          },
        ],
      },

      '\n',
      {
        text: mappingBody(transaction, invoiceType),
      },
      '\n',

      {
        layout: {
          defaultBorder: false,
          hLineWidth: function () {
            return 1;
          },
          vLineWidth: function () {
            return 1;
          },
          hLineColor: function (i) {
            return i === 0 ? '#000000' : '#eaeaea';
          },
          vLineColor: function () {
            return '#eaeaea';
          },
          hLineStyle: function () {
            // if (i === 0 || i === node.table.body.length) {
            return null;
            //}
          },
          // vLineStyle: function (i, node) { return {dash: { length: 10, space: 4 }}; },
          paddingLeft: function () {
            return 10;
          },
          paddingRight: function () {
            return 10;
          },
          paddingTop: function () {
            return 3;
          },
          paddingBottom: function () {
            return 3;
          },
          fillColor: function () {
            return '#fff';
          },
        },
        table: {
          widths: ['*', 'auto', 'auto', 100],
          body: mappingItem(transaction, invoiceType),
        },
      },
      '\n',
      {
        layout: {
          defaultBorder: false,
          hLineWidth: function () {
            return 1;
          },
          vLineWidth: function () {
            return 1;
          },
          hLineColor: function (i) {
            return i === 3 ? '#000000' : '#eaeaea';
          },
          vLineColor: function () {
            return '#eaeaea';
          },
          hLineStyle: function () {
            // if (i === 0 || i === node.table.body.length) {
            return null;
            //}
          },
          // vLineStyle: function () { return {dash: { length: 10, space: 4 }}; },
          paddingLeft: function () {
            return 10;
          },
          paddingRight: function () {
            return 10;
          },
          paddingTop: function () {
            return 3;
          },
          paddingBottom: function (i) {
            return i === 2 ? 20 : 3;
          },
          fillColor: function () {
            return '#fff';
          },
        },
        table: {
          headerRows: 1,
          widths: ['*', 100],
          body: mappingPrice(transaction, invoiceType),
        },
      },

      '\n',
      {
        text: mappingBodyBottom(transaction, invoiceType, banks),
      },
      '\n',

      {
        layout: {
          defaultBorder: false,
          hLineWidth: function () {
            return 1;
          },
          vLineWidth: function () {
            return 1;
          },
          hLineColor: function (i) {
            return i === 0 ? '#000000' : '#eaeaea';
          },
          vLineColor: function () {
            return '#eaeaea';
          },
          hLineStyle: function () {
            // if (i === 0 || i === node.table.body.length) {
            return null;
            //}
          },
          // vLineStyle: function () { return {dash: { length: 10, space: 4 }}; },
          paddingLeft: function () {
            return 10;
          },
          paddingRight: function () {
            return 10;
          },
          paddingTop: function () {
            return 20;
          },
          paddingBottom: function () {
            return 3;
          },
          fillColor: function () {
            return '#fff';
          },
        },
        table: {
          widths: ['*', 200],
          body: mappingFooter(),
        },
      },
    ],
    styles: {
      notesTitle: {
        fontSize: 10,
        bold: true,
        margin: [0, 50, 0, 3],
      },
      notesText: {
        fontSize: 10,
      },
      tableExample: {
        margin: [0, 5, 0, 15],
        headerRows: 1,
        widths: ['*', 100],
      },
      tableHeader: {
        bold: true,
        fontSize: 13,
        color: 'black',
      },
    },
    defaultStyle: {
      columnGap: 20,
    },
    images: {
      data: imageUrl,
    },
  };
}
