<?php

namespace Plainwork\Ui\Renderer;

use Plainwork\Http\Request;
use Plainwork\Ui\Component\Table;
use Plainwork\Ui\Ui;
use Plainwork\Ui\Html;

final class TableRenderer
{
    public $ui = Ui::class;

    public function __invoke(Table $el, Request $request): string
    {
        $headRows = $el->getHeadRows();
        $footRows = $el->getFootRows();
        $rows = $el->getRows();
        $rows = array_values($rows);
        $countRows = count($rows);

// _print_r($rows);

        // if (!$countRows) {
            // $ret = '';
            // return $ret;
        // }

        $ret = [];

        $ret[] = '<table';

        if ($el->isAutoWidth()) {
            $el->withAttr('style', 'width: auto;');
        }
        if (!$el->isResponsive()) {
            $el->withAttr('class', 'pw-noresponsive');
        }

        $attr = $el->getAttr();
        if ($attr) {
            $ret[] = ' ' . Html::attr($attr);
        }
        $ret[] = '>';

        $caption = $el->getCaption();
        $caption = $this->ui->render($caption, $request);
        if ($caption) {
            $captionAttr = $el->getCaptionAttr();
            $ret[] = '<caption' . ($captionAttr ? ' ' . Html::attr($captionAttr) : '') . '>';
            $ret[] = $caption;
            $ret[] = '</caption>';
        }

        $columns = $el->getColumns();
        $columns = array_values($columns);
        $countColumns = count($columns);

        $isSpanned = [];

        if ($headRows) {
            $ret[] = '<thead';
            $attr = [];
            if ($attr) {
                $ret[] = ' ' . Html::attr($attr);
            }
            $ret[] = '>';

            $headRows = array_values($headRows);
            $countHeadRows = count($headRows);
            for ($rii = 0; $rii < $countHeadRows; $rii++) {
                $rowId = $headRows[$rii];

                $rid_ = $headRows[$rii];
                $row = $el->getRow($rid_);

                $attr = $row->getAttr();
                $ret[] = '<tr';
                if ($attr) {
                    $ret[] = ' ' . Html::attr($attr);
                }
                $ret[] = '>';

                for ($cii = 0; $cii < $countColumns; $cii++) {
                    $colId = $columns[$cii];
                    $cell = $el->getCell($rowId, $colId);

                    if (!$cell) {
                        // $ret[] = '<td></td>';
                        continue;
                    }

                    $cell->setTag($cell->isHead() ? 'th' : 'td');
                    if ($cell->isScopeRow()) {
                        $cell->withAttr('scope', 'row');
                    }

                    $width = $cell->getWidth();
                    if ($width) {
                        if ($width <= 0.25) {
                            $cell->withAttr('class', 'pw-col-1em');
                        } elseif ($width <= 0.5) {
                            $cell->withAttr('class', 'pw-col-2em');
                        } elseif ($width <= 0.75) {
                            $cell->withAttr('class', 'pw-col-3em');
                        } else {
                            $cell->withAttr('class', 'pw-col-' . $width);
                        }
                    }

                    $ret[] = $this->ui->render($cell, $request);
                }
                $ret[] = '</tr>';
            }

            $ret[] = '</thead>';
        }

        if ($rows) {
            $ret[] = '<tbody';
            $attr = [];
            if ($attr) {
                $ret[] = ' ' . Html::attr($attr);
            }
            $ret[] = '>';

            for ($rii = 0; $rii < $countRows; $rii++) {
                $rowId = $rows[$rii];
                $row = $el->getRow($rowId);
                $attr = $row->getAttr();

                $ret[] = '<tr';
                if ($attr) {
                    $ret[] = ' ' . Html::attr($attr);
                }
                $ret[] = '>';

                for ($cii = 0; $cii < $countColumns; $cii++) {
                    $colId = $columns[$cii];
                    $cell = $el->getCell($rowId, $colId);

                    if ($cell) {
                        $cell->setTag($cell->isHead() ? 'th' : 'td');
                        if ($cell->isScopeRow()) {
                            $cell->withAttr('scope', 'row');
                        }

                        $isDanger = $cell->isDanger();
                        if ($isDanger) {
                            $cell->withAttr('class', 'pw-danger');
                        }

// $content = $rowId . '-' . $colId;
// $cell->setContent($content);
                        $ret[] = $this->ui->render($cell, $request);
                    } else {
                        // $ret[] = '<td>x</td>';
                    }
                }
                $ret[] = '</tr>';
            }

            $ret[] = '</tbody>';
        }

        if ($footRows) {
            $ret[] = '<tfoot';
            $attr = [];
            if ($attr) {
                $ret[] = ' ' . Html::attr($attr);
            }
            $ret[] = '>';

            $footRows = array_values($footRows);
            $countFootRows = count($footRows);
            for ($rii = 0; $rii < $countFootRows; $rii++) {
                $rowId = $footRows[$rii];
                $ret[] = '<tr>';

                for ($cii = 0; $cii < $countColumns; $cii++) {
                // foreach ($columns as $colId) {
                    $colId = $columns[$cii];
                    $cell = $el->getCell($rowId, $colId);

                    if (!$cell) {
                        // $ret[] = '<td></td>';
                        continue;
                    }

                    // if (!$cell) {
                        // $ret[] = '<td></td>';
                        // continue;
                    // }

                    $cell->setTag($cell->isHead() ? 'th' : 'td');
                    if ($cell->isScopeRow()) {
                        $cell->withAttr('scope', 'row');
                    }

                    $ret[] = $this->ui->render($cell, $request);
                }
                $ret[] = '</tr>';
            }

            $ret[] = '</tfoot>';
        }

        $ret[] = '</table>';
        $ret = join('', $ret);

        return $ret;
    }
}
