<?php namespace Tussendoor\Reloadify\Helpers;

class Log
{
    protected $logger;
    protected $source;
    protected static $instance;

    /**
     * Pass any static calls to the underlying WC_Logger_Interface implementation. Inject the
     * given data into the context, which will be processed independently by a filter.
     * @param  string $name
     * @param  array  $arguments
     * @return bool
     */
    public static function __callStatic($name, $arguments)
    {
        $settings = ['source' => static::instance()->getSource()];

        if (!static::instance()->log()) {
            return false;
        }

        if (count($arguments) == 1) {
            return static::instance()->log()->$name(reset($arguments), $settings);
        }

        list($message, $data) = $arguments;
        $settings['wcReloadify'] = json_encode($data, JSON_PRETTY_PRINT);

        return static::instance()->log()->$name($message, $settings);
    }

    /**
     * Boot up the logger by setting the logger implementation and the source string.
     * @param \WC_Logger_Interface|null $logger
     * @param string|null              $source
     */
    public function boot(\WC_Logger_Interface $logger = null, $source = null)
    {
        if (!is_null(static::$instance)) {
            return true;
        }

        if (!$logger) {
            $logger = function_exists('wc_get_logger') ? wc_get_logger() : null;
        }

        $this->logger = $logger;
        $this->source = $source ? $source : 'reloadify';

        add_filter('woocommerce_format_log_entry', [$this, 'formatLogMessage'], 10, 2);

        static::$instance = $this;

        return true;
    }

    /**
     * Return an instance of this class.
     * @return WeFactLog
     */
    public static function instance()
    {
        return static::$instance;
    }

    public static function path()
    {
        return wc_get_log_file_path(static::instance()->getSource());
    }

    /**
     * Return an instance of the WC_Logger_Interface implementation.
     * @return WC_Logger_Interface
     */
    public function log()
    {
        return $this->logger;
    }

    /**
     * Return the source string. Used by WooCommerce to separate logs.
     * @return string
     */
    public function getSource()
    {
        return $this->source;
    }

    /**
     * Format the entry string of a logmessage. Only does something if wcReloadify is set
     * in the given $context variable. Appends the given data as a json string.
     * @param  string $entry
     * @param  array  $context
     * @return string
     */
    public function formatLogMessage($entry, $context)
    {
        if (!isset($context['context']['wcReloadify'])) {
            return $entry;
        }

        list($timestamp, $severity) = explode(' ', $entry);

        $entry = str_replace(
            [$timestamp, $severity],
            ['[' . $timestamp . ']', '[' . $severity . ']'],
            $entry
        );

        if (!empty($context['context']['wcReloadify'])) {
            $entry .= "\n" . $context['context']['wcReloadify'];
        }

        return $entry;
    }
}
