<?php

namespace XF\Widget;

use XF\Entity\Thread;
use XF\Http\Request;
use XF\Repository\NodeRepository;
use XF\Repository\ThreadRepository;

use function in_array;

class NewThreads extends AbstractWidget
{
	protected $defaultOptions = [
		'limit' => 5,
		'node_ids' => '',
		'date_limit_days' => 0,
		'style' => 'simple',
		'show_expanded_title' => false,
	];

	protected function getDefaultTemplateParams($context)
	{
		$params = parent::getDefaultTemplateParams($context);
		if ($context == 'options')
		{
			$nodeRepo = $this->app->repository(NodeRepository::class);
			$params['nodeTree'] = $nodeRepo->createNodeTree($nodeRepo->getFullNodeList());
		}
		return $params;
	}

	public function render()
	{
		$visitor = \XF::visitor();

		$options = $this->options;
		$limit = $options['limit'];
		$style = $options['style'];
		$nodeIds = $options['node_ids'];
		$dateLimit = $options['date_limit_days'] ?? 0;

		$router = $this->app->router('public');

		$threadRepo = $this->repository(ThreadRepository::class);

		$threadFinder = $threadRepo->findLatestThreads();
		$title = \XF::phrase('latest_threads');
		$link = $router->buildLink('whats-new');

		$threadFinder
			->with('Forum.Node.Permissions|' . $visitor->permission_combination_id)
			->limit(max($limit * 4, 20));

		if ($nodeIds && !in_array(0, $nodeIds))
		{
			$threadFinder->where('node_id', $nodeIds);
		}

		if ($dateLimit > 0)
		{
			$threadFinder->where('post_date', '>=', \XF::$time - ($dateLimit * 86400));
		}

		if ($style == 'full')
		{
			$threadFinder->with('fullForum');
		}
		if ($style == 'expanded')
		{
			$threadFinder->with('FirstPost');
		}

		/** @var Thread $thread */
		foreach ($threads = $threadFinder->fetch() AS $threadId => $thread)
		{
			if (!$thread->canView()
				|| $thread->isIgnored()
			)
			{
				unset($threads[$threadId]);
			}

			if ($options['style'] != 'expanded' && $visitor->isIgnoring($thread->last_post_user_id))
			{
				unset($threads[$threadId]);
			}
		}
		$threads = $threads->slice(0, $limit, true);

		$viewParams = [
			'title' => $this->getTitle() ?: $title,
			'link' => $link,
			'threads' => $threads,
			'style' => $options['style'],
			'showExpandedTitle' => $options['show_expanded_title'],
		];
		return $this->renderer('widget_new_threads', $viewParams);
	}

	public function verifyOptions(Request $request, array &$options, &$error = null)
	{
		$options = $request->filter([
			'limit' => 'uint',
			'style' => 'str',
			'node_ids' => 'array-uint',
			'show_expanded_title' => 'bool',
			'date_limit_days' => 'uint',
		]);

		if (in_array(0, $options['node_ids']))
		{
			$options['node_ids'] = [0];
		}

		if ($options['limit'] < 1)
		{
			$options['limit'] = 1;
		}

		if ($options['style'] != 'expanded')
		{
			$options['show_expanded_title'] = false;
		}

		return true;
	}
}
