<?php
/**
 * @copyright Copyright (c) 2020, Matias De lellis <mati86dl@gmail.com>
 *
 * @author Matias De lellis <mati86dl@gmail.com>
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
namespace OCA\FaceRecognition\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

use OCP\IUser;
use OCP\IUserManager;

use OCA\FaceRecognition\Db\ImageMapper;
use OCA\FaceRecognition\Db\FaceMapper;
use OCA\FaceRecognition\Db\PersonMapper;

use OCA\FaceRecognition\Service\SettingsService;

class DumpCommand extends Command {

	/** @var IUserManager */
	protected $userManager;

	/** @var PersonMapper */
	protected $personMapper;

	/** @var FaceMapper */
	protected $faceMapper;

	/** @var SettingsService */
	private $settingsService;

	/**
	 * @param IUserManager $userManager
	 * @param ImageMapper $imageMapper
	 * @param FaceMapper $faceMapper
	 * @param PersonMapper $personMapper
	 * @param SettingsService $settingsService
	 */
	public function __construct(IUserManager    $userManager,
	                            PersonMapper    $personMapper,
	                            FaceMapper      $faceMapper,
	                            SettingsService $settingsService)
	{
		parent::__construct();

		$this->userManager     = $userManager;
		$this->faceMapper      = $faceMapper;
		$this->personMapper    = $personMapper;
		$this->settingsService = $settingsService;
	}

	/**
	 * @return void
	 */
	protected function configure() {
		$this
			->setName('face:dump')
			->setDescription('Get a summary of statistics images, faces and persons')
			->addOption(
				'user_id',
				'u',
				InputOption::VALUE_REQUIRED,
				'Get stats for a given user only. If not given, get stats for all users.',
				null
			);
	}

	/**
	 * @param InputInterface $input
	 * @param OutputInterface $output
	 * @return int
	 */
	protected function execute(InputInterface $input, OutputInterface $output) {
		$users = array();

		$userId = $input->getOption('user_id');
		if (!is_null($userId)) {
			if ($this->userManager->get($userId) === null) {
				$output->writeln("User with id <$userId> in unknown.");
				return 1;
			}
			else {
				$users[] = $userId;
			}
		}
		else {
			$this->userManager->callForAllUsers(function (IUser $iUser) use (&$users)  {
				$users[] = $iUser->getUID();
			});
		}

		$this->printCSVStats($output, $users);

		return 0;
	}

	private function printCSVStats(OutputInterface $output, array $users): void {
		$modelId = $this->settingsService->getCurrentFaceModel();

		foreach ($users as $userId) {
			$clusters = $this->personMapper->findAll($userId, $modelId);
			$output->writeln('id,size');
			foreach ($clusters as $cluster) {
				$faces = $this->faceMapper->findFromCluster($userId, $cluster->getId(), $modelId);
				$output->writeln($cluster->getId().','.count($faces));
			}
		}
	}
}
