<?php

include_once(dirname(__FILE__).'/Import/JoeClub.php');

function GetDefaultEventFilter(string $Table = ''): string
{
  global $Config;

  if ($Table != '') $Table = $Table.'.';

  return '('.$Table.'Hidden=0) AND ('.$Table.'TimeFrom > "'.TimeToMysqlDateTime(time() - (int)$Config['EventInterval']).'")';
}

function ReduceSpaces(string $Content): string
{
  while (strpos($Content, '  ') !== false) $Content = str_replace('  ', ' ', $Content);
  return $Content;
}

function RemoveLines(string $Content): string
{
  while (strpos($Content, "\n") !== false) $Content = str_replace("\n", ' ', $Content);
  return $Content;
}

function RemoveTabs(string $Content): string
{
  while (strpos($Content, "\t") !== false) $Content = str_replace("\t", '', $Content);
  return $Content;
}

class EventSources
{
  public $Database;

  function Parse(int $Id = null): string
  {
    $Output = '';
    if (($Id != null) and is_numeric($Id)) $Where = 'Id='.$Id;
      else $Where = '1';
    $DbResult = $this->Database->select('EventSource', '*', $Where);
    while ($DbRow = $DbResult->fetch_assoc())
    {
      $Method = $DbRow['Method'];
      if ($Method == 'joe') $Source = new EventSourceJoeClub();
      else {
        $Output .= 'Unsupported parse method: '.$Method.'<br/>';
        continue;
      }
      $Source->Database = $this->Database;
      $Source->Id = $DbRow['Id'];
      $Source->URL = $DbRow['URL'];
      $Source->Method = $Method;
      $Source->Name = $DbRow['Name'];
      $this->Items[] = $Source;
      $Output .= $Source->Import();
    }
    return $Output;
  }
}

class EventSource
{
  public $Name;
  public $URL;
  public $Method;
  public $Id;
  public $Database;
  public $AddedCount;

  function ImportInternal()
  {
  }

  function Import(): string
  {
    $this->AddedCount = 0;
    $Output = 'Parsing '.$this->Name.' ('.$this->Id.')...';
    $this->ImportInternal();
    $Output .= 'done.';
    if ($this->AddedCount > 0) $Output .= ' '.$this->AddedCount.' new.';
    $Output .= '</br>'."\n";
    return $Output;
  }
}

class Event
{
  var $Database;
  var $Title = '';
  var $Description = '';
  var $TimeFrom = '';
  var $TimeTo = '';
  var $Source = 0;
  var $Location = '';
  var $Image = '';
  var $Link = '';
  var $RemoteId = '';
  var $Price = 0;

  function AddIfNotExist(int $TimeInterval = 0, bool $CompareTime = true, bool $CompareRemoteId = false): int
  {
    $Where = '(`Description` = "'.$this->Database->real_escape_string($this->Description).'") AND '.
      '(`Title` = "'.$this->Database->real_escape_string($this->Title).'") AND '.
      '(`Source` = '.$this->Source.')';
    if ($CompareTime)
      $Where .= ' AND (`TimeFrom` >= "'.$this->Database->real_escape_string(TimeToMysqlDateTime($this->TimeFrom - $TimeInterval)).'") AND '.
      '(`TimeFrom` <= "'.$this->Database->real_escape_string(TimeToMysqlDateTime($this->TimeFrom + $TimeInterval)).'")';
    if ($CompareRemoteId)
      $Where .= ' AND (`RemoteId` = "'.$this->Database->real_escape_string($this->RemoteId).'")';
    $DbResult = $this->Database->select('Event', '*', $Where);
    if ($DbResult->num_rows == 0)
    {
      $this->Database->insert('Event', array(
        'Title' => $this->Title,
        'Description' => $this->Description,
        'TimeFrom' => TimeToMysqlDateTime($this->TimeFrom),
        'TimeTo' => TimeToMysqlDateTime($this->TimeTo),
        'Location' => $this->Location,
        'Source' => $this->Source,
        'Link' => $this->Link,
        'Price' => $this->Price,
        'RemoteId' => $this->RemoteId,
        'TimeImport' => 'NOW()',
      ));
      $Result = 1;
    } else $Result = 0;
    return $Result;
  }
}
