<?php

class EventSourceJoeClub extends EventSource
{
  function ImportInternal(): string
  {
    $Output = '';
    $Content = file_get_contents($this->URL);

    $BlockStart = '<div class="ai1ec-date-events">';
    $BlockEnd = '<div class="ai1ec-pull-left"><div class="ai1ec-pagination ai1ec-btn-group">';
    $Content = GetTextBetween($Content, $BlockStart, $BlockEnd);
    if ($Content == '')
    {
      $Output .= 'Main block not isolated.</br>';
      return $Output;
    }

    $ItemStart = '<div class="ai1ec-event-header">';
    $ItemEnd = 'Číst více <i class="';
    while (strpos($Content, $ItemStart) !== false)
    {
      $Item = GetTextBetween($Content, $ItemStart, $ItemEnd);
      $Event = new Event();

      $Event->Title = trim(html_entity_decode(strip_tags(GetTextBetween($Item, '<span class="ai1ec-event-title">', '<div class="ai1ec-sas-actions'))));
      $Event->Title = ReduceSpaces(RemoveTabs(RemoveLines($Event->Title)));
      if (($Event->Title == 'MÁME ZAVŘENO') or ($Event->Title == 'PRIVATE PARTY v Klub Joe')) continue;
      //$Event->TimeFrom = $this->ParseTime(trim(html_entity_decode(strip_tags(GetTextBetween($Item, '<div class="ai1ec-event-time">', '</div>')))));
      $Event->Description = GetTextBetween($Item, '<div class="ai1ec-event-description">', '<div class="ai1ec-event-summary-footer">');
      $Event->Description = str_replace('>Uložit<', '><', $Event->Description);
      $Event->Description = trim(html_entity_decode(strip_tags($Event->Description)));
      $Event->Link = trim(html_entity_decode(GetTextBetween($Item, 'href="', '"')));
      $Event->Location = 'Praha';

      $Event->Database = $this->Database;
      $Event->Source = $this->Id;
      $Output .= $this->ImportItem($Event);
      $this->AddedCount += $Event->AddIfNotExist();
    }
    return $Output;
  }

  function ImportItem(&$Event)
  {
    $Output = '';
    if ($Event->Link == '') return $Output;
    $Content = file_get_contents($Event->Link);

    $BlockStart = '<header class="entry-header">';
    $BlockEnd = '<footer class="entry-footer">';
    $Content = GetTextBetween($Content, $BlockStart, $BlockEnd);
    if ($Content == '')
    {
      $Output .= 'Main block not isolated.</br>';
      return;
    }

    $Event->TimeFrom = strtotime(trim(GetTextBetween($Content, 'ai1ec-hidden dt-start">', '</div>')));
    $Event->TimeTo = strtotime(trim(GetTextBetween($Content, 'ai1ec-hidden dt-end">', '</div>')));
    $Event->Price = trim(strip_tags(GetTextBetween($Content, 'Náklady:</div>', '</div')));
    $Event->Price = trim(str_replace('Kč', '', $Event->Price));
    if ($Event->Price == 'Zdarma') $Event->Price = 0;
    if ($Event->Price == '') $Event->Price = 0;
    return $Output;
  }

  function ParseTime($Time)
  {
    // Strip out time range
    if (strpos($Time, '–') !== false)
      $Time = substr($Time, 0, strpos($Time, '–'));

    if ($Time == '') return NULL;
    $Time = str_replace('@', '', $Time);

    $Parts = explode(' ', $Time);
    $Month = $Parts[0];
    $Day = $Parts[1];
    $Year =

    $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 $Time;
  }
}
