<?php

use function PHPSTORM_META\type;


  $weblist_file=file_get_contents(JSON_WEB_LIST);
  $weblist_json=json_decode($weblist_file, true);
  if (file_exists(JSON_WEB_CHECK)){
    $webcheck_file=file_get_contents(JSON_WEB_CHECK);
    $webcheck_json=json_decode($webcheck_file, true);
  }

  //var_dump($webcheck_json);

  function checktoword($wordtochange){
    if ( $wordtochange == "1"){
      echo "OK";
    }else{
      echo "PROBLEM";
    }
  }

function status_class_bg($ssl, $word, $expiry, $retcol=null, $showday="null"){

    // CSS CLASS
    $red = "alert_red";
    $orange = "alert_orange";
    $yellow = "alert_yellow";
    $green = "alert_green";

    // CSS COLOR
    $col_red = "#ff4747";
    $col_orange = "#ff830f";
    $col_yellow = "#ffd70f";
    $col_green = "#42c500";
    $style="style='font-weight:bold; color:";
    

    $days = ssl_days_left($expiry);

    if ($showday == "on"){
      return $days;
    }

    if ($ssl != 1 || $word != 1) {
      echo $red;
      return;
    }

      

    if ($days !== null) {

        if ($days <= SSL_CRITICAL_DAYS) {
          
        if ($retcol){          // CRITICAL
          echo $style . $col_orange . ";'";
        }else{
          echo $orange;
          return;
        }
        }elseif ($days <= SSL_WARNING_DAYS) {
            
        if ($retcol){
          echo $style . $col_yellow . ";'";
          
        }else{
          echo $yellow;
          return;
        }
        }
    }

    
    if ($retcol){
      echo $style . $col_green . ";'";
      return ;
    }else{
      echo $green;
      return;
    }


    
}


  function word_col_status($input){
    $col="#f7ce5f";
    $style = " style='color: ". $col . "; text-decoration: underline; text-shadow: 0 0 0 yellow; animation: bumping 3s linear 2s infinite;'  ";

    if ($input != 1){
      echo $style;
    }
  }
  
  function get_checked_list(){
    global $webcheck_json;

    // Ordina: problemi prima
    usort($webcheck_json, function($a, $b) {

    $a_problem = ($a['sslcheck_ok'] != 1 || $a['wordcheck_ok'] != 1);
    $b_problem = ($b['sslcheck_ok'] != 1 || $b['wordcheck_ok'] != 1);

    // Se A ha problema e B no → A prima
    if ($a_problem && !$b_problem) return -1;

    // Se B ha problema e A no → B prima
    if (!$a_problem && $b_problem) return 1;

    // Se entrambi uguali → mantieni ordine
    return 0;
    });
   
    foreach($webcheck_json as $check_element){

      //VARIABLES
      $host=$check_element['host'];
      $domain=$check_element['domain'];
      $path=$check_element['path'];
      $checkword_ok=$check_element['wordcheck_ok'];
      $sslcheck_ok=$check_element['sslcheck_ok'];
      $wordtocheck=$check_element['wordtocheck'];
      $ssl_released=$check_element['ssl-released'];
      $ssl_expiry=$check_element['ssl-expiry'];
      $ssl_company=$check_element['ssl-company'];
      $http_error=$check_element['content_http_code'];
      $expirying_days=status_class_bg($sslcheck_ok, $checkword_ok, $ssl_expiry, false, 'on');
      $col_red = "#ff4747";
      $col_orange = "#ff830f";
      $col_yellow = "#ffd70f";
      $col_green = "#42c500";

      if($expirying_days < 0){
        $expiry_col='style="font-weight:bold; color:' . $col_red . ';"';
      }elseif($expirying_days <= SSL_CRITICAL_DAYS ){
        $expiry_col='style="font-weight:bold; color:' . $col_orange . ';"';
      }elseif($expirying_days <= SSL_WARNING_DAYS){
        $expiry_col='style="font-weight:bold; color:' . $col_yellow . ';"';
      }else{
        $expiry_col='style="font-weight:bold; color:' . $col_green . ';"';
      }

      if ($path == ""){
        $path = "/";
      }

    ?>
      <div class="alert-container <?php status_class_bg($sslcheck_ok, $checkword_ok, $ssl_expiry)?>">
        <div class="alert-info">
          <div><b>HOST: </b><span><?= $host ?></span></div>
          <div><b>DOMAIN: </b><span><?= $domain ?></span></div>
          <div <?php word_col_status($checkword_ok) ?>><b>CONTENT: </b><span><?= checktoword($checkword_ok) ?></span></div>
          <div <?php word_col_status($sslcheck_ok) ?>><b>SSL: </b><span><?= checktoword($sslcheck_ok) ?></span></div>
        </div>
        <div class="alert-details">
          <div><b>WORD TO CHECK: </b> <span><?= $wordtocheck ?></span></div>
          <div><b>SSL RELEASED: </b> <span><?= $ssl_released ?></span></div>
          <div><b>SSL EXPIRY: </b> <span <?= $expiry_col ?>> <?= $ssl_expiry ?></span></div>
          <div><b>SSL DAYS LEFT: </b> <span <?= $expiry_col ?>><?= $expirying_days ?></span></div>
          <div><b>SSL COMPANY: </b> <span> <?= $ssl_company ?></span></div>
          <div><b>PATH: </b> <span><?= $path ?></span></div>
          <div><b>ERROR CODE: </b> <span><?= $http_error ?></span></div>
          
        </div>
      </div>
    
    <?php
    }
  }

  function get_totals($type){
    global $webcheck_json;
    
    $totaldomain=0;
    $totalok=0;
    $totalssl_p=0;
    $totalcheck_p=0;
    
    foreach($webcheck_json as $i => $element){
     
      // Total domain to check
      $totaldomain ++;

      // Total fine domain
      if ($element['sslcheck_ok'] == 1 && $element['wordcheck_ok'] == 1){
        $totalok ++;
      }

      // Total SSL Problem
      if ($element['sslcheck_ok'] !== 1){
        $totalssl_p ++;
      }

      // Total Word-check Problem
      if ($element['wordcheck_ok'] !== 1){
        $totalcheck_p ++;
      }


    }

    switch($type){
        case "total":
          echo $totaldomain;
          break;
        
        case "ok":
          echo $totalok;
          break;

        case "ssl":
          echo $totalssl_p;
          break;

        case "word":
          echo $totalcheck_p;
          break;
      }
    
  }

  function ssl_days_left($expiry_date) {

    if (empty($expiry_date)) return null;

    $expiry = DateTime::createFromFormat('d/m/Y', $expiry_date);
    if (!$expiry) return null;

    $now = new DateTime();

    $interval = $now->diff($expiry);
    $days = (int)$interval->format('%r%a'); // giorni con segno

    return $days;
  }


?>