/**
 * Barcode printing utility functions
 * Provides structure for patient wristbands and specimen labels
 * Note: Full barcode generation to be implemented with library integration
 */

/**
 * Print patient wristband with barcode
 * @param {Object} patient - Patient data
 * @param {string} patient.PatientID - Patient identifier
 * @param {string} patient.NameFirst - First name
 * @param {string} patient.NameLast - Last name
 * @param {string} patient.Birthdate - Birthdate
 * @param {string} patient.Sex - Sex (1=Female, 2=Male)
 */
export function printPatientWristband(patient) {
  if (!patient?.PatientID) {
    console.warn('Cannot print wristband: Missing patient ID');
    return;
  }

  const sexLabel = patient.Sex === '1' ? 'F' : patient.Sex === '2' ? 'M' : 'U';
  const birthdate = patient.Birthdate 
    ? new Date(patient.Birthdate).toLocaleDateString('en-US')
    : '-';
  
  const patientName = [patient.NameFirst, patient.NameLast]
    .filter(Boolean)
    .join(' ') || 'Unknown';

  const printContent = `
    <!DOCTYPE html>
    <html>
    <head>
      <title>Patient Wristband - ${patient.PatientID}</title>
      <style>
        @media print {
          body { margin: 0; padding: 10mm; font-family: Arial, sans-serif; }
          .wristband { 
            width: 250mm; 
            height: 25mm; 
            border: 2px solid #000; 
            padding: 3mm;
            box-sizing: border-box;
            display: flex;
            align-items: center;
            gap: 5mm;
          }
          .barcode-placeholder {
            width: 80mm;
            height: 18mm;
            background: #f0f0f0;
            border: 1px dashed #999;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 10px;
            color: #666;
          }
          .patient-info { flex: 1; }
          .patient-id { font-size: 14px; font-weight: bold; }
          .patient-name { font-size: 12px; margin-top: 2mm; }
          .patient-details { font-size: 10px; color: #666; margin-top: 1mm; }
        }
        @media screen {
          body { background: #f5f5f5; padding: 20px; }
          .wristband { background: white; margin: 20px auto; }
        }
      </style>
    </head>
    <body>
      <div class="wristband">
        <div class="barcode-placeholder">
          [BARCODE]<br>${patient.PatientID}
        </div>
        <div class="patient-info">
          <div class="patient-id">${patient.PatientID}</div>
          <div class="patient-name">${patientName}</div>
          <div class="patient-details">DOB: ${birthdate} | Sex: ${sexLabel}</div>
        </div>
      </div>
      <script>
        window.onload = function() { window.print(); };
      <\/script>
    </body>
    </html>
  `;

  const printWindow = window.open('', '_blank', 'width=800,height=400');
  if (printWindow) {
    printWindow.document.write(printContent);
    printWindow.document.close();
  } else {
    console.error('Failed to open print window - popup blocker may be enabled');
  }
}

/**
 * Print specimen label with barcode
 * @param {Object} order - Order data
 * @param {string} order.OrderID - Order identifier
 * @param {string} order.OrderNumber - Order number
 * @param {string} order.PatientID - Patient ID
 * @param {string} order.PatientName - Patient name
 * @param {Array} order.Tests - Array of tests
 * @param {string} order.OrderDate - Order date
 */
export function printSpecimenLabel(order) {
  if (!order?.OrderID) {
    console.warn('Cannot print label: Missing order ID');
    return;
  }

  const orderDate = order.OrderDate 
    ? new Date(order.OrderDate).toLocaleDateString('en-US')
    : new Date().toLocaleDateString('en-US');
  
  const testCodes = order.Tests?.map(t => t.TestSiteID || t.TestID).join(', ') || 'N/A';
  const patientName = order.PatientName || 'Unknown';
  const patientId = order.PatientID || order.InternalPID || 'N/A';

  const printContent = `
    <!DOCTYPE html>
    <html>
    <head>
      <title>Specimen Label - ${order.OrderNumber || order.OrderID}</title>
      <style>
        @media print {
          body { margin: 0; padding: 5mm; font-family: Arial, sans-serif; }
          .label { 
            width: 100mm; 
            height: 60mm; 
            border: 1px solid #000; 
            padding: 3mm;
            box-sizing: border-box;
          }
          .barcode-placeholder {
            width: 94mm;
            height: 20mm;
            background: #f0f0f0;
            border: 1px dashed #999;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 10px;
            color: #666;
            margin-bottom: 2mm;
          }
          .order-info { font-size: 11px; margin-bottom: 1mm; }
          .patient-info { font-size: 10px; color: #333; margin-bottom: 1mm; }
          .test-codes { 
            font-size: 9px; 
            color: #666; 
            border-top: 1px solid #ddd;
            padding-top: 1mm;
            margin-top: 2mm;
          }
          .date-info { font-size: 9px; color: #999; margin-top: 2mm; }
        }
        @media screen {
          body { background: #f5f5f5; padding: 20px; }
          .label { background: white; margin: 20px auto; }
        }
      </style>
    </head>
    <body>
      <div class="label">
        <div class="barcode-placeholder">
          [BARCODE]<br>${order.OrderNumber || order.OrderID}
        </div>
        <div class="order-info"><strong>Order:</strong> ${order.OrderNumber || order.OrderID}</div>
        <div class="patient-info"><strong>Patient:</strong> ${patientName} (${patientId})</div>
        <div class="test-codes"><strong>Tests:</strong> ${testCodes}</div>
        <div class="date-info">Collected: ${orderDate}</div>
      </div>
      <script>
        window.onload = function() { window.print(); };
      <\/script>
    </body>
    </html>
  `;

  const printWindow = window.open('', '_blank', 'width=600,height=400');
  if (printWindow) {
    printWindow.document.write(printContent);
    printWindow.document.close();
  } else {
    console.error('Failed to open print window - popup blocker may be enabled');
  }
}

/**
 * Show coming soon message for barcode features
 * @param {string} feature - Feature name
 */
export function showBarcodeComingSoon(feature = 'Barcode printing') {
  // This is a placeholder for when full barcode library is integrated
  console.log(`${feature} - Full barcode generation coming soon`);
}
