<?php

namespace ModulesGarden\HostingQuotaNotifications\App\UI\Groups\Pages;

use \ModulesGarden\HostingQuotaNotifications\Core\UI\Builder\BaseContainer;
use \ModulesGarden\HostingQuotaNotifications\Core\UI\Interfaces\AdminArea;
use \ModulesGarden\HostingQuotaNotifications\App\UI\Groups\Providers\GroupsProvider;
use \ModulesGarden\HostingQuotaNotifications as main;
use \ModulesGarden\HostingQuotaNotifications\Core\UI\Widget\DataTable\DataTable;
use \ModulesGarden\HostingQuotaNotifications\Core\UI\Widget\DataTable\Column;
use \ModulesGarden\HostingQuotaNotifications\Core\UI\Widget\DataTable\DataProviders\DataProvider;
use \ModulesGarden\HostingQuotaNotifications\Core\UI\Widget\DataTable\DataProviders\Providers\ArrayDataProvider;
use \ModulesGarden\HostingQuotaNotifications\App\UI\Groups\Buttons;
use \ModulesGarden\HostingQuotaNotifications\Core\Helper\BuildUrl;


class Groups extends DataTable implements AdminArea
{
    protected $id   = 'groups_id';
    protected $name = 'groups_name';

    public function __construct($baseId = null)
    {
        parent::__construct($baseId);
    }

    protected function loadHtml()
    {
        $this
                ->addColumn((new Column('id'))
                        ->setOrderable(DataProvider::SORT_ASC)
                        ->setSearchable(true, Column::TYPE_INT))
                ->addColumn((new Column('name'))
                        ->setSearchable(true)
                        ->setOrderable())
                ->addColumn((new Column('type'))
                        ->setSearchable(true))
                ->addColumn((new Column('status')));
    }

    public function customColumnHtmlStatus()
    {
        $switch = new Buttons\StatusSwitch();
        return $switch->getHtml();
    }

    protected function loadData()
    {
        $provider = new GroupsProvider();
        $dataProv = new ArrayDataProvider();
        $dataProv->setData($provider->getGroups());
        $this->setDataProvider($dataProv);
    }
    
    public function initContent()
    {
        $this->appendMassActionButtons();
        $this->appendButtons();
        $this->appendActionButtons();
    }

    protected function appendMassActionButtons()
    {
        $this->addMassActionButton(new Buttons\MassEnableButton());
        $this->addMassActionButton(new Buttons\MassDisableButton());
        $this->addMassActionButton(new Buttons\MassDeleteButton());
    }
    
    protected function appendButtons()
    {
        $this->addButton(new Buttons\AddGroupButton());
    }
    
    protected function appendActionButtons()
    {
        $edit = new Buttons\GroupEditButton();
        $edit->setRawUrl(BuildUrl::getUrl('groups', 'configuration'))
                ->setRedirectParams(['groupid' => ':id']);
        $this->addActionButton($edit);
        
        $this->addActionButton((new Buttons\GroupDeleteButton()));
    }
}
