<?php
/**
 * @package edoobox
 */

namespace Edoobox\Base;

use Edoobox\Helpers\Arr;
use Edoobox\Plugin;
use Edoobox\Requests\Auth;

class Bootstrap {
    private Auth $auth;
    public string $bootstrapName = '';
    public array $cachedBootstraps = [];
    public string $designSetting = 'standard';
    public string $hash = '';
    public string $optionKeyName = 'edoobox_cached_bootstraps';
    public array $settings = [];
    public string $version = '';

    public function __construct(string $name, string $version = '', string $hash = '') {
        $this->auth = new Auth();
        $this->bootstrapName = $name;
        $this->cachedBootstraps = Options::get($this->optionKeyName, []);
        $this->settings = Options::get(Settings::SETTINGS_OPTION_NAME, []);
        $this->designSetting = Arr::get($this->settings, 'design', 'standard');
        $this->hash = $hash;
        $this->version = $version;
    }

    public function enqueue(): void {

       // xdebug_break();
        if ( $this->bootstrapIsFetched() ) {
            $this->enqueueStyle();
            return;
        }
        $urlData = $this->fetchBootstrapUrlData();
        $url = Arr::get($urlData, 'data.cdn_url');
        if ( $url ) {
            $this->storeStyle($url);
            $this->enqueueStyle();
        }
    }

    private function bootstrapIsFetched(): bool {
        $bootstrap = Arr::get($this->cachedBootstraps, $this->bootstrapName);
        $url = Arr::get($bootstrap, 'url');
        return (bool)$url;
    }

    private function enqueueStyle(): void {
        $defaultUrl = Plugin::get('plugin_url') . 'css/bootstrap.min.css';
        $bootstrap = Arr::get($this->cachedBootstraps, $this->bootstrapName);
        $url = Arr::get($bootstrap, 'url');

        if ( $this->designSetting === 'bootstrap' ) {
            wp_enqueue_style('default_bootstrap', $defaultUrl, [], $this->hash, 'all');
        } else if ( $url ) {
            wp_enqueue_style($this->bootstrapName, $url, [], $this->hash, 'all');
        }
    }

    private function fetchBootstrapUrlData() {
        $baseUrl = BaseController::getApiBaseUrl();
        $environment = Settings::getSetting('environment', 'app1');
        $url = "https://$environment.edoobox.com/ajax/wp_css/$this->bootstrapName.css";
        if (Arr::get($this->settings, Settings::USE_V2) && $this->version > 4 && ! "50_edooboxstartstyle") {
            $this->auth->authenticate();
            $url = "$baseUrl/v2/bootstrap/$this->bootstrapName/wp-css";
        }
        $headers = [
            'headers' => [
                "edid" => $this->auth->getEdid(),
                "grant-type" => "access_token",
                "Authorization" => "Bearer " . $this->auth->getAuthToken()
            ],
            'timeout' => Settings::SETTINGS_REQUEST_TIMEOUT
        ];
        $response = wp_remote_get($url, $headers);
        return json_decode(Arr::get($response, 'body'), true);
    }

    private function storeStyle($url) {
        $this->cachedBootstraps[$this->bootstrapName] = [
            'cache_key' => $this->hash,
            'url' => $url
        ];
        Options::set($this->optionKeyName, $this->cachedBootstraps);
    }

    public static function getHash($apiResponse) {
        return Arr::get($apiResponse, 'api.data.header.bootstrap_hash', '');
    }

    public static function getName($apiResponse) {
        return Arr::get($apiResponse, 'api.data.header.bootstrap', '50_edooboxstartstyle');
    }

    public static function getVersion($apiResponse) {
        return Arr::get($apiResponse, 'api.data.header.bootstrap_version', '50_edooboxstartstyle');
    }
}