<?php

namespace App\Http;

use Nwidart\Menus\Presenters\Presenter;

class AdminlteCustomPresenter extends Presenter
{
    /**
     * {@inheritdoc}.
     */
    public function getOpenTagWrapper()
    {
        return '<div class="tw-flex-1 tw-p-3 tw-space-y-3 tw-overflow-y-auto tw-border-r tw-border-gray-200" id="side-bar">' . PHP_EOL;
    }

    /**
     * {@inheritdoc}.
     */
    public function getCloseTagWrapper()
    {
        return '</div>' . PHP_EOL;
    }

    /**
     * {@inheritdoc}.
     */
    public function getMenuWithoutDropdownWrapper($item)
    {
        return '<a href="' . $item->getUrl() . '" title="" class="tw-flex tw-items-center tw-gap-3 tw-px-3 tw-py-2 tw-text-sm tw-font-medium tw-tracking-tight tw-text-gray-600 tw-transition-all tw-duration-200 tw-rounded-lg tw-whitespace-nowrap hover:tw-text-gray-900 hover:tw-bg-gray-100' . $this->getActiveState($item) . '" ' . $item->getAttributes() . '>' .
        $this->formatIcon($item->icon) . ' <span class="tw-truncate">' . $item->title . '</span>' .
            '</a>' . PHP_EOL;
    }

    /**
     * {@inheritdoc}.
     */
    public function getActiveState($item, $state = ' tw-bg-gray-200 tw-text-primary-700')
    {
        return $item->isActive() ? $state : null;
    }

    /**
     * Get active state on child items.
     *
     * @param $item
     * @param  string  $state
     * @return null|string
     */
    public function getActiveStateOnChild($item, $state = 'tw-pb-1 tw-rounded-md tw-bg-gray-200 tw-text-primary-700')
    {
        return $item->hasActiveOnChild() ? $state : null;
    }

    /**
     * {@inheritdoc}.
     *
     * Stronger divider: 1px line + a touch of breathing room above and
     * below. Previously this was just empty vertical space which made
     * grouped menus indistinguishable from ungrouped flat lists.
     */
    public function getDividerWrapper()
    {
        return '<div class="tw-my-3 tw-mx-3 tw-border-t tw-border-gray-200"></div>';
    }

    /**
     * {@inheritdoc}.
     *
     * Bolder header: uppercase, semibold, slightly larger, with a tint
     * colour so groups read at a glance. Previous text-xs blended into
     * the body items.
     */
    public function getHeaderWrapper($item)
    {
        return '<div class="tw-px-3 tw-pt-3 tw-pb-1 tw-text-[11px] tw-font-bold tw-uppercase tw-tracking-widest tw-text-gray-500">' . $item->title . '</div>';
    }

    /**
     * {@inheritdoc}.
     */
    public function getMenuWithDropDownWrapper($item)
    {
        $dropdownToggle = '<a href="#" title="" class="drop_down tw-flex tw-items-center tw-gap-3 tw-px-3 tw-py-2 tw-text-sm tw-font-medium tw-tracking-tight tw-text-gray-600 tw-transition-all tw-duration-200 tw-rounded-lg tw-whitespace-nowrap hover:tw-text-gray-900 hover:tw-bg-gray-100 focus:tw-text-gray-900 focus:tw-bg-gray-100' . $this->getActiveStateOnChild($item) . '" ' . $item->getAttributes() . '>' .
        $this->formatIcon($item->icon) . ' <span class="tw-truncate">' . $item->title . '</span>' .
        '<svg aria-hidden="true" class="svg tw-ml-auto tw-text-gray-500 tw-size-4 tw-shrink-0" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">' . $this->getArray($item) .
            '</svg>' .
            '</a>';

        $childItemsContainerStart = '';

        $childItemsContainerEnd = '';

        // Compile child menu items
        $childItems = $this->getChildMenuItems($item);

        // echo "here";
        // print_r($dropdownToggle);exit;

        return '<div class="' . $this->getActiveStateOnChild($item) . '">' . $dropdownToggle . $childItemsContainerStart . $childItems . $childItemsContainerEnd . '</div>' . PHP_EOL;
    }

