<?php

function GetTextBetween(string &$Text, string $Start, string $End): string
{
  $Result = '';
  if (($Start == '') && ($End == ''))
  {
    $Result = $Text;
    $Text = '';
  } else
  if (($Start == '') && ($End != ''))
  {
    if (strpos($Text, $End) !== false)
    {
      $Result = substr($Text, 0, strpos($Text, $End));
      $Text = substr($Text, strpos($Text, $End) + strlen($End));
    }
  } else
  if (($Start != '') && ($End == ''))
  {
    if (strpos($Text, $Start) !== false)
    {
      $Result = substr($Text, strpos($Text, $Start) + strlen($Start));
      $Text = '';
    }
  } else
  if ((strpos($Text, $Start) !== false) and (strpos($Text, $End) !== false))
  {
    $Text = substr($Text, strpos($Text, $Start) + strlen($Start));
    $Result = substr($Text, 0, strpos($Text, $End));
    $Text = substr($Text, strpos($Text, $End) + strlen($End));
  }
  return $Result;
}

function HumanDateTimeToTime(string $DateTime): int
{
  if ($DateTime == '') return NULL;
  $DateTime = str_replace('. ', '.', $DateTime);
  $Parts = explode(' ', $DateTime);
  $DateParts = explode('.', $Parts[0]);
  if (count($Parts) > 1) {
    $TimeParts = explode(':', $Parts[1]);
    if (count($TimeParts) == 1) $TimeParts[1] = '0';
    if (count($TimeParts) == 2) $TimeParts[2] = '0';
  } else $TimeParts = array(0, 0, 0);
  $Result = mktime($TimeParts[0], $TimeParts[1], $TimeParts[2], $DateParts[1], $DateParts[0], $DateParts[2]);
  return $Result;
}

function HumanDateToTime(string $Date): int
{
  if ($Date == '') return NULL;
  return HumanDateTimeToTime($Date.' 0:0:0');
}

function GetUrlBase(string $Url): string
{
  $Result = parse_url($Url);
  return $Result['scheme']."://".$Result['host'];
}

class NewsSources
{
  public Database $Database;
  public array $Items;

  function Parse($Id = null)
  {
    $Output = '';
    $Where = '(Enabled=1)';
    if (($Id != null) and is_numeric($Id)) $Where .= ' AND (Id='.$Id.')';
    $DbResult = $this->Database->select('NewsImport', '*', $Where);
    while ($DbRow = $DbResult->fetch_assoc())
    {
      $Method = $DbRow['Method'];
      if ($Method == 'vismo') $Source = new NewsSourceVismo();
      else if ($Method == 'zdechovnet') $Source = new NewsSourceZdechovNET();
      else {
        $Output .= 'Unsupported parse method: '.$Method.'<br/>';
        continue;
      }
      $Source->Database = $this->Database;
      $Source->Id = $DbRow['Id'];
      $Source->URL = $DbRow['Source'];
      $Source->Method = $Method;
      $Source->Category = $DbRow['Category'];
      $Source->Name = $DbRow['Name'];
      $this->Items[] = $Source;
      $Output .= $Source->DoImport();
    }
    return $Output;
  }
}

class NewsSource
{
  public string $Name;
  public string $URL;
  public $Method;
  public int $Id;
  public Database $Database;
  public array $NewsItems;
  public int $AddedCount;
  public $Category;

  function __construct()
  {
    $this->NewsItems = array();
    $this->AddedCount = 0;
  }

  function Import(): string
  {
    return '';
  }

  function DoImport(): string
  {
    $this->NewsItems = array();
    $this->AddedCount = 0;
    $Output = 'Parsing '.$this->Name.' (#'.$this->Id.')...';
    $Output .= $this->Import();
    $Output .= ' parsed: '.count($this->NewsItems);
    foreach ($this->NewsItems as $NewsItem)
    {
      $this->AddedCount += $NewsItem->AddIfNotExist();
    }
    $Output .= ', new added: '.$this->AddedCount;
    $Output .= '</br>'."\n";
    return $Output;
  }
}

class NewsItem
{
  public Database $Database;
  public string $Title = '';
  public string $Content = '';
  public int $Date = 0;
  public string $Link = '';
  public string $Category = '';
  public string $Author = '';
  public string $IP = '';
  public string $Enclosure = '';

  function AddIfNotExist(): int
  {
    $Where = '(`Title` = "'.$this->Database->real_escape_string($this->Title).'") AND '.
      '(`Category` = "'.$this->Category.'") AND '.
      '(`Date` = "'.TimeToMysqlDateTime($this->Date).'")';
    $DbResult = $this->Database->select('News', '*', $Where);
    if ($DbResult->num_rows == 0)
    {
      $this->Database->insert('News', array(
        'Content' => $this->Content,
        'Date' => TimeToMysqlDateTime($this->Date),
        'Title' => $this->Title,
        'Link' => $this->Link,
        'Category' => $this->Category,
        'Author' => $this->Author,
        'IP' => $this->IP,
        'Enclosure' => $this->Enclosure,
      ));
      $Result = 1;
    } else $Result = 0;
    return $Result;
  }
}
