<?php

/**
 * @package KantPHP
 * @author  Zhenqiang Zhang <zhenqiang.zhang@hotmail.com>
 * @copyright (c) KantPHP Studio, All rights reserved.
 * @license http://www.opensource.org/licenses/BSD-3-Clause  The BSD 3-Clause License
 */
namespace Kant\Log;

use Kant\Kant;
use Kant\Foundation\Component;
use Kant\Helper\ArrayHelper;
use Kant\Helper\VarDumper;

class Target extends Component
{

    /**
     *
     * @var boolean whether to enable this log target. Defaults to true.
     */
    public $enabled = true;

    /**
     *
     * @var array list of message categories that this target is interested in. Defaults to empty, meaning all categories.
     *      You can use an asterisk at the end of a category so that the category may be used to
     *      match those categories sharing the same common prefix. For example, 'Kant\Database\*' will match
     *      categories starting with 'Kant\Database\', such as 'Kant\Database\Connection'.
     */
    public $categories = [];

    /**
     *
     * @var array list of message categories that this target is NOT interested in. Defaults to empty, meaning no uninteresting messages.
     *      If this property is not empty, then any category listed here will be excluded from [[categories]].
     *      You can use an asterisk at the end of a category so that the category can be used to
     *      match those categories sharing the same common prefix. For example, 'Kant\Database\*' will match
     *      categories starting with 'Kant\Database\', such as 'Kant\Database\Connection'.
     * @see categories
     */
    public $except = [];

    /**
     *
     * @var array list of the PHP predefined variables that should be logged in a message.
     *      Note that a variable must be accessible via `$GLOBALS`. Otherwise it won't be logged.
     *     
     *      Defaults to `['_GET', '_POST', '_FILES', '_COOKIE', '_SESSION', '_SERVER']`.
     *     
     *      Since version 2.0.9 additional syntax can be used:
     *      Each element could be specified as one of the following:
     *     
     *      - `var` - `var` will be logged.
     *      - `var.key` - only `var[key]` key will be logged.
     *      - `!var.key` - `var[key]` key will be excluded.
     *     
     * @see \Kant\Helper\ArrayHelper::filter()
     */
    public $logVars = [
        '_GET',
        '_POST',
        '_FILES',
        '_COOKIE',
        '_SESSION',
        '_SERVER'
    ];

    /**
     *
     * @var callable a PHP callable that returns a string to be prefixed to every exported message.
     *     
     *      If not set, [[getMessagePrefix()]] will be used, which prefixes the message with context information
     *      such as user IP, user ID and session ID.
     *     
     *      The signature of the callable should be `function ($message)`.
     */
    public $prefix;

    /**
     *
     * @var integer how many messages should be accumulated before they are exported.
     *      Defaults to 1000. Note that messages will always be exported when the application terminates.
     *      Set this property to be 0 if you don't want to export messages until the application terminates.
     */
    public $exportInterval = 1000;

    /**
     *
     * @var array the messages that are retrieved from the logger so far by this log target.
     *      Please refer to [[Logger::messages]] for the details about the message structure.
     */
    public $messages = [];

    private $_levels = 0;

    /**
     * Processes the given log messages.
     * This method will filter the given messages with [[levels]] and [[categories]].
     * And if requested, it will also export the filtering result to specific medium (e.g. email).
     * 
     * @param array $messages
     *            log messages to be processed. See [[Logger::messages]] for the structure
     *            of each message.
     * @param boolean $final
     *            whether this method is called at the end of the current application
     */
    public function collect($messages, $final)
    {
        $this->messages = array_merge($this->messages, static::filterMessages($messages, $this->getLevels(), $this->categories, $this->except));
        $count = count($this->messages);
        
        if ($count > 0 && ($final || $this->exportInterval > 0 && $count >= $this->exportInterval)) {
            if (($context = $this->getContextMessage()) !== '') {
                $this->messages[] = [
                    $context,
                    Logger::LEVEL_INFO,
                    'application',
                    KANT_BEGIN_TIME
                ];
            }
            // set exportInterval to 0 to avoid triggering export again while exporting
            $oldExportInterval = $this->exportInterval;
            $this->exportInterval = 0;
            $this->export();
            $this->exportInterval = $oldExportInterval;
            
            $this->messages = [];
        }
    }

    /**
     * Generates the context information to be logged.
     * The default implementation will dump user information, system variables, etc.
     * 
     * @return string the context information. If an empty string, it means no context information.
     */
    protected function getContextMessage()
    {
        $context = ArrayHelper::filter($GLOBALS, $this->logVars);
        $result = [];
        foreach ($context as $key => $value) {
            $result[] = "\${$key} = " . VarDumper::dumpAsString($value);
        }
        return implode("\n\n", $result);
    }

