<?php

namespace CleantalkSP\SpbctWP\AdjustToEnvironmentModule;

class AdjustToEnvironmentSettings
{
    public static function render()
    {
        $view = '';
        $view_buttons = '';

        $view .= '<div class="spbc_wrapper_field" id="action-adjust-env">';
        $view__adjusted = __('We changed some settings of third party plugins to make compatibility. ', 'security-malware-firewall');
        $view__adjusted .= __('You are free to reverse it, but it may lead to incorrect operation of the software.', 'security-malware-firewall');
        $view__adjust_suggestion = __('We suggest you to change some settings of third party plugins to make compatibility. ', 'security-malware-firewall');

        $info = AdjustToEnvironmentHandler::getInfoWhatWeChanged();

        foreach ( AdjustToEnvironmentHandler::SET_OF_ADJUST as $_env_name => $class ) {
            $key_of_adjust = array_search($class, AdjustToEnvironmentHandler::SET_OF_ADJUST);

            if (!class_exists($class) || false === $key_of_adjust) {
                continue;
            }


            if (isset($info[$class]['changed'])) {
                $adjust_obj = new $class($info);

                if ($adjust_obj->isEnvComplyToAdjustRequires()) {
                    if ( $adjust_obj->hasEnvBeenAdjustedByModule() ) {
                            $view .= $view__adjusted;
                            $view_buttons .= self::renderReverseBtn($key_of_adjust);
                    } else {
                        $view .= $view__adjust_suggestion;
                        $view_buttons .= self::renderChangeBtn($key_of_adjust);
                    }
                }
            }
        }

        $view .= $view_buttons;
        $view .= '</div>';

        if ($view_buttons === '') {
            $view = '';
        }

        return $view;
    }

    private static function renderChangeBtn($key)
    {
        $btn = self::btnLayout();

        $title = '';
        $description = '';

        switch ($key) {
            case 'w3tc':
                $title = 'W3 Total Cache';
                $description = __('Next button will disable "Cache URIs with query string variables" option.', 'security-malware-firewall');
                break;
        }

        $replaces = [
            '{TITLE}' => sprintf(__("Make adjust for %s", 'security-malware-firewall'), $title),
            '{DESCRIPTION}' => $description,
            '{KEY}' => 'change-' . $key,
            '{ADJUST}' => $key,
        ];

        foreach ( $replaces as $place_holder => $replace ) {
            $replace = is_null($replace) ? '' : $replace;
            $btn = str_replace($place_holder, $replace, $btn);
        }

        return $btn;
    }

    private static function renderReverseBtn($key)
    {
        $btn = self::btnLayout();

        $title = '';
        $description = '';

        switch ($key) {
            case 'w3tc':
                $title = 'W3 Total Cache';
                $description = __('Next button will enable "Cache URIs with query string variables" option back.', 'security-malware-firewall');
                break;
        }

        $replaces = [
            '{TITLE}' => __("Reverse adjust for $title", 'security-malware-firewall'),
            '{DESCRIPTION}' => $description,
            '{KEY}' => 'reverse-' . $key,
            '{ADJUST}' => $key,
        ];

        foreach ( $replaces as $place_holder => $replace ) {
            $replace = is_null($replace) ? '' : $replace;
            $btn = str_replace($place_holder, $replace, $btn);
        }

        return $btn;
    }

    private static function btnLayout()
    {
        $btn = '<div class="spbc_settings_description"> {DESCRIPTION} </div>'
            . '<button type="button" id="action-adjust-{KEY}" data-adjust="{ADJUST}"'
            . 'class="button button-secondary" style="margin: 5px 0 0 10px;">'
            . '{TITLE}'
            . '</button>';

        return $btn;
    }
}
