<?php namespace Qcr\QcrTheme\Http\Controller;

use Anomaly\PostsModule\Post\Contract\PostInterface;
use Anomaly\PostsModule\Post\Contract\PostRepositoryInterface;
use Anomaly\Streams\Platform\Http\Controller\PublicController;
use Anomaly\Streams\Platform\Stream\Contract\StreamRepositoryInterface;
use Carbon\Carbon;
use Eluceo\iCal\Component\Calendar;
use Eluceo\iCal\Component\Event;

/**
 * Class IcsController
 *
 * @link   http://pyrocms.com/
 * @author PyroCMS, Inc. <support@pyrocms.com>
 * @author Ryan Thompson <ryan@pyrocms.com>
 */
class IcsController extends PublicController
{

    /**
     * Download an ICS for an events post.
     *
     * @param PostRepositoryInterface $posts
     * @param                         $id
     */
    public function post(PostRepositoryInterface $posts, $id)
    {
        /* @var PostInterface $post */
        if (!$post = $posts->find($id)) {
            abort(404);
        }

        if (!$post->isLive()) {
            abort(400);
        }

        if ($post->type->slug !== 'events') {
            abort(400);
        }

        $event = new Event('post-' . $post->getStrId());

        $event->setSummary($post->getTitle());
        $event->setDescription($post->getMetaDescription());
        $event->setTimezoneString(config('app.timezone'));

        if (!$post->entry->time_start) {
            $event->setDtStart($post->entry->date_start);
            $event->setDtEnd($post->entry->date_end);
            $event->setNoTime(true);
        } else {

            /* @var Carbon $start */
            /* @var Carbon $end */
            $start = $post->entry->date_start;
            $end   = $post->entry->date_end;

            $event->setDtStart(
                $start->setTime(
                    $post->entry->time_start->hour,
                    $post->entry->time_start->minute,
                    0
                )
            );
            $event->setDtEnd(
                $end->setTime(
                    $post->entry->time_end->hour,
                    $post->entry->time_end->minute,
                    0
                )
            );
            $event->setNoTime(false);
        }

        $calendar = new Calendar($this->request->getHost());

        $calendar->addComponent($event);

        header('Content-Type: text/calendar; charset=utf-8');
        header('Content-Disposition: attachment; filename="' . $post->getSlug() . '.ics"');

        echo $calendar->render();
    }

    /**
     * Download an ICS for a select 50 event.
     *
     * @param StreamRepositoryInterface $streams
     * @param                           $id
     */
    public function events(StreamRepositoryInterface $streams, $id)
    {
        $model = $streams->findBySlugAndNamespace('events', 'events')->getEntryModel();

        /* @var PostInterface $post */
        if (!$entry = $model->find($id)) {
            abort(404);
        }

        $event = new Event('event-' . $entry->getId());

        $event->setSummary($entry->title);
        $event->setDescription($entry->description);
        $event->setTimezoneString(config('app.timezone'));

        if (!$entry->time_start) {
            $event->setDtStart($entry->date_start);
            $event->setDtEnd($entry->date_end);
            $event->setNoTime(true);
        } else {

            /* @var Carbon $start */
            /* @var Carbon $end */
            $start = $entry->date_start;
            $end   = $entry->date_end;

            $event->setDtStart(
                $start->setTime(
                    $entry->time_start->hour,
                    $entry->time_start->minute,
                    0
                )
            );
            $event->setDtEnd(
                $end->setTime(
                    $entry->time_end->hour,
                    $entry->time_end->minute,
                    0
                )
            );
            $event->setNoTime(false);
        }

        $calendar = new Calendar($this->request->getHost());

        $calendar->addComponent($event);

        header('Content-Type: text/calendar; charset=utf-8');
        header('Content-Disposition: attachment; filename="' . str_slug($entry->title) . '.ics"');

        echo $calendar->render();
    }
}
