<?php

namespace Lightscale\LaralightTables\Columns;

use Lightscale\LaralightTables\TableComponent;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\View\ComponentAttributeBag;
use Illuminate\Support\HtmlString;

use Closure;

class Column {

    private TableComponent $table;
    private bool $showInSelect;
    private bool $shouldEscape = true;

    private ?Closure $slotFn = null;
    private ?Closure $sortFn = null;
    private ?Closure $tdAttributesFn = null;

    private ?string $colClass = null;

    public function __construct(
        public string $name,
        private ?string $title = null
    ) {
        $this->showInSelect = $this->title !== null;
    }

    public static function make(string $name, ?string $title = null): static
    {
        return new static($name, $title);
    }

    public function setTable(TableComponent $table): void
    {
        $this->table = $table;
    }

    public function getTitle(): string
    {
        return $this->title;
    }

    private function defaultSlot(Model $row): string
    {
        return (string) $row->{$this->name};
    }

    public function slot(callable $fn) : static
    {
        $this->slotFn = Closure::fromCallable($fn);
        return $this;
    }

    public function sortable(?callable $fn): static
    {
        $this->sortFn = $fn;
        return $this;
    }

    public function isSortable(): bool
    {
        return $this->sortFn !== null;
    }

    public function applySort(Builder $query, string $dir): void
    {
        if ($this->sortFn !== null) {
            ($this->sortFn)($query, $dir);
        }
    }

    public function colClass(string $v) : static
    {
        $this->colClass = $v;
        return $this;
    }

    public function getColClass() : ?string
    {
        return $this->colClass;
    }

    public function tdAttributes(callable $fn) : static
    {
        $this->tdAttributesFn = Closure::fromCallable($fn);
        return $this;
    }

    public function showInSelect($show = true): static
    {
        $this->showInSelect = $show;
        return $this;
    }

    public function getShowInSelect(): bool
    {
        return $this->showInSelect;
    }

    public function shouldEscape(bool $v): static
    {
        $this->shouldEscape = $v;
        return $this;
    }

    public function escape(string $content): string
    {
        return $this->shouldEscape ? e($content) : $content;
    }

    protected function getContent(Model $row): string
    {
        return $this->escape(
            $this->slotFn?->call($this, $row, $this) ??
            $this->defaultSlot($row)
        );
    }

    public function view(Model $row): HtmlString
    {
        $attributes = $this->tdAttributesFn?->call($this, $row) ?? [];
        $attributes = (new ComponentAttributeBag($attributes))->toHtml();
        $content = $this->getContent($row);
        return new HtmlString("<td {$attributes}>{$content}</td>");
    }

}