<?php

namespace ModulesGarden\HostingQuotaNotifications\Core\UI\ResponseTemplates;

use \ModulesGarden\HostingQuotaNotifications\Core\Helper;
use \ModulesGarden\HostingQuotaNotifications\Core\ServiceLocator;

/**
 *  Abstract Ajax Response Model
 */
abstract class Response
{
    const STATUS_SUCCESS      = 'success';
    const STATUS_ERROR        = 'error';
    protected $status   = self::STATUS_SUCCESS;
    protected $data     = [];
    protected $message  = null;
    protected $dataType = 'data';
    protected $callBackFunction = null;

    protected $lang = null;

    public function __construct($data = [])
    {
        $this->data = $data;
    }

    public function setStatusSuccess()
    {
        $this->status = self::STATUS_SUCCESS;

        return $this;
    }

    public function setStatusError()
    {
        $this->status = self::STATUS_ERROR;

        return $this;
    }

    public function setData($data)
    {
        $this->data = $data;

        return $this;
    }

    public function addData($key, $data)
    {
        $this->data[$key] = $data;

        return $this;
    }
    
    public function setCallBackFunction($name)
    {
        $this->callBackFunction = $name;
        
        return $this;
    }
    
    public function disableCallBackFunction()
    {
        $this->callBackFunction = null;
        
        return $this;
    }

    public function getData()
    {
        $return = [
            'status'        => $this->status,
            'message'       => $this->message,
            $this->dataType => $this->data
        ];
        if ($this->callBackFunction)
        {
            $return['callBackFunction'] = $this->callBackFunction;
        }
        
        return $return;
    }

    public function getFormatedResponse()
    {
        return Helper\json($this->getData())->setStatusCode(200);
    }
    
    public function setMessage($message)
    {
        $this->message = $message;
        
        return $this;
    }
    
    protected function loadLang()
    {
        if($this->lang === null)
        {
            $this->lang = ServiceLocator::call('lang');
        }
    }
    
    public function setMessageAndTranslate($message)
    {
        $this->loadLang();
        
        $this->message = $this->lang->absoluteT($message);
           
        return $this;
    }    
}