    /**
     * Get multi-level dropdown wrapper.
     *
     * Note: This example doesn't directly implement a multi-level dropdown, as it wasn't specified, but you could extend
     * the functionality similarly to `getMenuWithDropDownWrapper`, adjusting for deeper nesting.
     *
     * @param  \Nwidart\Menus\MenuItem  $item
     * @return string
     */
    public function getMultiLevelDropdownWrapper($item)
    {
        // Nested dropdown — rendered as a smaller toggle inside a
        // parent's open .chiled container. Matches the parent's
        // <a.drop_down> + next-sibling .chiled pattern so the existing
        // sidebar JS handler picks it up without modification.
        // Inline width/height on the SVG: the previous tw-size-3.5
        // class isn't in this project's Tailwind scale, so the SVG was
        // rendering at the viewport default (~300x150) and pushing the
        // title text off-screen.
        $dropdownToggle = '<a href="#" title="" class="drop_down tw-flex tw-items-center tw-gap-2 tw-px-3 tw-py-1.5 tw-text-sm tw-font-semibold tw-tracking-tight tw-text-gray-700 tw-transition-all tw-duration-200 tw-rounded-md tw-whitespace-nowrap hover:tw-text-gray-900 hover:tw-bg-gray-100' . $this->getActiveStateOnChild($item) . '" ' . $item->getAttributes() . '>' .
            $this->formatIcon($item->icon) .
            '<span class="tw-truncate" style="flex:1;">' . $item->title . '</span>' .
            '<svg aria-hidden="true" class="svg tw-text-gray-400 tw-shrink-0" style="width:14px;height:14px;margin-left:auto;" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">' . $this->getArray($item) .
            '</svg>' .
            '</a>';

        // Note: we render with smaller indent than top-level (.chiled
        // uses tw-pl-11) — nested rail is tw-pl-4 so deep nesting
        // doesn't push items off-screen.
        $childItems = $this->getChildMenuItems($item, /*nested*/ true);

        return '<div class="' . $this->getActiveStateOnChild($item) . '">' . $dropdownToggle . $childItems . '</div>' . PHP_EOL;
    }

    /**
     * Render child menu items inside a collapsible .chiled container.
     *
     * Dispatches by child type so headers/dividers/nested dropdowns
     * all render correctly — the previous implementation flattened
     * every child to <a href="..."> which (a) turned headers/dividers
     * into broken anchors and (b) collapsed nested $sub->dropdown()
     * calls into "#" links that visibly went nowhere on click.
     *
     * @param  \Nwidart\Menus\MenuItem  $item
     * @param  bool  $nested  true when called from getMultiLevelDropdownWrapper
     * @return string
     */
    public function getChildMenuItems($item, $nested = false)
    {
        if (count($item->getChilds()) === 0) return '';

        $displayStyle = $item->hasActiveOnChild() ? 'block' : 'none';
        $padCls       = $nested ? 'tw-pl-5' : 'tw-pl-11';
        $railLeft     = $nested ? 'tw-left-2' : 'tw-left-5';

        $out = '<div class="chiled tw-relative tw-mt-2 tw-mb-4 ' . $padCls . '" style="display:' . $displayStyle . '">'
             . '<div class="tw-absolute tw-inset-y-0 tw-w-px tw-h-full tw-bg-gray-200 ' . $railLeft . '"></div>'
             . '<div class="tw-space-y-3.5">';

        foreach ($item->getChilds() as $child) {
            if ($child->hidden()) continue;

            if ($child->isHeader()) {
                // Sub-section label inside an open dropdown. Smaller +
                // muted vs. top-level headers so the hierarchy reads.
                $out .= '<div class="tw-pt-2 tw-pb-1 tw-text-[10px] tw-font-bold tw-uppercase tw-tracking-widest tw-text-gray-400">'
                      . e($child->title) . '</div>';
                continue;
            }
            if ($child->isDivider()) {
                $out .= '<div class="tw-my-2 tw-border-t tw-border-gray-200"></div>';
                continue;
            }
            if ($child->hasSubMenu()) {
                // Nested dropdown — recurse via the multi-level wrapper.
                $out .= $this->getMultiLevelDropdownWrapper($child);
                continue;
            }

            $isActive = $child->isActive() ? 'tw-text-primary-700 tw-font-semibold' : '';
            $out .= '<a href="' . $child->getUrl() . '" title="" class="tw-flex tw-items-center tw-gap-2 tw-text-sm tw-font-medium tw-tracking-tight tw-text-gray-600 tw-truncate tw-transition-all tw-duration-200 hover:tw-text-gray-900 tw-whitespace-nowrap ' . $isActive . '" ' . $child->getAttributes() . '>'
                  . $this->formatIcon($child->icon)
                  . '<span class="tw-truncate">' . $child->title . '</span>'
                  . '</a>' . PHP_EOL;
        }

        $out .= '</div></div>';
        return $out;
    }

    /**
     * Returns the icon HTML. If the icon is SVG, it returns directly; otherwise, it assumes it's a FontAwesome class and wraps it in an <i> tag.
     *
     * @param string $icon
     * @return string
     */
    protected function formatIcon($icon)
    {
        // Check if the icon string contains "<svg", indicating it's an SVG icon
        if (strpos($icon, '<svg') !== false) {
            return $icon; // Return the SVG icon directly
        } else {
            // Assume it's a FontAwesome icon and return it wrapped in an <i> tag
            return '<i class="' . $icon . '"></i>';
        }
    }

    public function getArray($item)
    {
        if ($item->hasActiveOnChild()) {
            return '<path stroke="none" d="M0 0h24v24H0z" fill="none" /><path d="M6 9l6 6l6 -6" />';
        } else {
            return '<path stroke="none" d="M0 0h24v24H0z" fill="none"/>
            <path d="M15 6l-6 6l6 6" />';
        }
    }
}


