<?php

class PicaroBridge extends BridgeAbstract {
	const NAME = 'Sala Pícaro';
	const URI = 'https://sala.picarotoledo.com/agenda/';
	const DESCRIPTION = 'Concerts in Sala Pícaro, Toledo (Spain)';
	const MAINTAINER = 'danoloan';
	const PARAMETERS = array(); // Can be omitted!
	const CACHE_TIMEOUT = 1;

	private function eventToItem($node) : Event {
		$event_date  = $node->find('.mec-start-date-label', 0);
		$event_time  = $node->find('.mec-start-time', 0);
		$event_title = $node->find('.mec-event-title', 0);

		$date  = $event_date->innertext . ' ' . strftime("%Y");
		$time  = $event_time->innertext;
		$title = $event_title->find('a', 0)->innertext;
		$link  = $event_title->find('a', 0)->href;

		$date = str_replace("enero", "january", $date);
		$date = str_replace("febrero", "february", $date);
		$date = str_replace("marzo", "march", $date);
		$date = str_replace("abril", "april", $date);
		$date = str_replace("mayo", "may", $date);
		$date = str_replace("junio", "june", $date);
		$date = str_replace("julio", "july", $date);
		$date = str_replace("agosto", "august", $date);
		$date = str_replace("septiembre", "september", $date);
		$date = str_replace("octubre", "october", $date);
		$date = str_replace("noviembre", "november", $date);
		$date = str_replace("diciembre", "december", $date);

		$event = new \Event();
		$event->timezone = "Europe/Madrid";
		$event->uid = $link;
		$event->stamp = time();
		$event->start = strtotime($date) + strtotime($time, 0);
		$event->end = $event->start + 3600*2;
		$event->summary = $title;
		$event->fill = false;

		return $event;
	}

	private function getEventItems() : array {
		$page = 0;
		$events = array();
		$html = getSimpleHTMLDOM(self::URI);
		foreach ($html->find('.mec-event-article') as $node) {
			$event = $this->eventToItem($node);
			$events[] = $event;
		}
		return $events;
	}

	public function collectData() {
		$this->events = array_merge($this->items, $this->getEventItems());
	}
}
