<?php

declare(strict_types=1);

namespace PpPhp\Core\Designer\Inspector;

use PpPhp\Core\Designer\Selection\DesignerSelectionTarget;

final class DesignerInspectorBuilder
{
    public function build(array $page, DesignerSelectionTarget $target): array
    {
        return DesignerInspectorDefinition::create(
            $target->toArray(),
            $this->breadcrumb($page, $target),
            [
                DesignerInspectorTab::create('general', 'General', $this->generalFields($target))->toArray(),
                DesignerInspectorTab::create('layout', 'Layout', $this->layoutFields())->toArray(),
                DesignerInspectorTab::create('style', 'Style', $this->styleFields())->toArray(),
                DesignerInspectorTab::create('data', 'Data', $this->dataFields($target))->toArray(),
                DesignerInspectorTab::create('events', 'Events', $this->eventFields())->toArray(),
                DesignerInspectorTab::create('visibility', 'Visibility', $this->visibilityFields())->toArray(),
                DesignerInspectorTab::create('resources', 'Resources', $this->resourceFields())->toArray(),
                DesignerInspectorTab::create('advanced', 'Advanced', $this->advancedFields($target))->toArray(),
            ],
            ['rename', 'duplicate', 'delete', 'hide', 'lock', 'moveUp', 'moveDown', 'copy', 'paste'],
            [
                'componentId' => $target->componentId(),
                'componentType' => $target->componentType(),
                'part' => $target->part(),
                'path' => $target->path(),
            ]
        )->toArray();
    }

    private function breadcrumb(array $page, DesignerSelectionTarget $target): array
    {
        $crumbs = [
            ['label' => (string) ($page['title'] ?? $page['name'] ?? 'Page'), 'type' => 'page'],
        ];

        if ($target->componentId() !== '') {
            $crumbs[] = ['label' => $target->componentId(), 'type' => $target->componentType()];
        }

        if ($target->part() !== null) {
            $crumbs[] = ['label' => $target->label(), 'type' => $target->part()];
        }

        return $crumbs;
    }

    private function generalFields(DesignerSelectionTarget $target): array
    {
        return [
            ['name' => 'id', 'label' => 'ID', 'type' => 'readonly', 'value' => $target->componentId()],
            ['name' => 'label', 'label' => 'Label', 'type' => 'text', 'value' => $target->label()],
            ['name' => 'type', 'label' => 'Type', 'type' => 'readonly', 'value' => $target->part() ?: $target->componentType()],
            ['name' => 'part', 'label' => 'Part', 'type' => 'readonly', 'value' => $target->part() ?? 'component'],
        ];
    }

    private function layoutFields(): array
    {
        return [
            ['name' => 'display', 'label' => 'Display', 'type' => 'select', 'options' => ['block', 'flex', 'grid', 'inline-block']],
            ['name' => 'width', 'label' => 'Width', 'type' => 'text'],
            ['name' => 'height', 'label' => 'Height', 'type' => 'text'],
            ['name' => 'align', 'label' => 'Align', 'type' => 'select', 'options' => ['left', 'center', 'right']],
        ];
    }

    private function styleFields(): array
    {
        return [
            ['name' => 'class', 'label' => 'CSS Class', 'type' => 'text'],
            ['name' => 'margin', 'label' => 'Margin', 'type' => 'spacing'],
            ['name' => 'padding', 'label' => 'Padding', 'type' => 'spacing'],
            ['name' => 'fontSize', 'label' => 'Font Size', 'type' => 'text'],
            ['name' => 'backgroundColor', 'label' => 'Background', 'type' => 'color'],
            ['name' => 'color', 'label' => 'Text Color', 'type' => 'color'],
            ['name' => 'borderRadius', 'label' => 'Border Radius', 'type' => 'text'],
        ];
    }

    private function dataFields(DesignerSelectionTarget $target): array
    {
        $fields = [
            ['name' => 'binding', 'label' => 'Data Binding', 'type' => 'text'],
            ['name' => 'source', 'label' => 'Data Source', 'type' => 'text'],
        ];

        if ($target->part() === 'column') {
            $fields[] = ['name' => 'field', 'label' => 'Field', 'type' => 'readonly', 'value' => $target->meta()['field'] ?? ''];
        }

        return $fields;
    }

    private function eventFields(): array
    {
        return [
            ['name' => 'onClick', 'label' => 'On Click', 'type' => 'code'],
            ['name' => 'onChange', 'label' => 'On Change', 'type' => 'code'],
            ['name' => 'onLoad', 'label' => 'On Load', 'type' => 'code'],
        ];
    }

    private function visibilityFields(): array
    {
        return [
            ['name' => 'hidden', 'label' => 'Hidden', 'type' => 'checkbox'],
            ['name' => 'locked', 'label' => 'Locked', 'type' => 'checkbox'],
            ['name' => 'roles', 'label' => 'Allowed Roles', 'type' => 'tags'],
            ['name' => 'condition', 'label' => 'Visibility Condition', 'type' => 'code'],
        ];
    }

    private function resourceFields(): array
    {
        return [
            ['name' => 'icon', 'label' => 'Icon', 'type' => 'icon-picker'],
            ['name' => 'css', 'label' => 'CSS Resources', 'type' => 'resource-list'],
            ['name' => 'js', 'label' => 'JS Resources', 'type' => 'resource-list'],
        ];
    }

    private function advancedFields(DesignerSelectionTarget $target): array
    {
        return [
            ['name' => 'path', 'label' => 'State Path', 'type' => 'readonly', 'value' => implode('.', $target->path())],
            ['name' => 'meta', 'label' => 'Metadata', 'type' => 'json', 'value' => $target->meta()],
        ];
    }
}
