<?php namespace NicoSt\GCalendar\Components;

use Cms\Classes\ComponentBase;
use NicoSt\GCalendar\Classes\EventEntity;
use NicoSt\GCalendar\Classes\GoogleCalendarService;
use NicoSt\GCalendar\Models\Settings;

use Log;

class Upcoming extends ComponentBase {

    /**
     * Returns information about this component, including name and description.
     */
    public function componentDetails() {
        return [
            'name'        => 'nicost.gcalendar::lang.component.upcoming.name',
            'description' => 'nicost.gcalendar::lang.component.upcoming.description'
        ];
    }

    /*
    public function get() {

        $connector = new GoogleCalendarService();

        return $connector->calendarList();
    }
    */
    
    public function defineProperties() {
        return [
            'maxEvents' => [
                'title'             => 'Max Events',
                'description'       => 'The maximal amount of events which shall be displayed.',
                'default'           => 5,
                'type'              => 'string',
                'validationPattern' => '^[0-9]+$',
                'validationMessage' => 'The Max Events property can contain only numeric symbols'
            ]
        ];
    }

    public function events() {
        $connector = new GoogleCalendarService();
        $maxEvents = $this->property('maxEvents');
        $calendarEvents = $connector->allEventList($maxEvents);
        // Check for error to pass through
        if (isset($calendarEvents['error'])) {
            return $calendarEvents;
        }

        if (!is_array($calendarEvents) && !is_object($calendarEvents)) {
            return [];
        }
        $eventObjects = self::mapToEventDto($calendarEvents);
        
        // Sort events from all calendars
        usort($eventObjects, array("NicoSt\GCalendar\Components\Upcoming", "sortEventsByDate"));

        // Trim to maxEvents value and return
        return $output = array_slice($eventObjects, 0, $maxEvents);
    }

    private static function mapToEventDto($calendarEvents){
        $savedCalendars = Settings::get('calendar_selector');
        $eventObjects = [];
        foreach ($calendarEvents as $calendarEvent) {
            foreach ($calendarEvent as $event) {
                // Map to data response object.
                $eventObject = new EventEntity();
                $eventObject->setCalendarName($calendarEvent->summary);
                $eventObject->setEventName($event->summary);
                $eventObject->setLocation($event->location);
                $eventObject->setDescription($event->description);
                $eventObject->setStartTime($event->start->dateTime);
                $eventObject->setEndTime($event->end->dateTime);
                array_push($eventObjects, $eventObject);

                foreach($savedCalendars as $calendar) {
                    if($calendar['id'] == $event->organizer->email) {
                        $eventObject->setColor($calendar['color']);
                    }
                }
            }
        }
        return $eventObjects;
    }

    private function sortEventsByDate($a, $b) {
        if ($a->start_time == $b->start_time) {
            return 0;
        }
        return ($a->start_time < $b->start_time) ? -1 : 1;
    }

}