<?php

namespace Sibs2;

require_once __DIR__ . '/../external' .
            DIRECTORY_SEPARATOR . 'sodium_compat-1.7.0' .
            DIRECTORY_SEPARATOR . 'autoload.php';

class sibsPayments
{
    public function __construct()
    {
        // code...
    }

    public static function init()
    {
        return SIBS_VERSION ?? '1.0.0';
    }

    public static function saveData($post)
    {
        $sibs_stg_general_merchant_no                      = sanitize_text_field($post[sibsConstants::SIBS_PLUGIN_PREFIX . '_general_merchant_no']);
        $sibs_stg_general_bearer_token                     = sanitize_text_field($post[sibsConstants::SIBS_PLUGIN_PREFIX . '_general_bearer_token']);
        $sibs_stg_general_client_id                        = sanitize_text_field($post[sibsConstants::SIBS_PLUGIN_PREFIX . '_general_client_id']);
        $sibs_stg_general_environment                      = sanitize_text_field($post[sibsConstants::SIBS_PLUGIN_PREFIX . '_general_environment']);
        $sibs_stg_general_webhook_secret                   = sanitize_text_field($post[sibsConstants::SIBS_PLUGIN_PREFIX . '_general_webhook_secret']);

        if (empty(get_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_general_merchant_no'))) {
            delete_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_general_merchant_no');
            add_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_general_merchant_no', $sibs_stg_general_merchant_no, '', '');
        } else {
            update_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_general_merchant_no', $sibs_stg_general_merchant_no);
        }

        if (empty(get_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_general_bearer_token'))) {
            delete_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_general_bearer_token');
            add_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_general_bearer_token', $sibs_stg_general_bearer_token, '', '');
        } else {
            update_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_general_bearer_token', $sibs_stg_general_bearer_token);
        }

        if (empty(get_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_general_client_id'))) {
            delete_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_general_client_id');
            add_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_general_client_id', $sibs_stg_general_client_id, '', '');
        } else {
            update_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_general_client_id', $sibs_stg_general_client_id);
        }

        if (empty(get_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_general_environment'))) {
            delete_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_general_environment');
            add_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_general_environment', $sibs_stg_general_environment, '', '');
        } else {
            update_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_general_environment', $sibs_stg_general_environment);
        }

        if (empty(get_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_general_webhook_secret'))) {
            delete_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_general_webhook_secret');
            add_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_general_webhook_secret', $sibs_stg_general_webhook_secret, '', '---');
        } else {
            update_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_general_webhook_secret', $sibs_stg_general_webhook_secret);
        }

        return 'true';
    }

    public static function saveDataStyleSettings($post)
    {
        $sibs_stg_style_layout                = sanitize_text_field($post[sibsConstants::SIBS_PLUGIN_PREFIX . '_style_layout']);
        $sibs_stg_style_layout_primary        = sanitize_text_field($post[sibsConstants::SIBS_PLUGIN_PREFIX . '_style_layout_primary']);
        $sibs_stg_style_layout_surface        = sanitize_text_field($post[sibsConstants::SIBS_PLUGIN_PREFIX . '_style_layout_surface']);
        $sibs_stg_style_layout_text_color     = sanitize_text_field($post[sibsConstants::SIBS_PLUGIN_PREFIX . '_style_layout_text_color']);
        $sibs_stg_style_layout_header_color   = sanitize_text_field($post[sibsConstants::SIBS_PLUGIN_PREFIX . '_style_layout_header_color']);
        $sibs_stg_style_theme                 = sanitize_text_field($post[sibsConstants::SIBS_PLUGIN_PREFIX . '_style_theme']);
        $sibs_stg_style_theme_secondary_color = sanitize_text_field($post[sibsConstants::SIBS_PLUGIN_PREFIX . '_style_theme_secondary_color']);
        $sibs_stg_style_theme_background_color= sanitize_text_field($post[sibsConstants::SIBS_PLUGIN_PREFIX . '_style_theme_background_color']);
        $sibs_stg_style_theme_border_color    = sanitize_text_field($post[sibsConstants::SIBS_PLUGIN_PREFIX . '_style_theme_border_color']);
        $sibs_stg_style_theme_header_text     = sanitize_text_field($post[sibsConstants::SIBS_PLUGIN_PREFIX . '_style_theme_header_text']);
        $sibs_stg_style_font                  = sanitize_text_field($post[sibsConstants::SIBS_PLUGIN_PREFIX . '_style_font']);

        if (empty(get_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_layout'))) {
            delete_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_layout');
            add_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_layout', $sibs_stg_style_layout, '', 'default');
        } else {
            update_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_layout', $sibs_stg_style_layout);
        }

        if (empty(get_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_layout_primary'))) {
            delete_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_layout_primary');
            add_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_layout_primary', $sibs_stg_style_layout_primary, '', '');
        } else {
            update_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_layout_primary', $sibs_stg_style_layout_primary);
        }

        if (empty(get_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_layout_surface'))) {
            delete_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_layout_surface');
            add_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_layout_surface', $sibs_stg_style_layout_surface, '', '');
        } else {
            update_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_layout_surface', $sibs_stg_style_layout_surface);
        }

        if (empty(get_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_layout_text_color'))) {
            delete_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_layout_text_color');
            add_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_layout_text_color', $sibs_stg_style_layout_text_color, '', '');
        } else {
            update_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_layout_text_color', $sibs_stg_style_layout_text_color);
        }

        if (empty(get_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_layout_header_color'))) {
            delete_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_layout_header_color');
            add_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_layout_header_color', $sibs_stg_style_layout_header_color, '', '');
        } else {
            update_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_layout_header_color', $sibs_stg_style_layout_header_color);
        }

        if (empty(get_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_theme'))) {
            delete_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_theme');
            add_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_theme', $sibs_stg_style_theme, '', 'default');
        } else {
            update_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_theme', $sibs_stg_style_theme);
        }

        if (empty(get_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_theme_secondary_color'))) {
            delete_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_theme_secondary_color');
            add_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_theme_secondary_color', $sibs_stg_style_theme_secondary_color, '', '');
        } else {
            update_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_theme_secondary_color', $sibs_stg_style_theme_secondary_color);
        }

        if (empty(get_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_theme_background_color'))) {
            delete_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_theme_background_color');
            add_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_theme_background_color', $sibs_stg_style_theme_background_color, '', '');
        } else {
            update_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_theme_background_color', $sibs_stg_style_theme_background_color);
        }

        if (empty(get_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_theme_border_color'))) {
            delete_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_theme_border_color');
            add_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_theme_border_color', $sibs_stg_style_theme_border_color, '', '');
        } else {
            update_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_theme_border_color', $sibs_stg_style_theme_border_color);
        }

        if (empty(get_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_theme_header_text'))) {
            delete_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_theme_header_text');
            add_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_theme_header_text', $sibs_stg_style_theme_header_text, '', '');
        } else {
            update_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_theme_header_text', $sibs_stg_style_theme_header_text);
        }

        if (empty(get_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_font'))) {
            delete_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_font');
            add_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_font', $sibs_stg_style_font, '', '');
        } else {
            update_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_style_font', $sibs_stg_style_font);
        }

        return 'true';
    }

    public static function deprecationMessage($message, $isDeprecated = false)
    {
        $deprecated = __('deprecated', sibsConstants::SIBS_TRANSLATE);

        return ($isDeprecated) ? "<strong style=\"color:red;\">$message ($deprecated)</strong>" : "$message";
    }

    public static function php_version_deprecated()
    {
        return version_compare(phpversion(), sibsConstants::DEPRECATION_PHP_VERSION, '<');
    }

     public static function wordpress_version_deprecated()
     {
         return version_compare(get_bloginfo('version'), sibsConstants::DEPRECATION_WORDPRESS_VERSION, '<');
     }

     public static function woocommerce_version_deprecated()
     {
         if (class_exists('WooCommerce')) {
             return version_compare(WC()->version, sibsConstants::DEPRECATION_WOOCOMMERCE_VERSION, '<');
         }

         return false;
     }

     public static function sibsContains($string, $vocabulary)
     {
         if (str_contains($vocabulary, $string)) {
             return true;
         }

         return false;
     }

    public static function sodium_test_descryption()
    {
        $PHPSodiumAvailable        = false;
        $PHPSodiumCompactAvailable = false;
        $sodiumDescryption         = '';

        $sodiumShellResult = shell_exec('php --ri sodium');
        $sd                = self::sibsContains('not present', $sodiumShellResult);

        if ($sd == false) {
            $sodiumDescryption = '<pre class="pre_sibs"><strong>' . __('Sodium Extension', sibsConstants::SIBS_TRANSLATE) . ":</strong><span style='color:green;'> " . __('Available', sibsConstants::SIBS_TRANSLATE) . '</span></pre>';
        } else {
            $sodiumDescryption = '<pre class="pre_sibs"><strong>' . __('Sodium Extension', sibsConstants::SIBS_TRANSLATE) . ":</strong><span style='color:red;'> " . __('Not Available', sibsConstants::SIBS_TRANSLATE) . '</span></pre>';
        }

        $key_from_configuration    = '000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f';
        $iv_from_http_header       = '000000000000000000000000';
        $auth_tag_from_http_header = 'CE573FB7A41AB78E743180DC83FF09BD';
        $http_body                 = '0A3471C72D9BE49A8520F79C66BBD9A12FF9';

        $key         = self::sibs_convert_encode($key_from_configuration);
        $iv          = self::sibs_convert_encode($iv_from_http_header);
        $cipher_text = self::sibs_convert_encode($auth_tag_from_http_header, $http_body);

        if (! function_exists('sodium_crypto_aead_aes256gcm_is_available')) {
            $PHPSodiumAvailable        = sodium_crypto_aead_aes256gcm_is_available();
            $PHPSodiumAvailableMessage = $PHPSodiumAvailable ?
                '<span style="color:green;">' . __('Available', sibsConstants::SIBS_TRANSLATE) . '</span>' :
                '<span style="color:red;">' . __('Not Available', sibsConstants::SIBS_TRANSLATE) . '</span>';

            $sodiumDescryption .= '<p><strong>' . __('Sodium Server', sibsConstants::SIBS_TRANSLATE) . ":</strong> $PHPSodiumAvailableMessage</p>";
        }

        if (! $PHPSodiumAvailable && function_exists('\Sodium\crypto_aead_aes256gcm_is_available')) {
            $PHPSodiumCompactAvailable        = \Sodium\crypto_aead_aes256gcm_is_available();
            $PHPSodiumCompactAvailableMessage = $PHPSodiumCompactAvailable ?
                '<span style="color:green;">' . __('Available', sibsConstants::SIBS_TRANSLATE) . '</span>' :
                '<span style="color:red;">' . __('Not Available', sibsConstants::SIBS_TRANSLATE) . '</span>';

            $sodiumDescryption .= '<p><strong>' . __('Sodium Compact', sibsConstants::SIBS_TRANSLATE) . ":</strong> $PHPSodiumCompactAvailableMessage</p>";
        }

        if ($PHPSodiumAvailable) {
            $decrypted        = sodium_crypto_aead_aes256gcm_decrypt($cipher_text, '', $iv, $key);
            $decrypted_msg    = '';

            if ($decrypted !== false) {
                $decrypted_msg = json_decode($decrypted);
            }
            $decryptedMessage = ($decrypted !== false) ?
                '<span style="color:green;">' . ($decrypted_msg->type == 'PAYMENT' ? __('Available', sibsConstants::SIBS_TRANSLATE) : 'N/A') . '</span>' :
                '<span style="color:red;">' . __('Error', sibsConstants::SIBS_TRANSLATE) . '</span>';
            $sodiumDescryption .= '<p><strong>' . __('Decryption Test by Sodium Server', sibsConstants::SIBS_TRANSLATE) . ":</strong> $decryptedMessage</p>";
        }

        if ($PHPSodiumCompactAvailable) {
            $decrypted        = \Sodium\crypto_aead_aes256gcm_decrypt($cipher_text, '', $iv, $key);
            $decrypted_msg    = '';

            if ($decrypted !== false) {
                $decrypted_msg = json_decode($decrypted);
            }
            $decryptedMessage = ($decrypted !== false) ?
                '<span style="color:green;">' . ($decrypted_msg->type == 'PAYMENT' ? __('Available', sibsConstants::SIBS_TRANSLATE) : 'N/A') . '</span>' :
                '<span style="color:red;">' . __('Error', sibsConstants::SIBS_TRANSLATE) . '</span>';
            $sodiumDescryption .= '<p><strong>' . __('Decryption Test by Sodium Compact', sibsConstants::SIBS_TRANSLATE) . ":</strong> $decryptedMessage</p>";
        }

        return $sodiumDescryption;
    }

    public static function sibs_convert_encode($string, $http_body = null)
    {
        $to_encoding   = 'UTF-8';
        $from_encoding = 'BASE64';
        // $from_encoding = 'aes-256-gcm';

        // DPG: hex2bin - SPG: mb_convert_encoding
        if (is_null($http_body)) {
            if (ctype_xdigit($string)) {
                return hex2bin($string);
            }

            return mb_convert_encoding($string, $to_encoding, $from_encoding);
        } else {
            if (ctype_xdigit($http_body . $string)) {
                return hex2bin($http_body . $string);
            }

            return mb_convert_encoding($http_body, $to_encoding, $from_encoding) .
                mb_convert_encoding($string, $to_encoding, $from_encoding);
        }
    }

    public static function webhook_description()
    {
        $siteUrl            = get_option('home') . '/';
        $webhookEndpoint    = sibsConstants::SIBS_WEBHOOK_ENDPOINT;
        $webhookURL         = "<code>{$siteUrl}wp-json/$webhookEndpoint</code>";
        $webhookDescription = sprintf(
            __(
                "Place here your SIBS' WebHook Secret. <br> Provide this WebHook URL %s to your SIBS point of contact.",
                sibsConstants::SIBS_TRANSLATE
            ),
            $webhookURL
        );

        return $webhookDescription;
    }

    public static function version_description()
    {
        $system_date         = date('Y-m-d H:m:s');
        $php_version         = self::deprecationMessage(phpversion(), self::php_version_deprecated());
        $wordpress_version   = self::deprecationMessage(get_bloginfo('version'), self::wordpress_version_deprecated());
        $woocommerce_version = self::deprecationMessage(WC()->version, self::woocommerce_version_deprecated());
        $sibs_version        = constant('SIBS_VERSION');

        $versionDescryption = '<p class="version_description"><strong>' . __('PHP System Date', sibsConstants::SIBS_TRANSLATE) . ":</strong> $system_date</p>";
        $versionDescryption .= '<p class="version_description"><strong>' . __('PHP Version', sibsConstants::SIBS_TRANSLATE) . ":</strong> $php_version</p>";
        $versionDescryption .= '<p class="version_description"><strong>' . __('Wordpress Version', sibsConstants::SIBS_TRANSLATE) . ":</strong> $wordpress_version</p>";
        $versionDescryption .= '<p class="version_description"><strong>' . __('Woocommerce Version', sibsConstants::SIBS_TRANSLATE) . ":</strong> $woocommerce_version</p>";
        $versionDescryption .= '<p class="version_description"><strong>' . __('Sibs Plugin Version', sibsConstants::SIBS_TRANSLATE) . ":</strong> $sibs_version</p>";

        return $versionDescryption;
    }
}
