<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */


class PDFLogo
{
  
  public static function getLogoFilePath(array $pLogoFileNames = array()): string
  {
    $imagePaths = array(
      \FSOperations::path(BASEDIR, 'webtools', 'layout', 'logos') . DIRECTORY_SEPARATOR,
      \FSOperations::path(\ConstantOperations::getPathForCoreSettings()) . DIRECTORY_SEPARATOR
    );
    $result = '';

    $fileNamesToCheck = array_merge($pLogoFileNames, array('standard_logo.gif', 'standard_logo.jpg', 'standard_logo.png'));

    foreach ($imagePaths as $imagePath) {
      foreach ($fileNamesToCheck as $fileName) {
        if (is_file($imagePath . $fileName)) {
          $result = $imagePath . $fileName;
          break;
        }
      }
    }

    return $result;
  }

  
  public static function printLogoImg($pPdf, $pLogoFilePath, $pIsPositionLeft = false, $pShiftDown = true, $pXPos = -1, $pYPos = -1)
  {
    
    $px_faktor4mm = 0.3553;
    if (!empty($pLogoFilePath)) {
      $imageSize = getimagesize($pLogoFilePath);
      $currentImageWidth = $imageSize[0];
      $currentImageHeight = $imageSize[1];

      
      
      
      
      $toolSettings0 = \ToolSettingsAPI::getToolSettings(0);
      $preferredImageSize = $toolSettings0->pdflisteLogoWidth;
      if ($preferredImageSize <= 0) {
        $preferredImageSize = 150;
      }
      if ($currentImageWidth > $preferredImageSize) {
        $imageWidth = $preferredImageSize * $px_faktor4mm;
        $imageHeight = $preferredImageSize / $currentImageWidth * $currentImageHeight * $px_faktor4mm;
      } else {
        
        
        $imageHeight = $currentImageHeight * $px_faktor4mm;
        $imageWidth = $currentImageWidth * $px_faktor4mm;
      }

      
      $imageWidthRel = $imageWidth * 100 / $pPdf->GetPageWidth();

      
      if (!$pIsPositionLeft) {
        
        $img_xpos = 100 - ($imageWidthRel + 5);
        $img_xpos = (string)$img_xpos . '%';
      } else {
        
        if ($pXPos == -1) {
          $img_xpos = '5%';
        } else {
          $img_xpos = $pXPos . '%';
        }
      }
      
      if ($pYPos == -1) {
        $img_ypos = '5%';
      } else {
        $img_ypos = $pYPos . '%';
      }

      $pPdf->Image($pLogoFilePath, $img_xpos, $img_ypos, $imageWidth);
      
      
      if ($pShiftDown) {
        $pPdf->ln($imageHeight);
      }

      return true;
    }

    return false;
  }
}