<?php

$log_file_dir = __DIR__ . '/../../../../../../../var/log';

$objectManager   = \Magento\Framework\App\ObjectManager::getInstance();
$productMetadata = $objectManager->get('Magento\Framework\App\ProductMetadataInterface');

$storeManager = \Sibs\Sibs\DB\SibsDB::get('\Magento\Store\Model\StoreManagerInterface');
$domain       = $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);

//add first element
$options = '<option value="0">' . __('Choose Log File') . '</option>';

//files in log dir
$filesInDir = scandir($log_file_dir);

//log files content array
$logsArray = [0 => ''];

foreach ($filesInDir as $key => $value) {
  if (strpos($value, 'sibs_') !== false) {
    //read file
    $fileRead = file_get_contents("$log_file_dir/$value");
    //html encoded
    $logsArray[$key + 1] = nl2br($fileRead);

    // add to select
    $options .= '<option value="' . ($key + 1) . '">' . $value . '</option>';
  }
}

$select = '<select id="SibsLogsSelect" onchange="SibsLogsSelectFunction()">' . $options . '</select>';

$libSodiumAvailableMessage = '<br>' .
  __('We have detected that you have') . ' <font color="green"> ' . __('Libsodium enabled') . ' </font> ' .
  __("which will be able to run SIBS' asynchronous notifications");

$libSodiumUnavailableMessage = '<br>' .
  __('We have detected that you do not have') . ' <font color="red"> ' . __('Libsodium enabled') . ' </font> ' .
  __("which will not be able to run SIBS' asynchronous notifications");

$sodiumMessage = extension_loaded('sodium') ? $libSodiumAvailableMessage : $libSodiumUnavailableMessage;

$sibs_version   = $block->sibsVersion();
$sibs_client_id = $block->sibsClientId();
?>

<style>
  #SibsLogsSelectContent {
    font-size: 10px;
    border: 2px solid grey;
    width: 100%;
    border-left: 5px solid green;
    border-radius: 5px;
    padding: 14px;
    line-height: 5px;
    background-image: linear-gradient(180deg, #eee 50%, #fff 50%);
    background-size: 100% 20px;
    background-position: 0 12px;
    font-family: 'Open Sans';
  }
</style>

<script>
  function SibsLogsSelectFunction() {
    var x = document.getElementById("SibsLogsSelect").value;
    var tempArray = <?php echo json_encode($logsArray); ?>;
    document.getElementById("SibsLogsSelectContent").innerHTML = tempArray[x];
  }
</script>

<b><?php echo __('Browser Info') ?></b>
<?php echo $block->getBrowserInfo()->__toString(); ?>
<br>

<b> <?php echo __('Magento Version') ?>: <?php echo $productMetadata->getVersion(); ?> </b>
<br />
<b> <?php echo __('Plugin Version') ?>: <?php echo $sibs_version ?? 'N/A'; ?> </b>
<br />
<b> <?php echo __('Webhook Endpoint') ?> </b> <?php echo $domain . 'rest/V1/webhook' ?>
<br />
<b> <?php echo __('Sodium available?') ?> </b> <?php echo $sodiumMessage ?>
<br />
<br />
<link href="https://cdn.jsdelivr.net/npm/boxicons@latest/css/boxicons.min.css" rel="stylesheet" type="text/css" />
<link href="https://plugin-stargate.store/bug-report/src/assets/css/sibs-bug.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
  window.SIBS_PLUGIN_VERSION = "<?php echo $productMetadata->getVersion(); ?>";
  window.SIBS_VERSION = "<?php echo $sibs_version ?? 'N/A'; ?>";
  window.SIBS_PHP_VERSION = "<?php echo phpversion() ?>";
  window.SIBS_CLIENT = "<?php echo $sibs_client_id ?? '0' ?>";
  window.SIBS_FRAMEWORK = "magento";
  window.SIBS_PROJECT = "stargate";
  window.SIBS_URL_REPORT = "https://plugin-stargate.store/bug-report?plugin=" + SIBS_VERSION + "&plugin_version=" + SIBS_PLUGIN_VERSION + "&php_version=" + SIBS_PHP_VERSION + "&client=" + SIBS_CLIENT + "&framework=" + SIBS_FRAMEWORK + "&project=" + SIBS_PROJECT;
</script>
<script src='https://code.jquery.com/jquery-1.9.1.min.js'></script>
<script src='https://plugin-stargate.store/bug-report/src/assets/js/main.js' id='sibs_bug_report_script-js'></script>

<a href="#" id="sibs-open-modal" class="action-default scalable save primary ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only modal__button">
  <?php echo __('Report an Error') ?>
</a>
<br />
<br />
<b> <?php echo __('Select') ?>: </b> <?php echo $select ?>
<br />
<b><?php echo __('Logs') ?>: </b>
<br />
<pre id="SibsLogsSelectContent"></pre>
<br />