    /**
     *
     * @return integer the message levels that this target is interested in. This is a bitmap of
     *         level values. Defaults to 0, meaning all available levels.
     */
    public function getLevels()
    {
        return $this->_levels;
    }

    /**
     * Sets the message levels that this target is interested in.
     *
     * The parameter can be either an array of interested level names or an integer representing
     * the bitmap of the interested level values. Valid level names include: 'error',
     * 'warning', 'info', 'trace' and 'profile'; valid level values include:
     * [[Logger::LEVEL_ERROR]], [[Logger::LEVEL_WARNING]], [[Logger::LEVEL_INFO]],
     * [[Logger::LEVEL_TRACE]] and [[Logger::LEVEL_PROFILE]].
     *
     * For example,
     *
     * ```php
     * ['error', 'warning']
     * // which is equivalent to:
     * Logger::LEVEL_ERROR | Logger::LEVEL_WARNING
     * ```
     *
     * @param array|integer $levels
     *            message levels that this target is interested in.
     * @throws InvalidConfigException if an unknown level name is given
     */
    public function setLevels($levels)
    {
        static $levelMap = [
            'error' => Logger::LEVEL_ERROR,
            'warning' => Logger::LEVEL_WARNING,
            'info' => Logger::LEVEL_INFO,
            'trace' => Logger::LEVEL_TRACE,
            'profile' => Logger::LEVEL_PROFILE
        ];
        if (is_array($levels)) {
            $this->_levels = 0;
            foreach ($levels as $level) {
                if (isset($levelMap[$level])) {
                    $this->_levels |= $levelMap[$level];
                } else {
                    throw new InvalidConfigException("Unrecognized level: $level");
                }
            }
        } else {
            $this->_levels = $levels;
        }
    }

    /**
     * Filters the given messages according to their categories and levels.
     * 
     * @param array $messages
     *            messages to be filtered.
     *            The message structure follows that in [[Logger::messages]].
     * @param integer $levels
     *            the message levels to filter by. This is a bitmap of
     *            level values. Value 0 means allowing all levels.
     * @param array $categories
     *            the message categories to filter by. If empty, it means all categories are allowed.
     * @param array $except
     *            the message categories to exclude. If empty, it means all categories are allowed.
     * @return array the filtered messages.
     */
    public static function filterMessages($messages, $levels = 0, $categories = [], $except = [])
    {
        foreach ($messages as $i => $message) {
            if ($levels && ! ($levels & $message[1])) {
                unset($messages[$i]);
                continue;
            }
            
            $matched = empty($categories);
            foreach ($categories as $category) {
                if ($message[2] === $category || ! empty($category) && substr_compare($category, '*', - 1, 1) === 0 && strpos($message[2], rtrim($category, '*')) === 0) {
                    $matched = true;
                    break;
                }
            }
            
            if ($matched) {
                foreach ($except as $category) {
                    $prefix = rtrim($category, '*');
                    if (($message[2] === $category || $prefix !== $category) && strpos($message[2], $prefix) === 0) {
                        $matched = false;
                        break;
                    }
                }
            }
            
            if (!$matched) {
                unset($messages[$i]);
            }
        }
        return $messages;
    }

    /**
     * Formats a log message for display as a string.
     * 
     * @param array $message
     *            the log message to be formatted.
     *            The message structure follows that in [[Logger::messages]].
     * @return string the formatted message
     */
    public function formatMessage($message)
    {
        list ($text, $level, $category, $timestamp) = $message;
        $level = Logger::getLevelName($level);
        if (!is_string($text)) {
            // exceptions may not be serializable if in the call stack somewhere is a Closure
            if ($text instanceof \Exception) {
                $text = (string) $text;
            } else {
                $text = VarDumper::export($text);
            }
        }
        $traces = [];
        if (isset($message[4])) {
            foreach ($message[4] as $trace) {
                $traces[] = "in {$trace['file']}:{$trace['line']}";
            }
        }
        
        $prefix = $this->getMessagePrefix($message);
        return date('Y-m-d H:i:s', $timestamp) . " {$prefix}[$level][$category] $text" . (empty($traces) ? '' : "\n    " . implode("\n    ", $traces));
    }

    /**
     * Returns a string to be prefixed to the given message.
     * If [[prefix]] is configured it will return the result of the callback.
     * The default implementation will return user IP, user ID and session ID as a prefix.
     * 
     * @param array $message
     *            the message being exported.
     *            The message structure follows that in [[Logger::messages]].
     * @return string the prefix string
     */
    public function getMessagePrefix($message)
    {
        if ($this->prefix !== null) {
            return call_user_func($this->prefix, $message);
        }
        
        if (Kant::$app === null) {
            return '';
        }
        
        $ip = Kant::$app->request->ip();
        
        $session = Kant::$app->has('session', true) ? Kant::$app->get('session') : null;
        $sessionID = $session ? $session->getId() : '-';
        
        return "[$ip][$sessionID]";
    }
}
