<?php

namespace ModulesGarden\HostingQuotaNotifications\App\Helper\SmartyHelper;

use ModulesGarden\HostingQuotaNotifications\Core\Helper;
use \Illuminate\Database\Capsule\Manager as DB;
use ModulesGarden\HostingQuotaNotifications\App\Helper\DBModelsHelper;

/**
 * Description of Ticket
 *
 * @author Artur Pilch <artur.pi@modulesgarden.com>
 */
class Ticket
{
    private $data;
    private $output;

    public function getUserID()
    {
        return $this->data['userid'];
    }

    public function __construct($id, $last_replay = false)
    {
        //$this->langHelper = $langHelper;
        $model      = DB::table('tbltickets')
                ->join('tblticketdepartments', 'tblticketdepartments.id', '=', 'tbltickets.did')
                ->select("tbltickets.*", "tblticketdepartments.name AS departmentname")
                ->where('tbltickets.id', $id)
                ->first();
        $this->data = (array) $model;

        $this->data['replyid'] = 0;

        if ($last_replay)
        {
            $model = DB::table('tblticketreplies')
                    ->select("tblticketreplies.*")
                    ->where('tid', $this->data['id'])
                    ->orderBy('date', 'desc')
                    ->first();

            $replay = (array) $model;

            if (!empty($replay))
            {
                $this->data['replyid']        = $replay['id'];
                $this->data['replay_message'] = $replay['message'];
                $this->data['replay_date']    = $replay['date'];
            }
        }
    }

    public function getFormatedData()
    {
        $this->output                   = $this->data;
        $this->output['urgency']        = Helper\sl('lang')->T("supportticketsticketurgency" . strtolower($this->output['urgency']));
        $this->output['date']           = fromMySQLDate($this->output['date'], 0, 1);
        $this->output['replay_date']    = fromMySQLDate($this->output['replay_date'], 0, 1);
        $this->output['message']        = strip_tags($this->output['message']); //nl2br
        $this->output['replay_message'] = strip_tags($this->output['replay_message']); //nl2br

        return $this->output;
    }

    public function getMergeFields()
    {
        $map      = self::getMergeFieldsMap();
        $formated = $this->getFormatedData();
        $fields   = array();

        foreach ($map as $k => $v)
            $fields[$k] = $formated[$v];

        if ($formated['replayid'])
        {
            $fields['ticket_message']     = $formated['replay_message'];
            $fields['ticket_date_replay'] = $formated['replay_date'];
        }

        $fields['ticket_url']  = DBModelsHelper::getSystemURL() . ("/viewticket.php?tid=" . $this->data['id'] . "&c=" . $this->data['c']);
        $fields['ticket_link'] = $fields['ticket_url'];

        return $fields;
    }

    public static function getMergeFieldsNames()
    {
        return array_keys(self::getMergeFieldsMap());
    }

    private static function getMergeFieldsMap()
    {
        return [
            'ticket_id'                  => 'id',
            'ticket_reply_id'            => 'replyid',
            'ticket_department'          => 'departmentname',
            'ticket_date_opened'         => 'date',
            'ticket_subject'             => 'title',
            'ticket_message'             => 'message',
            'ticket_status'              => 'status',
            'ticket_priority'            => 'urgency',
            'ticket_tid'                 => 'tid',
            'ticket_url'                 => '',
            'ticket_link'                => '',
            'ticket_auto_close_time'     => '',
            'ticket_kb_auto_suggestions' => ''
        ];
    }
}
