<?php
namespace Misuzu\Http\Handlers;

use ErrorException;
use Misuzu\Config;
use Misuzu\Config\CfgType;
use Misuzu\Pagination;
use Misuzu\Template;
use Misuzu\Changelog\ChangelogChange;
use Misuzu\Changelog\ChangelogChangeNotFoundException;
use Misuzu\Changelog\ChangelogTag;
use Misuzu\Changelog\ChangelogTagNotFoundException;
use Misuzu\Feeds\Feed;
use Misuzu\Feeds\FeedItem;
use Misuzu\Feeds\AtomFeedSerializer;
use Misuzu\Feeds\RssFeedSerializer;
use Misuzu\Users\User;
use Misuzu\Users\UserNotFoundException;

class ChangelogHandler extends Handler {
    public function index($response, $request) {
        $filterDate = $request->getParam('date');
        $filterUser = $request->getParam('user', FILTER_SANITIZE_NUMBER_INT);
        //$filterTags = $request->getParam('tags');

        if($filterDate !== null)
            try {
                $dateParts = explode('-', $filterDate, 3);
                $filterDate = gmmktime(12, 0, 0, (int)$dateParts[1], (int)$dateParts[2], (int)$dateParts[0]);
            } catch(ErrorException $ex) {
                return 404;
            }

        if($filterUser !== null)
            try {
                $filterUser = User::byId($filterUser);
            } catch(UserNotFoundException $ex) {
                return 404;
            }

        /*if($filterTags !== null) {
            $splitTags = explode(',', $filterTags);
            $filterTags = [];
            for($i = 0; $i < min(10, count($splitTags)); ++$i)
                try {
                    $filterTags[] = ChangelogTag::byId($splitTags[$i]);
                } catch(ChangelogTagNotFoundException $ex) {
                    return 404;
                }
        }*/

        $count = $filterDate !== null ? -1 : ChangelogChange::countAll($filterDate, $filterUser);
        $pagination = new Pagination($count, 30);
        if(!$pagination->hasValidOffset())
            return 404;

        $changes = ChangelogChange::all($pagination, $filterDate, $filterUser);
        if(empty($changes))
            return 404;

        $response->setContent(Template::renderRaw('changelog.index', [
            'changelog_infos' => $changes,
            'changelog_date' => $filterDate,
            'changelog_user' => $filterUser,
            'changelog_pagination' => $pagination,
            'comments_user' => User::getCurrent(),
        ]));
    }

    public function change($response, $request, int $changeId) {
        try {
            $changeInfo = ChangelogChange::byId($changeId);
        } catch(ChangelogChangeNotFoundException $ex) {
            return 404;
        }

        $response->setContent(Template::renderRaw('changelog.change', [
            'change_info' => $changeInfo,
            'comments_user' => User::getCurrent(),
        ]));
    }

    private function createFeed(string $feedMode): Feed {
        $changes = ChangelogChange::all(new Pagination(10));

        $feed = (new Feed)
            ->setTitle(Config::get('site.name', CfgType::T_STR, 'Misuzu') . ' » Changelog')
            ->setDescription('Live feed of changes to ' . Config::get('site.name', CfgType::T_STR, 'Misuzu') . '.')
            ->setContentUrl(url_prefix(false) . url('changelog-index'))
            ->setFeedUrl(url_prefix(false) . url("changelog-feed-{$feedMode}"));

        foreach($changes as $change) {
            $changeUrl = url_prefix(false) . url('changelog-change', ['change' => $change->getId()]);
            $commentsUrl = url_prefix(false) . url('changelog-change-comments', ['change' => $change->getId()]);

            $feedItem = (new FeedItem)
                ->setTitle($change->getActionString() . ': ' . $change->getHeader())
                ->setCreationDate($change->getCreatedTime())
                ->setUniqueId($changeUrl)
                ->setContentUrl($changeUrl)
                ->setCommentsUrl($commentsUrl);

            $feed->addItem($feedItem);
        }

        return $feed;
    }

    public function feedAtom($response, $request) {
        $response->setContentType('application/atom+xml; charset=utf-8');
        return (new AtomFeedSerializer)->serializeFeed(self::createFeed('atom'));
    }

    public function feedRss($response, $request) {
        $response->setContentType('application/rss+xml; charset=utf-8');
        return (new RssFeedSerializer)->serializeFeed(self::createFeed('rss'));
    }
}
