<?php

namespace ModulesGarden\HostingQuotaNotifications\Core\Http;

use Symfony\Component\HttpFoundation\JsonResponse as OrgiJsonResponse;
use ModulesGarden\HostingQuotaNotifications\Core\ServiceLocator;

/**
 * Description of Json
 *
 * @author Rafał Ossowski <rafal.os@modulesgarden.com>
 */
class JsonResponse extends OrgiJsonResponse
{
    protected $lang;

    public function setLang($lang)
    {
        $this->lang = $lang;
        return $this;
    }

    public function getLang()
    {
        return $this->lang;
    }
    
    public function setData($data = array())
    {
		try
		{
			return parent::setData($data);
		}
		catch (\Exception $e)
        {
            return parent::setData($this->utf8_walk_convert($data));
        }
    }

	public function utf8_walk_convert($array)
	{
		array_walk_recursive($array, function(&$item, $key)
			{
				if (is_array($item) || is_object($item))
				{
					foreach ($item as &$param)
					{
						
						if (is_array($param) || is_object($param)) { $param = $this->utf8_walk_convert($param); }
						else {
							if(!mb_detect_encoding($param, 'utf-8', true)){
								$param = utf8_encode($param);
							}
						}
						
					}
				}
				else
				{
					if(!mb_detect_encoding($item, 'utf-8', true)){
						$item = utf8_encode($item);
					}
				}
			});

		return $array;
	}

	public function utf8_converter ($array)
	{
		array_walk_recursive($array, function(&$item, $key)
			{
				if(!is_object($item) && !mb_detect_encoding($item, 'utf-8', true)){
				    $item = utf8_encode($item);
				}
			});
		return $array;
	}

    public function getData()
    {
        $data = null;
        if (defined('HHVM_VERSION'))
        {
            // HHVM does not trigger any warnings and let exceptions
            // thrown from a JsonSerializable object pass through.
            // If only PHP did the same...
            $data = json_decode($this->data, true, 512, $this->encodingOptions);
        }
        else
        {
            try
            {
                // PHP 5.4 and up wrap exceptions thrown by JsonSerializable
                // objects in a new exception that needs to be removed.
                // Fortunately, PHP 5.5 and up do not trigger any warning anymore.
                $data = json_decode($this->data, true, 512, $this->encodingOptions);
            }
            catch (\Exception $e)
            {
                if ('Exception' === get_class($e) && 0 === strpos($e->getMessage(), 'Failed calling '))
                {
                    throw $e->getPrevious() ?: $e;
                }
                ServiceLocator::call('errorManager')->addError(self::class, $e->getMessage(), $e->getTrace());
            }
        }

        if (JSON_ERROR_NONE !== json_last_error())
        {
            throw new \InvalidArgumentException(json_last_error_msg());
        }

        return $data;
    }

    public function withSuccess($message = '')
    {
        $data            = $this->getData();
        $data['status']  = 'success';
        $data['message'] = $message;

        $this->setData($data);

        return $this;
    }

    public function withError($message = '')
    {
        $data            = $this->getData();
        $data['status']  = 'success';
        $data['message'] = $message;

        $this->setData($data);

        return $this;
    }
}
