<?php

namespace App\Modules\Admin;

class Menu implements ControlPanelMenuInterface
{
    /**
     * A list of all sections.
     *
     * @var array
     */
    protected static $sections = [];

    /**
     * Registers a new item with the menu.
     *
     * @param  string $name
     * @param  string $link
     * @param  string $icon
     * @param  string $section
     *
     * @return bool
     */
    public static function register($name, $link, $icon = null, $section = 'main')
    {
        if (! isset(self::$sections[$section])) {
            self::$sections[$section] = [];
        }

        // Add the item to the list
        self::$sections[$section][] = [
            'name' => $name,
            'link' => $link,
            'icon' => $icon,
        ];

        return true;
    }

    /**
     * Determines if the section has any items.
     *
     * @param  string  $section
     *
     * @return bool
     */
    public static function hasItems($section = 'main')
    {
        return isset(self::$sections[$section]) ? count(self::$sections[$section]) : false;
    }

    /**
     * Renders the section.
     *
     * @param  string $section
     *
     * @return string
     */
    public static function render($section = 'main')
    {
        $items = isset(self::$sections[$section]) ? self::$sections[$section] : [];

        // Check if there are any items
        if (count($items) == 0) {
            return '';
        }

        switch ($section) {
            case 'addons' : $color = '#C53D36'; break;
            case 'others' : $color = '#C53D36'; break;
            default       : $color = '#3CA2C0'; break;
        }

        // Now render all the items
        $out = '<ul class="list-unstyled admin-settings-list">';

        foreach ($items as $item) {
            $out .= '<li>';
            $out .= '<div class="text-center">';
            $out .= '<a href="'.$item['link'].'" style="color: '.$color.'; text-decoration: none;">';
            $out .= '<span class="fa-stack fa-2x">
						<i class="fa fa-circle fa-stack-2x" style="color: '.$color.';"></i>
						<i class="fa '.$item['icon'].' fa-stack-1x fa-inverse"></i>
					</span>';
            $out .= '<p style="color: '.$color.'; margin-top: 5px;">'.$item['name'].'</p>';
            $out .= '</a>';
            $out .= '</div>';
            $out .= '</li>';
        }

        $out .= '</ul>';

        return $out;
    }
}
