<?php

namespace Modules\Reception\Outil;

use Modules\Reception\Outil\DatatableFilterSearch;

// TODO: move into Helper
abstract class Datatable
{
    /**
     * @var array<mixed>
     */
    private array $columnList = [];

//_____________________________________________________________________________
// Constructor

    public function __construct()
    {
    }

    /**
     * @return void
     */
    public static function restoreFromSession(): void
    {
    }

//_____________________________________________________________________________
// Modifier

    /**
     * @param string $key
     * @param string $label
     * @param array<mixed> $html_attr
     * @param callable|null $fnRowRender
     * @return $this
     */
    public function addColumn(
        string    $key,
        string $label,
        array     $html_attr = [],
        ?callable $fnRowRender = null
    ): self {
        $this->columnList[] = (new DatatableColumn(
            $key,
            $label,
            $html_attr
        ))->setRowCallback($fnRowRender);

        // TODO : multikey ?
        new DatatableFilterSearch($key, [$key]);
        return $this;
    }

    /**
     * @param array<mixed> $key_list
     * @param string $label
     * @param callable|null $fnRowRender
     * @return $this
     */
    public function addColumnMultikey(
        array     $key_list,
        string $label,
        ?callable $fnRowRender = null
    ): self {
        $key = implode('__', $key_list);
        $this->columnList[] = (new DatatableColumn(
            $key,
            $label,
        ))->setRowCallback($fnRowRender);

        // TODO : multikey ?
        new DatatableFilterSearch($key, $key_list);
        return $this;
    }

    /**
     * @return void
     */
    public function addFilterSelect(): void
    {
    }

    /**
     * @return void
     */
    public function setSort(): void
    {
        // TODO
    }

    /**
     * @return void
     */
    public function setPage(): void
    {
        // TODO
    }

    /**
     * @return void
     */
    public function interpretRequest(): void
    {
        // TODO : retrieve post and modify datatable state
    }


//_____________________________________________________________________________
// Accessor

    /**
     * @return mixed
     */
    public function getColumnList()
    {
        return $this->columnList;
    }

    /**
     * @return array<mixed>
     */
    abstract public function getRawData(): array;

    /**
     * @return array<mixed>
     */
    public function getRowList(): array
    {
        $row_list = [];

        foreach ($this->getRawData() as $raw_data) {
            $row_list[] = $this->processRow($raw_data);
        }

        return $row_list;
    }

    /**
     * @param mixed $raw_data
     * @return array<mixed>
     */
    private function processRow($raw_data): array
    {
        $row = [];
        foreach ($this->getColumnList() as $col) {
            $key = $col->getKey();
            $row[$key] = $col->renderRow($raw_data, $raw_data[$key] ?? null, $key);
        }

        return $row;
    }
}
