<?php

namespace ModulesGarden\HostingQuotaNotifications\App\UI\TicketTemplates\Providers;

use \ModulesGarden\HostingQuotaNotifications\Core\UI\Widget\Forms\DataProviders\BaseModelDataProvider;
use \ModulesGarden\HostingQuotaNotifications\Core\UI\Interfaces\AdminArea;
use \ModulesGarden\HostingQuotaNotifications\App\Libs\TicketTemplates\Manage;
use \ModulesGarden\HostingQuotaNotifications\App\Models\TicketTemplates;
use \ModulesGarden\HostingQuotaNotifications\Core\Helper\BuildUrl;
use \ModulesGarden\HostingQuotaNotifications\Core\UI\ResponseTemplates;
use \ModulesGarden\HostingQuotaNotifications\App\Helper\Request;
use ModulesGarden\HostingQuotaNotifications\App\Libs\TicketTemplatesLangs\Manage as TicketTemplateLangManage;

/**
 * Description of TicketTemplatesProvider
 *
 * @author Artur Pilch <artur.pi@modulesgarden.com>
 */
class TicketTemplatesProvider extends BaseModelDataProvider implements AdminArea
{

    public function __construct($model = null)
    {
        parent::__construct($model);
    }
    
    public function read()
    {
        $this->data['ticketTemplateIdToDelete'] = $this->getActionElementIdValue();
        $this->loadDataToConfiguration();
    }

    public function create()
    {
        $ticketTemplates = new TicketTemplates();
        foreach($this->formData as $key => $value)
        {
            $ticketTemplates->{$key} = $value;
        }
        $ticketTemplates->save();
        return $this->redirectToEdit($ticketTemplates->id);
                
    }

    public function update()
    {
        
    }

    public function delete()
    {
         if($this->getMassActionsValues())
         {
             return $this->massDeleteTicketTemplate($this->getMassActionsValues());
         }
         return $this->deleteTicketTemplate($this->formData['ticketTemplateIdToDelete']);
    }

    public function loadTemplates()
    {
        return Manage::getTemplates();
    }
    
    public function redirectToEdit($templateId)
    {
        return (new ResponseTemplates\HtmlDataJsonResponse())
                ->setMessageAndTranslate('ticketTemplateAdded')
                ->setCallBackFunction('mgCustomRedirect')
                ->setData(["redirectUrl" => BuildUrl::getUrl('TicketTemplates', 'edit', ['templateId' => $templateId])]);
    }
    
    private function deleteTicketTemplate($id)
    {
        Manage::deleteTicketTemplate($id);
        TicketTemplateLangManage::deleteWhereTemplateId($id);
        return (new ResponseTemplates\HtmlDataJsonResponse())->setMessageAndTranslate('ticketTemplateDeleted')->setStatusSuccess();
    }
    
    private function massDeleteTicketTemplate($ticketTemplates)
    {
        foreach ($ticketTemplates as $id)
        {
            Manage::deleteTicketTemplate($id);
        }
        return (new ResponseTemplates\HtmlDataJsonResponse())->setMessageAndTranslate('ticketTemplatesDeleted')->setStatusSuccess();
    }
    
    
    private function loadDataToConfiguration()
    {
        $id = Request::getValue('templateId');
        if(!$id)
        {
            return;
        }
        $selects = ['department_id'];
        
        $ticketTemplate = Manage::getTicketTemplate($id);
        foreach($ticketTemplate as $key => $value)
        {
            if(in_array($key, $selects))
            {
                $this->data[$key]['value'] = $value;
                continue;
            }
            $this->data[$key] = $value;
        }
    }
    
}
