<?php
/** @var FreshRSS_View $this */
if (!Minz_Request::paramBoolean('ajax')) {
	$this->partial('aside_feed');
	$this->partial('nav_menu');
}

call_user_func($this->callbackBeforeEntries, $this);

$display_today = true;
$display_yesterday = true;
$display_others = true;
$hidePosts = !FreshRSS_Context::$user_conf->display_posts;
$lazyload = FreshRSS_Context::$user_conf->lazyload;
$content_width = FreshRSS_Context::$user_conf->content_width;
$MAX_TAGS_DISPLAYED = FreshRSS_Context::$user_conf->show_tags_max;

$today = @strtotime('today');
?>

<main id="stream" class="normal<?= $hidePosts ? ' hide_posts' : '' ?>">
	<h1 class="title_hidden"><?= _t('conf.reading.view.normal') ?></h1>
	<div id="new-article">
		<a href="<?= Minz_Url::display(Minz_Request::currentRequest()) ?>"><?= _t('gen.js.new_article'); /* TODO: move string in JS*/ ?></a>
	</div><?php
	$lastEntry = null;
	$nbEntries = 0;
	/** @var FreshRSS_Entry */
	foreach ($this->entries as $item):
		$lastEntry = $item;
		$nbEntries++;
		ob_flush();
		/** @var FreshRSS_Entry */
		$item = Minz_ExtensionManager::callHook('entry_before_display', $item);
		if ($item == null) {
			continue;
		}
		$this->entry = $item;

		// We most likely already have the feed object in cache
		$this->feed = FreshRSS_CategoryDAO::findFeed($this->categories, $this->entry->feedId());
		if ($this->feed == null) {
			$this->feed = $this->entry->feed() ?: null;
			if ($this->feed == null) {
				$this->feed = FreshRSS_Feed::example();
			}
		}

		if ($display_today && $this->entry->isDay(FreshRSS_Days::TODAY, $today)) {
			?><div class="day" id="day_today"><?php
				echo _t('gen.date.today');
				?><span class="date"> — <?= timestamptodate(time(), false) ?></span><?php
				?><span class="name"><?= FreshRSS_Context::$name ?></span><?php
			?></div><?php
			$display_today = false;
		}
		if ($display_yesterday && $this->entry->isDay(FreshRSS_Days::YESTERDAY, $today)) {
			?><div class="day" id="day_yesterday"><?php
				echo _t('gen.date.yesterday');
				?><span class="date"> — <?= timestamptodate(time() - 86400, false) ?></span><?php
				?><span class="name"><?= FreshRSS_Context::$name ?></span><?php
			?></div><?php
			$display_yesterday = false;
		}
		if ($display_others && $this->entry->isDay(FreshRSS_Days::BEFORE_YESTERDAY, $today)) {
			?><div class="day" id="day_before_yesterday"><?php
				echo _t('gen.date.before_yesterday');
				?><span class="name"><?= FreshRSS_Context::$name ?></span><?php
			?></div><?php
			$display_others = false;
		}
	?><div class="flux<?= !$this->entry->isRead() ? ' not_read' : ''
		?><?= $this->entry->isFavorite() ? ' favorite' : ''
		?>" id="flux_<?= $this->entry->id()
		?>" data-feed="<?= $this->feed->id()
		?>"><?php
			$this->renderHelper('index/normal/entry_header');

			$tags = null;
			$firstTags = array();
			$remainingTags = array();

			if (FreshRSS_Context::$user_conf->show_tags === 'h' || FreshRSS_Context::$user_conf->show_tags === 'f' || FreshRSS_Context::$user_conf->show_tags === 'b') {
				$tags = $this->entry->tags();
				if (!empty($tags)) {
					if ($MAX_TAGS_DISPLAYED > 0) {
						$firstTags = array_slice($tags, 0, $MAX_TAGS_DISPLAYED);
						$remainingTags = array_slice($tags, $MAX_TAGS_DISPLAYED);
					} else {
						$firstTags = $tags;
					}
				}
			}
		?><article class="flux_content" dir="auto">
			<div class="content <?= $content_width ?>">
				<header>
					<?php if (FreshRSS_Context::$user_conf->show_feed_name === 't') { ?>
						<div class="website"><a href="<?= _url('index', 'index', 'get', 'f_' . $this->feed->id()) ?>" title="<?= _t('gen.action.filter') ?>">
							<?php if (FreshRSS_Context::$user_conf->show_favicons): ?>
								<img class="favicon" src="<?= $this->feed->favicon() ?>" alt="✇" loading="lazy" /><?php
							endif; ?><span><?= $this->feed->name() ?></span></a>
						</div>
					<?php } ?>
					<?php if (FreshRSS_Context::$user_conf->show_tags === 'h' || FreshRSS_Context::$user_conf->show_tags === 'b') { ?>
						<div class="tags">
							<?php
							if (!empty($tags)) {
								?><?= _i('tag') ?><ul class="list-tags"><?php
								foreach ($firstTags as $tag) {
									?><li class="item tag"><a class="link-tag" href="<?= _url('index', 'index', 'search', '#' . str_replace(' ', '+', htmlspecialchars_decode($tag, ENT_QUOTES))) ?>" title="<?= _t('gen.action.filter') ?>">#<?= $tag ?></a></li><?php
								}

								if (!empty($remainingTags)) { // more than 7 tags: show dropdown menu ?>
									<li class="item tag">
										<div class="dropdown">
											<div id="dropdown-tags2-<?= $this->entry->id() ?>" class="dropdown-target"></div>
											<a class="dropdown-toggle" href="#dropdown-tags2-<?= $this->entry->id() ?>"><?= _i('down') ?></a>
											<ul class="dropdown-menu">
												<li class="dropdown-header"><?= _t('index.tag.related') ?></li>
												<?php
												foreach ($remainingTags as $tag) {
													?><li class="item"><a href="<?= _url('index', 'index', 'search', '#' . str_replace(' ', '+', htmlspecialchars_decode($tag, ENT_QUOTES))) ?>" title="<?= _t('gen.action.filter') ?>"><?= $tag ?></a></li><?php
												} ?>
											</ul>
											<a class="dropdown-close" href="#close">❌</a>
										</div>
									</li>
									<?php
								} ?>
								</ul><?php
							} ?>
						</div>
						<?php
					} ?>
					<h1 class="title"><a target="_blank" rel="noreferrer" class="go_website" href="<?= $this->entry->link() ?>" title="<?= _t('conf.shortcut.see_on_website')?>"><?= $this->entry->title() ?></a></h1>
					<?php if (FreshRSS_Context::$user_conf->show_author_date === 'h' || FreshRSS_Context::$user_conf->show_author_date === 'b') { ?>
					<div class="subtitle">
						<?php if (FreshRSS_Context::$user_conf->show_feed_name === 'a') { ?>
							<div class="website"><a href="<?= _url('index', 'index', 'get', 'f_' . $this->feed->id()) ?>" title="<?= _t('gen.action.filter') ?>">
								<?php if (FreshRSS_Context::$user_conf->show_favicons): ?>
									<img class="favicon" src="<?= $this->feed->favicon() ?>" alt="✇" loading="lazy" /><?php
								endif; ?><span><?= $this->feed->name() ?></span></a>
							</div>
							<?php
						}

						$authors = $this->entry->authors();
						if (!empty($authors) && is_array($authors)) {
						?>
						<div class="author">
							<?= _t('gen.short.by_author') ?>
							<?php
							foreach ($authors as $author) {
								$href = Minz_Url::display(Minz_Request::modifiedCurrentRequest(['search' => 'author:"' . htmlspecialchars_decode($author, ENT_QUOTES) . '"']));
							?>
							<a href="<?= $href ?>" title="<?= _t('gen.action.filter') ?>"><?= $author ?></a>
							<?php } ?>
						</div>
						<?php } ?>
						<div class="date"><?= $this->entry->date() ?></div>
					</div>
					<?php } ?>
				</header>
				<div class="text"><?php
					echo $lazyload && $hidePosts ? lazyimg($this->entry->content(true)) : $this->entry->content(true);
				?></div>
				<?php
				$display_authors_date = FreshRSS_Context::$user_conf->show_author_date === 'f' || FreshRSS_Context::$user_conf->show_author_date === 'b';
				$display_tags = FreshRSS_Context::$user_conf->show_tags === 'f' || FreshRSS_Context::$user_conf->show_tags === 'b';

				if ($display_authors_date || $display_tags) {
					?>
					<footer>
						<?php
						if ($display_authors_date) { ?>
							<div class="subtitle">
								<?php if (FreshRSS_Context::$user_conf->show_feed_name === 'a') { ?>
									<div class="website"><a href="<?= _url('index', 'index', 'get', 'f_' . $this->feed->id()) ?>" title="<?= _t('gen.action.filter') ?>">
										<?php if (FreshRSS_Context::$user_conf->show_favicons): ?>
											<img class="favicon" src="<?= $this->feed->favicon() ?>" alt="✇" loading="lazy" /><?php
										endif; ?><span><?= $this->feed->name() ?></span></a>
									</div>
									<?php
								}
								$authors = $this->entry->authors();
								if (!empty($authors) && is_array($authors)) {
								?>
								<div class="author">
									<?= _t('gen.short.by_author') ?>
									<?php
									foreach ($authors as $author) {
										$href = Minz_Url::display(Minz_Request::modifiedCurrentRequest(['search' => 'author:"' . htmlspecialchars_decode($author, ENT_QUOTES) . '"']));
									?>
									<a href="<?= $href ?>" title="<?= _t('gen.action.filter') ?>"><?= $author ?></a>
									<?php } ?>
								</div>
								<?php } ?>
								<div class="date"><?= $this->entry->date() ?></div>
							</div>
						<?php
						}
						if ($display_tags) { ?>
							<div class="tags">
								<?php
								if (!empty($tags)) {
									?><?= _i('tag') ?><ul class="list-tags"><?php
									foreach ($firstTags as $tag) {
										?><li class="item tag"><a class="link-tag" href="<?= _url('index', 'index', 'search', '#' . str_replace(' ', '+', htmlspecialchars_decode($tag, ENT_QUOTES))) ?>" title="<?= _t('gen.action.filter') ?>">#<?= $tag ?></a></li><?php
									}
									if (!empty($remainingTags)) { ?>
										<li class="item tag">
											<div class="dropdown">
												<div id="dropdown-tags3-<?= $this->entry->id() ?>" class="dropdown-target"></div>
												<a class="dropdown-toggle" href="#dropdown-tags3-<?= $this->entry->id() ?>"><?= _i('down') ?></a>
												<ul class="dropdown-menu">
													<li class="dropdown-header"><?= _t('index.tag.related') ?></li>
													<?php
													foreach ($remainingTags as $tag) {
														?><li class="item"><a href="<?= _url('index', 'index', 'search', '#' . str_replace(' ', '+', htmlspecialchars_decode($tag, ENT_QUOTES))) ?>" title="<?= _t('gen.action.filter') ?>"><?= $tag ?></a></li><?php
													} ?>
												</ul>
												<a class="dropdown-close" href="#close">❌</a>
											</div>
										</li>
										<?php
									} ?>
									</ul><?php
								} ?>
							</div>
							<?php
						} ?>
					</footer>
					<?php
				} ?>
			</div><footer><?php
				$this->renderHelper('index/normal/entry_bottom');
			?>
		</footer></article>
	</div><?php
	endforeach;

	if ($nbEntries > 0):
		call_user_func($this->callbackBeforePagination, $this, $nbEntries, $lastEntry);
		$this->renderHelper('stream-footer');
