<?php

namespace XF\Service\Unfurl;

use XF\App;
use XF\Entity\UnfurlResult;
use XF\Repository\UnfurlRepository;
use XF\Service\AbstractService;

class FetcherService extends AbstractService
{
	protected $result;

	public function __construct(App $app, UnfurlResult $result)
	{
		parent::__construct($app);
		$this->result = $result;
	}

	public function fetch(&$error = null)
	{
		$result = $this->result;

		$metadata = $this->app->http()->metadataFetcher()->fetch($result->url, $error);

		if (!$metadata)
		{
			$this->logError();
			return false;
		}

		$title = $metadata->getTitle();

		if (!$title)
		{
			// if no title, might as well bail out
			$error = 'Could not fetch title metadata from URL.';
			$this->logError();
			return false;
		}

		$unfurlData = [
			'title' => $title,
			'description' => $metadata->getDescription(),
			'image_url' => $metadata->getImage(),
			'favicon_url' => $metadata->getFavicon(),
			'last_request_date' => \XF::$time,
			'pending' => 0,
		];
		$result->bulkSet(
			$this->prepareUnfurlData($unfurlData)
		);

		if (!$result->preSave())
		{
			$error = 'The metadata could not be saved successfully.';
			$result->reset();
			$this->logError();
			return false;
		}

		$result->save();

		return true;
	}

	public function render()
	{
		return $this->app->templater()->renderUnfurl($this->result);
	}

	protected function logError()
	{
		return $this->repository(UnfurlRepository::class)->logError($this->result);
	}

	protected function prepareUnfurlData(array $unfurlData)
	{
		return $unfurlData;
	}
}
