<?php


namespace Olegnax\Athlete2\Model;


use Closure;
use Exception;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\ProductMetadataInterface;
use Magento\Framework\Component\ComponentRegistrar;
use Magento\Framework\Component\ComponentRegistrarInterface;
use Magento\Framework\Filesystem\Directory\ReadFactory;
use Olegnax\Core\Helper\Helper;

class Api
{
    const THEME_PATH = 'frontend/Olegnax/athlete2';

    public static function activate(Client $a, Closure $b, $c)
    {
 


        return '';
    }

    private static function getMeta()
    {
        return [
            'user_ip' => static::getClientIp(),
            'magento_version' => static::getMagentoVersion(),
            'theme_version' => static::getThemeVersion(static::THEME_PATH),
        ];
    }

    public static function getClientIp()
    {
        return isset($_SERVER['HTTP_CLIENT_IP'])
            ? $_SERVER['HTTP_CLIENT_IP']
            : (
            isset($_SERVER['REMOTE_ADDR'])
                ? $_SERVER['REMOTE_ADDR']
                : (
            isset($_SERVER['REMOTE_HOST'])
                ? $_SERVER['REMOTE_HOST']
                : 'UNKNOWN'
            )
            );
    }

    private static function getMagentoVersion()
    {
        return ObjectManager::getInstance()->get(ProductMetadataInterface::class)->getVersion();
    }

    private static function getThemeVersion($a)
    {
        /** @var ComponentRegistrarInterface $b */
        $b = ObjectManager::getInstance()->get(ComponentRegistrarInterface::class);
        $c = $b->getPath(ComponentRegistrar::THEME, $a);
        if ($c) {
            /** @var ReadFactory $d */
            $d = ObjectManager::getInstance()->get(ReadFactory::class);
            $e = $d->create($c);
            if ($e->isExist('composer.json')) {
                $f = (string)$e->readFile('composer.json');
                $f = json_decode($f, true);
                if (isset($f['version'])) {
                    return $f['version'];
                }
            }
        }
        return '';
    }

    protected static function log($message, array $context = [])
    {
        $level = (int)(floor($message / 1000) * 100);
        static::_log($level, $message, $context);
    }

    protected static function _log($level, $message, array $context = [])
    {
        ObjectManager::getInstance()->get(Helper::class)->log($level, 'Athlete2', $message, $context);
    }

    public static function validate(
        Client $a,
        Closure $b,
        $c,
        $d = null,
        $f = false,
        $g = false,
        $h = 2,
        $i = '+1000000 hour'
    ) {
        // Prepare
        
        return true;
     
    }

    public static function deactivate(Client $a, Closure $b, $c, $d = null)
    {
        // Prepare
        $e = $b();
        if (!is_a($e, Request::class)) {
            static::log(3010);
            throw new Exception(' \Closure must return an object instance of Request.');
        }
        $e->updateVersion();
        // Call
        if (empty($d)) {
            $d = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'Unknown';
        }
        $e->request['domain'] = preg_replace('/^(www|dev)\./i', '', $d);
        $e->request['meta'] = static::getMeta();
        static::log(1000);
        $f = $a->call(base64_decode('bGljZW5zZV9rZXlfZGVhY3RpdmF0ZQ=='), $e);
  
        return $f;
    }

    public static function softValidate(Closure $a)
    {
       
      
        return true;
    }

    public static function check(
        Client $a,
        Closure $b,
        $c,
        $j,
        $g = false,
        $h = 2,
        $i = '+1000000 hour'
    ) {
        // Prepare
        
     return true;
    }
}
