<?php

class RivieraBridge extends BridgeAbstract {
	const NAME = 'Sala La Riviera';
	const URI = 'https://salariviera.com/conciertossalariviera/';
	const DESCRIPTION = 'Concerts in Sala La Riviera, Madrid';
	const MAINTAINER = 'danoloan';
	const PARAMETERS = array(); // Can be omitted!
	const CACHE_TIMEOUT = 1;

	private function eventToItem($node) : Event {
		$event_date  = $node->find('.ecs-eventDate', 0);
		$event_time  = $node->find('.ecs-eventTime .decm_time', 0);
		$event_title = $node->find('.entry-title', 0);
		$event_status_wrapper = $node->find('.event-status-wrapper', 0);

		$date  = $event_date->plaintext;
		$time  = $event_time->plaintext;
		$title = $event_title->find('a', 0)->plaintext;
		$link  = $event_title->find('a', 0)->href;

		$time = str_replace(" ", "", $time);
		$date = str_replace("de ", "", $date);
		$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->uid = $link;
		$event->stamp = time();
		$event->summary = $title;

		if(!preg_match("/PM/", $time)) {
			$event->fill = true;
			$event->start = strtotime($date);
			$event->end = strtotime($date);
		} else {
			$event->fill = false;
			$event->start = strtotime($date." ".$time);
			$event->end = strtotime($date." ".$time) + (2*3600);
		}

		return $event;
	}

	private function getEventItems() : array {
		$events = array();

		$html = getSimpleHTMLDOM(self::URI);
		$month = '';

		foreach ($html->find('.ecs-event') as $node) {
			$event = $this->eventToItem($node);
			$events[] = $event;
		}

		return array_reverse($events);
	}

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