<?php
/* * ********************************************************************
*  ProxmoxVPS Product developed. (27.03.19)
* *
*
*  CREATED BY MODULESGARDEN       ->       http://modulesgarden.com
*  CONTACT                        ->       contact@modulesgarden.com
*
*
* This software is furnished under a license and may be used and copied
* only  in  accordance  with  the  terms  of such  license and with the
* inclusion of the above copyright notice.  This software  or any other
* copies thereof may not be provided or otherwise made available to any
* other person.  No title to and  ownership of the  software is  hereby
* transferred.
*
*
* ******************************************************************** */

namespace ModulesGarden\ProxmoxAddon\App\Services\Cloud;

use ModulesGarden\ProxmoxAddon\App\Models\VmModel;
use ModulesGarden\ProxmoxAddon\Core\UI\Traits\WhmcsParams;
use ModulesGarden\ProxmoxAddon\Core\UI\Widget\Sidebar\Sidebar;
use ModulesGarden\ProxmoxAddon\Core\UI\Widget\Sidebar\SidebarItem;
use ModulesGarden\ProxmoxAddon\Core\UI\Widget\Sidebar\SidebarService;
use ModulesGarden\Servers\ProxmoxCloudVps\App\Http\Client\BaseClientController;
use function ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\sl;

class ClientAreaSidebarService
{
    use HostingService;
    use WhmcsParams;
    use ProductService;
    use 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;
    }

    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;
    }

    public function serviceDetailsOverview(){

        if(!$this->getWhmcsParamByKey("packageid") || !$this->configuration()->isPermissionCustomTemplates()){
            return;
        }
        $overview = $this->primarySidebar->getChild('Service Details Overview');
        if (!is_a($overview, '\WHMCS\View\Menu\Item'))
        {
            return;
        }
        $order = 800;
        foreach ( sl("sidebar")->getSidebar('Service Details Overview')->get() as $sidebarItem)
        {
            /**
             * @var SidebarItem $sidebarItem
             */
            $newItem = [
                'label'   => sl('lang')->abtr($sidebarItem->getTitle()),
                'uri'     => str_replace('{$hostingId}', $this->getHostingId(), $sidebarItem->getHref()),
                'order'   => $sidebarItem->getOrder(),
                "current" => $sidebarItem->isActive()
            ];
            $childOrder =  $sidebarItem->getOrder();
            $overview->addChild($sidebarItem->getName(), $newItem);
        }
//        /**
//         * @var Sidebar $sidebar
//         */
//        $newPanel = [
//            'label' => sl("lang")->abtr('customTemplates'),
//            'order' => 6969
//            'uri'     => str_replace('{$hostingId}', $this->getHostingId(), $sidebarItem->getHref()),
//            "current" => $sidebarItem->isActive()
//        ];
//        $childPanel = $this->primarySidebar->addChild('customTemplates', $newPanel);
    }

    public function build()
    {
        /**
         * @var $sidebarService SidebarService
         */
        $sidebarService = sl("sidebar");
        $this->setProductId($this->hosting()->packageid);
        /**
         * @var $lang \ModulesGarden\Servers\ProxmoxVps\Core\Lang\Lang
         */
        $lang  = sl("lang");
        $request = sl('request');
        $order = 671;
        $vmUrl = 'clientarea.php?action=productdetails&id={$hostingId}&modop=custom&a=management&mg-page=vm&vm={$vmId}';

        /**
         * virtualMachines
         */
        foreach ($sidebarService->get() as $sidebar)
        {

            if($sidebar->getName()=='Service Details Overview'){
                continue;
            }
            /**
             * @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
                 */
                $newItem = [
                    'label'   => $lang->abtr($sidebarItem->getTitle()),
                    'uri'     => str_replace('{$hostingId}', $this->getHostingId(), $sidebarItem->getHref()),
                    'order'   => $sidebarItem->getOrder(),
                    "current" => $sidebarItem->isActive()
                ];
                $childOrder =  $sidebarItem->getOrder();
                $childPanel->addChild($sidebarItem->getName(), $newItem);
            }
            /**
             * @var VmModel $vmModel
             */
            foreach (VmModel::ofHostingId( $this->getHostingId())->notVmid(0)->notTemplate()->get() as $vmModel){
            	if($sidebar->getName()=='Virtual Datacenter'){
		    continue;
	        }
                $childOrder++;
                $newItem = [
                    'label'   => $vmModel->name,
                    'uri'     => str_replace(['{$hostingId}', '{$vmId}'], [$this->getHostingId(),$vmModel->id], $vmUrl),
                    'order'   => $childOrder,
                    "current" => $request->get('vm')==$vmModel->id
                ];
                $childPanel->addChild($vmModel->name, $newItem);
            }


        }
    }
}