<?php

/**
 * @package Dotclear
 * @subpackage Backend
 *
 * @copyright Olivier Meunier & Association Dotclear
 * @copyright AGPL-3.0
 */
declare(strict_types=1);

namespace Dotclear\Core\Backend;

use Dotclear\Core\Backend\Listing\ListingBlogs;
use Dotclear\Core\Backend\Listing\ListingComments;
use Dotclear\Core\Backend\Listing\ListingMedia;
use Dotclear\Core\Backend\Listing\ListingPosts;
use Dotclear\Core\Backend\Listing\ListingPostsMini;
use Dotclear\Core\Backend\Listing\ListingUsers;
use Dotclear\Core\Backend\Listing\Pager;
use Dotclear\Database\MetaRecord;
use Dotclear\Helper\Container\Container;
use Dotclear\Helper\Container\Factory;

/**
 * @brief   Admin list helpers library
 *
 * @since   2.36
 */
class Listing extends Container
{
    /**
     * @var string CONTAINER_ID
     */
    public const CONTAINER_ID = 'backendlisting';

    public function __construct()
    {
        // Create a non replaceable factory
        parent::__construct(new Factory(static::CONTAINER_ID, false));
    }

    public function getDefaultServices(): array
    {
        return [
            ListingBlogs::class     => ListingBlogs::class,
            ListingComments::class  => ListingComments::class,
            ListingMedia::class     => ListingMedia::class,
            ListingPosts::class     => ListingPosts::class,
            ListingPostsMini::class => ListingPostsMini::class,
            ListingUsers::class     => ListingUsers::class,
            Pager::class            => Pager::class,
        ];
    }

    /**
     * Get backend blogs listing helper instance.
     *
     * New instance is returned on each call.
     */
    public function blogs(MetaRecord $rs, int $rs_count): ListingBlogs
    {
        // @phpstan-ignore return.type
        return $this->get(ListingBlogs::class, true, rs: $rs, rs_count: $rs_count);
    }

    /**
     * Get backend comments listing helper instance.
     *
     * New instance is returned on each call.
     */
    public function comments(MetaRecord $rs, int $rs_count): ListingComments
    {
        // @phpstan-ignore return.type
        return $this->get(ListingComments::class, true, rs: $rs, rs_count: $rs_count);
    }

    /**
     * Get backend media listing helper instance.
     *
     * New instance is returned on each call.
     */
    public function media(MetaRecord $rs, int $rs_count): ListingMedia
    {
        // @phpstan-ignore return.type
        return $this->get(ListingMedia::class, true, rs: $rs, rs_count: $rs_count);
    }

    /**
     * Get backend posts listing helper instance.
     *
     * New instance is returned on each call.
     */
    public function posts(MetaRecord $rs, int $rs_count): ListingPosts
    {
        // @phpstan-ignore return.type
        return $this->get(ListingPosts::class, true, rs: $rs, rs_count: $rs_count);
    }

    /**
     * Get backend posts mini listing helper instance.
     *
     * New instance is returned on each call.
     */
    public function postsMini(MetaRecord $rs, int $rs_count): ListingPostsMini
    {
        // @phpstan-ignore return.type
        return $this->get(ListingPostsMini::class, true, rs: $rs, rs_count: $rs_count);
    }

    /**
     * Get backend users listing helper instance.
     *
     * New instance is returned on each call.
     */
    public function users(MetaRecord $rs, int $rs_count): ListingUsers
    {
        // @phpstan-ignore return.type
        return $this->get(ListingUsers::class, true, rs: $rs, rs_count: $rs_count);
    }

    /**
     * Ge backend pager instance.
     *
     * New instance is returned on each call.
     */
    public function pager(int $current_page, int $nb_elements, int $nb_per_page = 10, int $nb_pages_per_group = 10): Pager
    {
        // @phpstan-ignore return.type
        return $this->get(
            Pager::class, // service
            true,         // reload
            current_page: $current_page,
            nb_elements: $nb_elements,
            nb_per_page: $nb_per_page,
            nb_pages_per_group: $nb_pages_per_group
        );
    }
}