?>
</main><?php
	else:
		ob_end_clean();	//Discard the articles headers, as we have no articles
?>
<main id="stream" class="normal">
	<div id="new-article">
		<a href="<?= Minz_Url::display(Minz_Request::currentRequest()) ?>"><?= _t('gen.js.new_article'); /* TODO: move string in JS*/ ?></a>
	</div>
	<div class="prompt alert alert-warn">
		<h2 class="alert-head"><?= _t('index.feed.empty') ?></h2>
		<?php if (FreshRSS_Auth::hasAccess()) { ?>
			<p><a href="<?= _url('subscription', 'add') ?>"><?= _t('index.feed.add') ?></a></p>
		<?php } ?>
	</div>
</main>
<?php endif; ?>

<?php $class = $this->displaySlider ? ' active' : ''; ?>
<aside id="slider" class="<?= $class ?>">
	<a class="toggle_aside" href="#close"><img class="icon" src="../themes/icons/close.svg" loading="lazy" alt="❌"></a>
		<div id="slider-content">
		</div>
</aside>
<a href="#" id="close-slider" class="<?= $class ?>">
	<?= _i('close') ?>
</a>

<?php if ($nbEntries > 0 && FreshRSS_Context::$user_conf->show_nav_buttons) $this->partial('nav_entries'); ?>
