<?php

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

use ModulesGarden\HostingQuotaNotifications\App\Helper\Logger\Cron;
use \ModulesGarden\HostingQuotaNotifications\Core\Helper;
use \ModulesGarden\HostingQuotaNotifications\Core\UI\Interfaces\AdminArea;
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\Core\UI\Widget\Others;
use \ModulesGarden\HostingQuotaNotifications\Core\Logger\Entity;


use \ModulesGarden\HostingQuotaNotifications\App\UI\LoggerManager\Buttons\DeleteLoggerModalButton;
use \ModulesGarden\HostingQuotaNotifications\App\UI\LoggerManager\Buttons\MassDeleteLoggerButton;
use \ModulesGarden\HostingQuotaNotifications\App\Helper\Logger\ModuleAction;
use \ModulesGarden\HostingQuotaNotifications\App\Helper\Logger\SendMail;
use ModulesGarden\HostingQuotaNotifications\App\Helper\Logger\OpenTicket;


/**
 * Description of LoggerPage
 *
 * @author inbs
 */
class LoggerPage extends DataTable implements AdminArea
{
    protected $id    = 'loggercont';
    protected $name  = 'loggercont';
    protected $title = null;

    protected $colorArray = [
        Entity::TYPE_DEBUG    => [
            'color'           => '7b007b',
            'backgroundColor' => 'e9ebf0'
        ],
        Entity::TYPE_ERROR    => [
            'color'           => 'fcffff',
            'backgroundColor' => 'ed4040'
        ],
        Entity::TYPE_INFO     => [
            'color'           => 'e9fff7',
            'backgroundColor' => '737980'
        ],
        Entity::TYPE_SUCCESS  => [
            'color'           => 'e5fff4',
            'backgroundColor' => '5bc758'
        ],
        Entity::TYPE_CRITICAL => [
            'color'           => 'fcffff',
            'backgroundColor' => 'ed4040'
        ]
    ];

    protected $messages = [
        'SendEmail'
    ];

    protected function loadHtml()
    {
        $this->addColumn((new Column('id'))->setOrderable(DataProvider::SORT_DESC)
            ->setSearchable(true, Column::TYPE_INT))
            ->addColumn((new Column('message'))->setOrderable()
                ->setSearchable(true))
            ->addColumn((new Column('type'))->setOrderable()->setSearchable(true))
            ->addColumn((new Column('date'))->setSearchable(true, Column::TYPE_DATE)->setOrderable());
    }


    public function replaceFieldMessage($key, $row)
    {
        switch ($row[$key])
        {
            case SendMail::SEND_EMAIL:
                return SendMail::format($row, $key);
            case OpenTicket::OPEN_TICKET:
            case OpenTicket::NO_LANG:
                return OpenTicket::format($row, $key);
            case ModuleAction::MODULE_TERMINATE:
            case ModuleAction::MODULE_SUSPEND:
            case ModuleAction::MODULE:
                return ModuleAction::format($row, $key);
            case Cron::CRON:
                return Cron::format($row, $key);
        }
        return html_entity_decode($row[$key]);
    }

    public function replaceFieldType($key, $row)
    {
        return (new Others\Label())->initIds('label')
            ->setMessage($row['typeLabel'])
            ->setTitle($row['typeLabel'])
            ->setColor($this->colorArray[$row[$key]]['color'])
            ->setBackgroundColor($this->colorArray[$row[$key]]['backgroundColor'])
            ->getHtml();
    }

    public function initContent()
    {
        $this->addActionButton((new DeleteLoggerModalButton()));
        $this->addMassActionButton((new MassDeleteLoggerButton()));
    }

    protected function loadData()
    {
        $collection = Helper\sl('entityLogger')->all();
        $data       = [];
        foreach ($collection as $model)
        {
            $oneLogData = $model->toArray();
            $oneLogData['date'] = date( 'Y-m-d H:i:s', strtotime($oneLogData['date']));

            $data[] = $oneLogData;
        }

        $dataProv = new ArrayDataProvider();
        $dataProv->setDefaultSorting('id', 'desc')->setData($data);

        $this->setDataProvider($dataProv);
    }
}
