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


class SightsAPI
{
  
  public static function &_getInstance()
  {
    static $instance;
    if (!isset($instance)) {
      if (SIGHTS_LOADFROMFILE) {
        $instance = new FileWorkSights();
      } else {
        $instance = new DBWorkSights(1, true);
      }
    }

    return $instance;
  }

  
  public static function getSight($pSightId)
  {
    $work = &SightsAPI::_getInstance();

    return $work->getSight($pSightId);
  }

  
  public static function setSight($pSight)
  {
    $work = &SightsAPI::_getInstance();
    $work->setSight($pSight);
  }

  
  public static function getSights($pToolId)
  {
    $work = &SightsAPI::_getInstance();

    return $work->getSights($pToolId);
  }

  
  public static function getNewObject()
  {
    $work = &SightsAPI::_getInstance();

    return $work->getNewObject();
  }

  
  public static function backupSights($pDir, $pOverwrite = false, $pTools = false)
  {
    $work = &SightsAPI::_getInstance();
    $tools = (!$pTools) ? $work->getToolIds() : $pTools;
    if (is_dir($pDir) && !$pOverwrite) {
      throw new Exception("Verzeichnis '{$pDir}' existiert bereits!");
    } elseif (!is_dir($pDir)) {
      mkdir($pDir);
    }

    if (is_writable($pDir)) {
      $sights = array();
      foreach ($tools as $tool) {
        $sights = array_merge(SightsAPI::getSights($tool), $sights);
      }

      $f = new SimpleFileWork();
      foreach ($sights as $sight) {
        $work = new FileWorkSights();
        $filecontent = $work->getFileContentFromSight($sight);

        $f->filename = $pDir . SIGHTS_PREFIX . $sight->toolid . '-' . $sight->id . '.sgt';
        $f->access = 'w';
        $f->openFile();
        $f->content = $filecontent;
        $f->setContent();
        $f->closeFile();
      }
    } else {
      throw new Exception("Verzeichnis '{$pDir}' nicht schreibbar!");
    }
  }

  
  public static function exportSights()
  {
    $work = new FileWorkSights();

    $dir = $work->getFilePath();

    if (!is_dir($dir)) {
      mkdir($dir);
    }
    if (is_writable($dir)) {
      $db = new DBWorkSights(1, true);

      $sights = array();
      foreach ($db->getToolIds() as $tool) {
        $sights = array_merge($db->getSights($tool), $sights);
      }

      foreach ($sights as $sight) {
        
        touch($work->getFileNameBySightId($sight->id, $dir));
        $work->setSight($sight);
      }
    } else {
      throw new Exception("Verzeichnis '{$dir}' nicht schreibbar!");
    }
  }

  
  public static function getSightPart(
    int $sightId,
    string $sightpart,
    array $data,
    ?\Exportadapter $exportAdapter = null
  ): string {
    if ($exportAdapter == null) {
      $exportAdapter = new ExpBasis();
    }
    $sight = self::getSight($sightId);
    $exportAdapter->export->initiate();
    $exportAdapter->export->exportresults = $data;
    $exportAdapter->export->setNrOfMainBodyElements(count($data));
    switch ($sightpart) {
      case WB_SIGHT_PART_HEADER:
        $exportAdapter->filestream = $sight->header;
        break;
      case WB_SIGHT_PART_BODY:
        $exportAdapter->filestream = $sight->body;
        break;
      case WB_SIGHT_PART_FOOTER:
        $exportAdapter->filestream = $sight->footer;
        break;
      case WB_SIGHT_PART_NOCOLUMN:
        $exportAdapter->filestream = $sight->nocolumn;
        break;
      case WB_SIGHT_PART_NODATA:
        $exportAdapter->filestream = $sight->nodata;
        break;
    }
    $exportAdapter->replaceVarNames();
    if (count($exportAdapter->resultlist) > 0) {
      return implode("\n", $exportAdapter->resultlist);
    } else {
      return '';
    }
  }
}