<?php
namespace ModulesGarden\Servers\ZimbraEmail\App\UI\Client\EmailAlias\Pages;


use ModulesGarden\Servers\ZimbraEmail\App\Helpers\ZimbraManager;
use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Helpers\AccountHelper;
use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\Account;
use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\AccountAlias;
use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Repository;
use ModulesGarden\Servers\ZimbraEmail\App\UI\Client\EmailAlias\Buttons\AddEmailAliasButton;
use ModulesGarden\Servers\ZimbraEmail\App\UI\Client\EmailAlias\Buttons\DeleteEmailAliasButton;
use ModulesGarden\Servers\ZimbraEmail\App\UI\Client\EmailAlias\Buttons\MassDeleteEmailAliasButton;
use ModulesGarden\Servers\ZimbraEmail\Core\Models\Whmcs\Hosting;
use ModulesGarden\Servers\ZimbraEmail\Core\UI\Interfaces\ClientArea;
use ModulesGarden\Servers\ZimbraEmail\Core\UI\Widget\DataTable\Column;
use ModulesGarden\Servers\ZimbraEmail\Core\UI\Widget\DataTable\DataProviders\DataProvider;
use ModulesGarden\Servers\ZimbraEmail\Core\UI\Widget\DataTable\DataProviders\Providers\ArrayDataProvider;
use ModulesGarden\Servers\ZimbraEmail\Core\UI\Widget\DataTable\DataTable;
use ModulesGarden\Servers\ZimbraEmail\Core\UI\Widget\Others\Label;

/**
 *
 * Created by PhpStorm.
 * User: Tomasz Bielecki ( tomasz.bi@modulesgarden.com )
 * Date: 18.09.19
 * Time: 11:46
 * Class Aliases
 */
class Aliases extends DataTable implements ClientArea
{
    protected $id    = 'emailAliases';
    protected $name  = 'emailAliases';
    protected $title = null;


    protected function loadHtml()
    {
        $this
            ->addColumn((new Column('account'))
                ->setOrderable(DataProvider::SORT_ASC)
                ->setSearchable(true, Column::TYPE_STRING))
            ->addColumn((new Column('email_alias'))
                ->setOrderable()
                ->setSearchable(true));
    }

    public function initContent()
    {
        $this->addMassActionButton(new MassDeleteEmailAliasButton());

        $addEmailAliasesButton = new AddEmailAliasButton();

        if($this->isCreateButtonDisabled())
        {
            $addEmailAliasesButton->addHtmlAttribute('disabled', true);
        }

        $this->addButton($addEmailAliasesButton);
        $this->addActionButton(new DeleteEmailAliasButton());
    }

    public function loadData()
    {
        /**
         * load hosting
         */
        $hosting = Hosting::where('id', $this->getRequestValue('id'))->first();

        /**
         * load api
         */
        $aliases     = (new ZimbraManager())
            ->getApiByServer($hosting->server)
            ->soap->repository()
            ->accounts
            ->getAccountAliasesByDomainName($hosting->domain);

        /**
         * format model to array
         */
        $data = [];
        foreach ($aliases as $alias)
        {
            /* @var $alias AccountAlias */
            $tmp = [
                'id'            => base64_encode(json_encode(['alias' => $alias->getAlias(), 'accId' => $alias->getAccountId()])),
                'email_alias'   => $alias->getAlias(),
                'account'       => $alias->getAccountName()
            ];

            $data[] = $tmp;
        }

        $dataProv = new ArrayDataProvider();
        $dataProv->setDefaultSorting('account', 'ASC')->setData($data);

        $this->setDataProvider($dataProv);
    }

    /**
     *
     * check if add button should be disabled
     * @return bool
     */
    private function isCreateButtonDisabled()
    {
        $hid = $this->getRequestValue('id');

        /**
         * hosting model
         */
        $hosting = Hosting::where('id', $hid)->first();

        $accounts =(new ZimbraManager())
            ->getApiByHosting($hid)
            ->soap
            ->repository()
            ->accounts
            ->getByDomainName($hosting->domain);

        return !(count($accounts) > 0);
    }
}