import { extractLastNumber, getSession } from '../../../../Services/Others';
import moment from 'moment';

const SingleTokenMobilePrint = async (PrintDetails, UpiId, Mode) => {
  console.log(PrintDetails, Mode, 'PrintDetailsPrintDetails');
  let PrintDetail;
  let PaymentDetail;
  if (Mode === 'Reprint') {
    PrintDetail = PrintDetails;
    PaymentDetail = PrintDetails?.[0]?.OrderPaymentDtl?.filter(
      (ps) => ps.PaymentStatus === 'S'
    );
  } else {
    PrintDetail = PrintDetails?.[0]?.OrderDetails;
    PaymentDetail = PrintDetails?.[0]?.PaymentOrderDtl?.filter(
      (ps) => ps.PaymentStatus === 'S'
    );
  }

  let receiptTextnormal = '';
  let LogoImage = '';
  let TokenData = '';
  let OrderId;
  let receiptTextest = '';
  let OrderIdSales;
  let OrderIdEst;
  let FooterPrint = 'No';
  let TokenDataEstimate = '';
  OrderIdSales = PrintDetail?.find((a) => a.OrderType === 'S')?.OrderId
    ? extractLastNumber(PrintDetail?.find((a) => a.OrderType === 'S')?.OrderId)
    : '';
  OrderIdEst = PrintDetail?.find((a) => a.OrderType === 'E')?.OrderId
    ? extractLastNumber(PrintDetail?.find((a) => a.OrderType === 'E')?.OrderId)
    : '';

  PrintDetail?.map((PrintDetails) => {
    OrderId = extractLastNumber(PrintDetails?.OrderId);
    let OrderDate = moment(PrintDetails?.CreatedDate).format(
      'DD-MM-YYYY, hh:mm A'
    );
    let Takeawaydata = PrintDetails?.productDetails?.filter(
      (item) =>
        item?.BookingTypeName?.toLowerCase() === 'takeaway' &&
        item?.TokenAvailable === 'Y'
    );

    let fulltakeawaydata;
    if (Takeawaydata?.length > 0) {
      let takeawaybookingdata = Takeawaydata?.map((data, index) => {
        const snoSpacing = Math.max(0, 3 - String(index + 1).length);
        // Calculate dynamic spacing for Rate
        const rateSpacing = Math.max(0, 6 - String(data?.Rate).length);
        const QtySpacing = Math.max(0, 3 - String(data?.SalesQty).length);
        // Calculate dynamic spacing for TotalAmt
        const totalAmtSpacing = Math.max(0, 7 - String(data?.TotalAmt).length);
        // Generate formatted string with dynamic spacings
        const formattedString =
          ' '.repeat(snoSpacing) +
          parseInt(index + 1) +
          '  ' +
          (data?.ProdNameQty + ' '.repeat(22)).substring(0, 22) +
          '  ' +
          ' '.repeat(QtySpacing) +
          String(data?.SalesQty) +
          ' ' +
          ' '.repeat(rateSpacing) +
          String(data?.Rate) +
          ' ' +
          ' '.repeat(totalAmtSpacing) +
          String(data?.TotalAmt) +
          ' ' +
          '\n';

        return formattedString;
      });
      fulltakeawaydata =
        '[C]<b>------------------------------------------------</b>\n' +
        '[L]<b>' +
        '' +
        'S.No' +
        '   ' +
        '  Item               ' +
        '  ' +
        'Qty ' +
        '  ' +
        'Rate ' +
        '   ' +
        'Amt' +
        '' +
        '</b>' +
        '[L]\n' +
        '[C]------------------------------------------------\n' +
        takeawaybookingdata.join('') +
        '[C]<b>------------------------------------------------</b>\n';
    } else {
      fulltakeawaydata = '';
    }

    // // let LogoImage
    // if(PrintDetails?.CompLogo !== null && PrintDetails?.CompLogo !== undefined){
    //   LogoImage = PrintDetails?.CompLogo
    // }else{
    //   LogoImage =""
    // }

    var receiptText =
      "[C]<b><font size='big'>" +
      'Token' +
      '</font></b>\n' +
      '[L]<b>Bill No :' +
      OrderId +
      '</b>[R]<b>' +
      OrderDate +
      '</b>\n' +
      fulltakeawaydata +
      '\n';

    if (PrintDetails?.OrderType === 'E') {
      receiptTextest = receiptText;
    } else {
      receiptTextnormal = receiptText;
    }
  });
  console.log(receiptTextnormal, receiptTextest, 'receiptText single token');
  var textEncoded = encodeURI(receiptTextnormal);
  var textEncodedest = encodeURI(receiptTextest);
  var TypeCheck = 'PrintReceipt';
  var scheme = 'pozoprinter';
  var packageName = 'com.example.pozoprinter';
  window.location.href =
    scheme +
    '://' +
    textEncoded +
    '#Intent;scheme=' +
    scheme +
    ';package=' +
    packageName +
    TypeCheck +
    'ImgS' +
    LogoImage +
    'ImgE' +
    'TokenS' +
    TokenData +
    'TokenE' +
    'OrderS' +
    OrderIdSales +
    'OrderE' +
    'EstimateS' +
    textEncodedest +
    'EstimateE' +
    'EstimateOrderIdS' +
    OrderIdEst +
    'EstimateOrderIdE' +
    'TokenEstimateS' +
    TokenDataEstimate +
    'TokenEstimateE' +
    'EstimateOrderIdS' +
    OrderIdEst +
    'EstimateOrderIdE' +
    'HasFooterTextS' +
    FooterPrint +
    'HasFooterTextE' +
    ';end;';
};
export default SingleTokenMobilePrint;
