<?php
namespace Helpers\Intuition;

// phpcs:ignoreFile
class ControlConfigIntuition
{

    private $files;

    private $error;

    private $pdo;

    public function __construct($files, $pdo)
    {
        $this->files = $files;
        $this->pdo = $pdo;
        $this->error = null;
    }

    public function CheckAllFiles()
    {
        foreach ($this->files as $file => & $table) {
            if (! $this->CheckFile($file, $table)) {
                return false;
            }
        }
        
        return true;
    }

    public function CheckFile($file, $table)
    {
        if (($cfg = $this->ParseCfgIntuition($file)) != false) {
            $MoteurSinequa = new BaseSynequa($this->pdo);
            if ($MoteurSinequa->connect()) {
                $query = "SELECT * FROM {$table} LIMIT 1";
                if ($query_result = $MoteurSinequa::$the_session->in_query($query)) {
                    $row = $query_result->in_fetch_row();
                    if (! is_array($row) || count($row) == 0) {
                        $this->error = "Table " . $table . " vide, test non fait";
                        return null;
                    } else {
                        foreach ($cfg['columns'] as $key => & $value) {
                            if (! array_key_exists(strtolower($value[0]), $row)) {
                                $this->error = "Colonne manquante : " . $value[0] . " dans la table : " . $table;
                                return false;
                            }
                        }
                    }
                } else {
                    $this->error = 'Une erreur est survenue lors de la requette a la base intuition';
                    return false;
                }
                // $MoteurSinequa->disconnect();
            } else {
                $this->error =  'L\'application a rencontre un probleme de connexion a la base intuition';
                return false;
            }
        } else {
            return false;
        }
        
        return true;
    }

    public function CheckDistribution()
    {
        $a = chr(1);
        $b = chr(2);
        foreach ($this->files as $file => & $table) {
            if (($cfg = $this->ParseCfgIntuition($file)) != false) {
                $query = "SELECT ";
                foreach ($cfg['columns'] as $key => & $value) {
                    if (strpos($value[1], '/cal') !== false) {
                        $query .= "distribution('{$value[0]},order=freqdesc,separator={$a},innerseparator={$b}') as {$value[0]},";
                    }
                }
            } else {
                return false;
            }
            $query[strlen($query) - 1] = ' ';
            $query .= "FROM " . $_SESSION['INTUITION_CAT'] . " LIMIT 1";
            $MoteurSinequa = new BaseSynequa($this->pdo);
            if ($MoteurSinequa->connect()) {
                if (! ($query_result = $MoteurSinequa::$the_session->in_query($query))) {
                    $this->error =  \Helpers\Trad\Trad::strBuild("Il y a un probleme avec les distributions ('@1@' => @2@)", $file, $table);
                    return false;
                }
//                 $MoteurSinequa->disconnect();
            } else {
                $this->error =  "L'application a rencontre un probleme de connexion a la base intuition";
                return false;
            }
        }
        return true;
    }

    public function GetLastError()
    {
        return $this->error;
    }

    public function &GetFiles()
    {
        return $this->files;
    }

    private function ParseCfgIntuition($file)
    {
        $cfg = array();
        $handle = @fopen($file, 'r');
        if ($handle) {
            $cfg = array();
            while (! feof($handle)) {
                $buffer = fgets($handle);
                if (strpos($buffer, '--') === 0) {
                    $tab = explode('=', $buffer);
                    $key = substr($tab[0], 2);
                    $cfg[$key][] = isset($tab[1]) ? explode(':', $tab[1]) : null;
                }
            }
            return $cfg;
        }
        $this->error = "Impossible d'ouvrir le fichier : " . $file;
        return false;
    }
}
