<?php

namespace ModulesGarden\Servers\OpenXchange\App\UI\Client\EmailAlias\Pages;

use ModulesGarden\Servers\OpenXchange\App\Libs\RestApi;
use ModulesGarden\Servers\OpenXchange\App\Helpers\Helper;
use ModulesGarden\Servers\OpenXchange\Core\UI\Interfaces\ClientArea;
use ModulesGarden\Servers\OpenXchange\Core\UI\Widget\DataTable\Column;
use ModulesGarden\Servers\OpenXchange\Core\UI\Widget\DataTable\DataTable;
use ModulesGarden\Servers\OpenXchange\Core\UI\Widget\DataTable\DataProviders\DataProvider;
use ModulesGarden\Servers\OpenXchange\App\UI\Client\EmailAlias\Buttons\AddEmailAliasButton;
use ModulesGarden\Servers\OpenXchange\App\UI\Client\EmailAlias\Buttons\DeleteEmailAliasButton;
use ModulesGarden\Servers\OpenXchange\Core\UI\Widget\DataTable\DataProviders\Providers\ArrayDataProvider;

class EmailAlias extends DataTable implements ClientArea
{

    protected $id    = 'aliases';
    protected $name  = 'aliases';
    protected $title = null;

    /**
     * load columns
     */
    protected function loadHtml()
    {
        $this
        ->addColumn((new Column('user_id'))
            ->setOrderable(DataProvider::SORT_DESC)
            ->setSearchable(true, Column::TYPE_STRING))
        ->addColumn((new Column('primaryEmail'))
            ->setOrderable()
            ->setSearchable(true))
        ->addColumn((new Column('alias'))
            ->setOrderable()
            ->setSearchable(true));
    }

    public function initContent()
    {
        $this->addButton( new AddEmailAliasButton());
        $this->addActionButton(new DeleteEmailAliasButton());
    }


    /**
     * load data
     */
    public function loadData()
    {
        $data    = [];
        $params  = Helper::moduleParams($_REQUEST['id']);
        $context = new RestApi($params);
        $users   = $context->listAllUsers();

        foreach($users as $user)
        {
            if($user->isContextAdmin == true)
            {
                continue;
            }

            $aliases = $user->aliases;

            foreach($aliases as $alias)
            {
                $aliasParts = explode("@", $alias);
                
                if ($aliasParts[1] == $params['serverusername'])
                {
                    continue;
                }

                $accountArray = [
                    'user_id'      => Helper::convertEmailFromPunnycode($user->name),
                    'alias'        => Helper::convertEmailFromPunnycode($alias),
                    'primaryEmail' => Helper::convertEmailFromPunnycode($user->mail),
                    'aliases'      => $aliases,
                    'id'           => base64_encode(json_encode([
                        'user_id'      => Helper::convertEmailFromPunnycode($user->name),
                        'alias'        => Helper::convertEmailFromPunnycode($alias),
                        'primaryEmail' => Helper::convertEmailFromPunnycode($user->mail),
                        'aliases'      => $aliases,
                    ])),
                ];
    
                $data[] = $accountArray;
            }
        }
        $dataProv = new ArrayDataProvider();
        $dataProv->setDefaultSorting('mailbox', 'ASC')->setData($data);
        $this->setDataProvider($dataProv);
    }
}
