<?php
namespace Fifa\ConnectId\Command\Helpers;

use Psr\Log\LoggerInterface;

/**
 * Class FileLogger
 * @package Fifa\ConnectId\Command\Helpers
 */
class FileLogger implements LoggerInterface
{
    /**
     * @var string
     */
    private $filePath;

    /**
     * @var string
     */
    private $clientName;

    /**
     * @var bool
     */
    private $includeContext;

    /**
     * FileLogger constructor.
     * @param string $filePath
     * @param $clientName
     * @param bool $includeContext
     */
    public function __construct($filePath, $clientName, $includeContext = true)
    {
        $this->filePath = $filePath;
        $this->clientName = $clientName;
        $this->includeContext = $includeContext;
    }

    /**
     * System is unusable.
     *
     * @param string $message
     * @param array $context
     * @return null
     */
    public function emergency($message, array $context = array())
    {
        $this->log('EMERGENCY', $message, $context);
    }

    /**
     * Action must be taken immediately.
     *
     * Example: Entire website down, database unavailable, etc. This should
     * trigger the SMS alerts and wake you up.
     *
     * @param string $message
     * @param array $context
     * @return null
     */
    public function alert($message, array $context = array())
    {
        $this->log('ALERT', $message, $context);
    }

    /**
     * Critical conditions.
     *
     * Example: Application component unavailable, unexpected exception.
     *
     * @param string $message
     * @param array $context
     * @return null
     */
    public function critical($message, array $context = array())
    {
        $this->log('CRITICAL', $message, $context);
    }

    /**
     * Runtime errors that do not require immediate action but should typically
     * be logged and monitored.
     *
     * @param string $message
     * @param array $context
     * @return null
     */
    public function error($message, array $context = array())
    {
        $this->log('ERROR', $message, $context);
    }

    /**
     * Exceptional occurrences that are not errors.
     *
     * Example: Use of deprecated APIs, poor use of an API, undesirable things
     * that are not necessarily wrong.
     *
     * @param string $message
     * @param array $context
     * @return null
     */
    public function warning($message, array $context = array())
    {
        $this->log('WARNING', $message, $context);
    }

    /**
     * Normal but significant events.
     *
     * @param string $message
     * @param array $context
     * @return null
     */
    public function notice($message, array $context = array())
    {
        $this->log('NOTICE', $message, $context);
    }

    /**
     * Interesting events.
     *
     * Example: User logs in, SQL logs.
     *
     * @param string $message
     * @param array $context
     * @return null
     */
    public function info($message, array $context = array())
    {
        $this->log('INFO', $message, $context);
    }

    /**
     * Detailed debug information.
     *
     * @param string $message
     * @param array $context
     * @return null
     */
    public function debug($message, array $context = array())
    {
        $this->log('DEBUG', $message, $context);
    }

    /**
     * Logs with an arbitrary level.
     *
     * @param mixed $level
     * @param string $message
     * @param array $context
     * @return null
     */
    public function log($level, $message, array $context = array())
    {
        $this->dumpMessage($level, $message);
        $this->dumpContext($level, $context);
    }

    /**
     * @param string $level
     * @param array $context
     */
    private function dumpContext($level, array $context)
    {
        if (!empty($context) && $this->includeContext) {
            $this->dumpMessage($level, self::jsonEncodeContext($context));
        }
    }

    /**
     * @param string $level
     * @param string $message
     */
    private function dumpMessage($level, $message)
    {
        file_put_contents($this->filePath, sprintf("[%s][%s][%s] %s\n", $this->clientName, date('Y-m-d H:i:s'), $level, $message), FILE_APPEND);
    }

    /**
     * @param array $context
     */
    private function jsonEncodeContext(array $context)
    {
        foreach($context as &$value) {
            if($value instanceof \Exception)
            {
                $value = $value->__toString();
            }
        }
        return json_encode($context);
    }
}