<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace ModulesGarden\HostingQuotaNotifications\App\Libs\Cron\Notifications\Actions;

use \ModulesGarden\HostingQuotaNotifications\App\Helper\Api;
use \ModulesGarden\HostingQuotaNotifications\App\Helper\Logger\Log;
use ModulesGarden\HostingQuotaNotifications\App\Helper\Models\Admin;
use ModulesGarden\HostingQuotaNotifications\App\Helper\Models\TicketTemplates;
use \ModulesGarden\HostingQuotaNotifications\App\Libs\Notifications\Manage as NotificationManage;
use \ModulesGarden\HostingQuotaNotifications\App\Libs\TicketTemplates\Manage as TicketTemplatesManage;
use \ModulesGarden\HostingQuotaNotifications\App\Helper\Models\User as UserModelHelper;
use \ModulesGarden\HostingQuotaNotifications\App\Helper\Models\Hosting as HostingModelHelper;
use ModulesGarden\HostingQuotaNotifications\App\Helper\Notifications\OpenTicket as OpenTicketHelper;
use ModulesGarden\HostingQuotaNotifications\App\Helper\Models\Hosting;

/**
 * Description of OpenTicket
 *
 * @author Artur Pilch <artur.pi@modulesgarden.com>
 */
class OpenTicket extends Base
{
    CONST OPEN_TICKET = 'OpenTicket';
    CONST UPDATE_TICKET = 'UpdateTicket';
    const STATUS_OPEN = 'Open';

    private $alreadyOpen = false;

    public function run()
    {
        if ($this->data['ticket_template'] == 'none')
        {
            return 0;
        }

        $openedTicket = NotificationManage::isTicketOpen($this->data['hostingId'], $this->data['kind'], $this->data['type'], $this->data['groupId']);

        if ($openedTicket)
        {
            return $openedTicket;
        }

        $params = $this->getData();


        if (!$params)
        {
            Log::log('NoLangToOpenTicket', [], ['result' => true], $this->data);
            return;
        }

        return $this->open($params);
    }

    public function open($params)
    {
        $result = Api::apiCall(SELF::OPEN_TICKET, $params, $params['name']);
        if($result['id'])
        {
            $this->setAlreadyOpen(true);
            $ticket_template = TicketTemplatesManage::getTicketTemplate($this->data['ticket_template']);
            Api::apiCall(SELF::UPDATE_TICKET, [
                'ticketid' => $result['id'],
                'flag' => $ticket_template['admin_id'],
                'status' => $this->data['ticket_status'] ? : self::STATUS_OPEN
            ]);
        }
        Log::log(SELF::OPEN_TICKET, $this->data, $result, $this->data);
        return isset($result['id']) ? $result['id'] : 0;
    }

    public function getData()
    {

        $userId   = HostingModelHelper::getValueById($this->data['hostingId'], 'userid');
        $userLang = UserModelHelper::getUserValueById($userId, 'language');

        $ticket_template = TicketTemplatesManage::getTicketTemplate($this->data['ticket_template']);

        $ticketLanguage  = TicketTemplates::getTicketLanguage($userLang, $ticket_template);

        $message = OpenTicketHelper::assignValues($ticketLanguage['message'], $this->data);
        if (!$ticketLanguage)
        {
            return null;
        }

        $admin  = Admin::get($ticket_template['admin_id']);

        return [
            'clientid'  => $userId,
            'deptid'    => $ticket_template['department_id'],
            'subject'   => $ticketLanguage['subject'],
            'serviceid' => $this->data['hostingId'],
            'admin'     => true,
            'name'      => $admin->username,
            'email'     => $admin->email,
            'message'   => $message,
        ];
    }

    /**
     * @return mixed
     */
    public function isAlreadyOpen()
    {
        return $this->alreadyOpen;
    }

    /**
     * @param mixed $alreadyOpen
     */
    public function setAlreadyOpen($alreadyOpen)
    {
        $this->alreadyOpen = $alreadyOpen;
    }
}
