<?php
namespace ModulesGarden\Servers\ZimbraEmail\App\Services;


use ModulesGarden\Servers\ZimbraEmail\App\Libs\Product\ProductManager;
use ModulesGarden\Servers\ZimbraEmail\App\Traits\HostingService;
use ModulesGarden\Servers\ZimbraEmail\App\Traits\ProductService;
use function ModulesGarden\Servers\ZimbraEmail\Core\Helper\sl;
use ModulesGarden\Servers\ZimbraEmail\Core\UI\Widget\Sidebar\Sidebar;
use ModulesGarden\Servers\ZimbraEmail\Core\UI\Widget\Sidebar\SidebarItem;
use ModulesGarden\Servers\ZimbraEmail\Core\UI\Widget\Sidebar\SidebarService;

/**
 *
 * Created by PhpStorm.
 * User: Tomasz Bielecki ( tomasz.bi@modulesgarden.com )
 * Date: 10.09.19
 * Time: 10:23
 * Class ClientAreaSidebarService
 */
class ClientAreaSidebarService
{

    const SUPPORTED_SERVER_TYPES = ['zimbraEmail'];

    use HostingService;
    use \ModulesGarden\Servers\ZimbraEmail\Core\UI\Traits\WhmcsParams;
    use ProductService;
    //use ProductService;
    //u/se BaseClientController;

    /**
     * @var \WHMCS\View\Menu\Item
     */
    private $primarySidebar;

    /**
     * ClientAreaSidebarService constructor.
     * @param $hostingId
     */
    public function __construct($hostingId, \WHMCS\View\Menu\Item $primarySidebar)
    {
        $this->setHostingId($hostingId);
        $this->primarySidebar = $primarySidebar;
    }

    /**
     * @return $this|void
     */
    public function informationReplaceUri()
    {
        $overview = $this->primarySidebar->getChild('Service Details Overview');
        if (!is_a($overview, '\WHMCS\View\Menu\Item'))
        {
            return;
        }
        $panel = $overview->getChild('Information');
        if (!is_a($panel, '\WHMCS\View\Menu\Item'))
        {
            return;
        }
        $panel->setUri("clientarea.php?action=productdetails&id={$this->getHostingId()}");
        $panel->setAttributes([]);
        return $this;
    }

    /**
     * @return $this|void
     */
    public function changePasswordReplaceUri()
    {
        $actions = $this->primarySidebar->getChild('Service Details Overview');
        if (!is_a($actions, '\WHMCS\View\Menu\Item'))
        {
            return;
        }
        $panel = $actions->getChild('Change Password');
        if (!is_a($panel, '\WHMCS\View\Menu\Item'))
        {
            return;
        }
        $panel->setUri("clientarea.php?action=productdetails&id={$this->getHostingId()}#tabChangepw");
        $panel->setAttributes([]);
        return $this;
    }

    /**
     *
     */
    public function build()
    {
        if(!in_array($this->hosting()->product->servertype, self::SUPPORTED_SERVER_TYPES))
        {
            return;
        }

        /**
         * @var SidebarService $sidebarService
         */
        $sidebarService = sl("sidebar");
        $this->setProductId($this->hosting()->packageid);
        /**
         * product manager
         */
        $productManager = new ProductManager();
        $productManager->loadById($this->hosting()->packageid);

        /**
         * @var $lang \ModulesGarden\Servers\ProxmoxVps\Core\Lang\Lang
         */
        $lang  = sl("lang");
        $order = 671;
        foreach ($sidebarService->get() as $sidebar)
        {
            /**
             * @var Sidebar $sidebar
             */
            $newPanel = [
                'label' => $lang->abtr($sidebar->getTitle()),
                'order' => $order
            ];
            $order++;
            $childPanel = $this->primarySidebar->addChild($sidebar->getName(), $newPanel);
            foreach ($sidebar->getChildren() as $sidebarItem)
            {
                /**
                 * @var SidebarItem $sidebarItem
                 *
                 * check if is enabled in configuration
                 */
                if(!$productManager->isSidebarEnabled($sidebarItem->getTitle()))
                {
                    continue;
                }

                /**
                 *
                 * add item to menu
                 */
                $newItem = [
                    'label'   => $lang->abtr($sidebarItem->getTitle()),
                    'uri'     => str_replace('{$hostingId}', $this->getHostingId(), $sidebarItem->getHref()),
                    'order'   => $sidebarItem->getOrder(),
                    "current" => $sidebarItem->isActive()
                ];

                $childPanel->addChild($sidebarItem->getName(), $newItem);
            }
        }
    }

    /**
     * @return bool
     */
    public function isActive()
    {
        return true;
    }

    /**
     * @return bool
     */
    public function isSupportedModule()
    {
        return true;
    }
}
