<?php

namespace Modules\Additional\Specifique\Client92\Html;

use Helpers\Trad\ITrad;
use Modules\Additional\Specifique\Client92\Modules\Controller\SpecNAOSController;
use Psr\Container\ContainerInterface;
use Helpers\Trad\Trad;
use TwigExtension\OutilExtension;
use Modules\API\APIExterne\TwigExtension\OutilAPIExtension;
use Skeleton\Router;

class HtmlExtension extends \Twig\Extension\AbstractExtension
{

    /**
     * @var Router $router
     */
    private Router $router;
    private ContainerInterface $contenaire;
    private \PDO $pdo;
    private ITrad $trad;
    
    public function __construct(Router $router, ContainerInterface $contenaire, \PDO $pdo)
    {
        $this->router = $router;
        $this->pdo = $pdo;
        $this->contenaire = $contenaire;
        $this->trad = $contenaire->get(ITrad::class);
    }

    public function getFunctions()
    {
        return [
            new \Twig\TwigFunction('specifiqueBVD', [
                $this,
                'specifiqueBVD'
            ], [
                'is_safe' => [
                    'html'
                ]
            ])
        ];
    }
        
    /**
     * specifiqueBVD
     *
     * @param array<mixed> $array
     * @return string
     */
    public function specifiqueBVD($array) : string
    {
        $trad = $this->trad;
        
        $specNAOSController             = new SpecNAOSController(
            $this->contenaire->get(\Skeleton\Renderer\RendererInterface::class),
            $this->contenaire->get(\PDO::class),
            $this->contenaire->get(\Skeleton\Router::class),
            $this->contenaire->get(\Psr\Container\ContainerInterface::class)
        );
        $extensionTool                  = new OutilExtension($this->pdo, $this->contenaire, $this->router);
        $extensionAPITool               = new OutilAPIExtension($this->pdo, $this->contenaire, $this->router);

        

        $year_last_accounts             = $array['profilFinancier']->YEAR_LAST_ACCOUNTS;
        $pF_curr                        = $array['profilFinancier']->CURR;
        $pF_solvr                       = $array['profilFinancier']->SOLR;

        $vF_crif                        = $array['valueFrs']->CRIF_SCORE;
        $vF_more                        = $array['valueFrs']->MORRAT;
        $vF_compass                     = $array['valueFrs']->G2Scores_CompassScore;
        $vF_trucost                     = $array['valueFrs']->TRUCOST_SCORE_ENVIRSCORE;
        $vF_wkca                        = $array['valueFrs']->WKCA;
        $vF_gear                        = $array['valueFrs']->GEAR;
        $vF_var_3                       = $array['valueFrs']->VAR3;

        $all_tooltips                   = $specNAOSController->getTooltipsForNAOS();


        $html                           = '   
            <hr>
            <table id="specNaosTable" class="table">
                <thead>
                    <th scope="col" width="25%">'. $trad->t('Type de ratio') .'</th>
                    <th scope="col" width="15%" align="right" colspan="2">31/12/'.$year_last_accounts.' EUR '.$trad->t('12 Mois local GAAP').'</th>
                    <th scope="col" width="15%" align="right" colspan="2">31/12/'.intval($year_last_accounts - 1).' EUR '.$trad->t('12 Mois local GAAP').'</th>
                    <th scope="col" width="15%" align="right" colspan="2">31/12/'.intval($year_last_accounts - 2).' EUR '.$trad->t('12 Mois local GAAP').'</th>
                </thead>
                <tbody>
                    <tr>
                        <td align="left" class="td_indicator"><span class="indic_label">'.$trad->t('Ratio de liquidité') . $all_tooltips['ratio_liquidite'] .'</span> <span style="border-radius:15px;height:15px;width:15px;" class="indic_span '.$specNAOSController->indicatorForRL($pF_curr[0]).'"></span></td>
                        <td align="right">'.$extensionTool->tva($pF_curr[0]). '</td>
                        <td class="second_col_trend">'.$extensionAPITool->tendance('+', $extensionTool->convertFloat($pF_curr[0]), $extensionTool->convertFloat($pF_curr[1])).'</td>
                        <td align="right" class="third_col">'.$extensionTool->tva($pF_curr[1]).'</td>
                        <td class="third_col_trend">'.$extensionAPITool->tendance('+', $extensionTool->convertFloat($pF_curr[1]), $extensionTool->convertFloat($pF_curr[2])).'</td>
                        <td align="right" class="no_parametrage" colspan="2">'.$extensionTool->tva($pF_curr[2]).'</td>
                    </tr>
                    <tr>
                        <td align="left" class="td_indicator"><span class="indic_label">'.$trad->t('Taux de profitabilité') . $all_tooltips['taux_profitabilite'] .'</span><span style="border-radius:15px;height:15px;width:15px;" class="indic_span '. $specNAOSController->indicatorforTP($vF_var_3[0]) .'"></span></td>
                        <td align="right">'. round($vF_var_3[0], 2) .'%</td>
                        <td class="second_col_trend">'.$extensionAPITool->tendance('+', $extensionTool->convertFloat($vF_var_3[0]), $extensionTool->convertFloat($vF_var_3[1])).'</td>
                        <td align="right" class="third_col">-</td>
                        <td class="third_col_trend">'.$extensionAPITool->tendance('+', $extensionTool->convertFloat($vF_var_3[1]), $extensionTool->convertFloat($vF_var_3[2])).'</td>
                        <td align="right" class="no_parametrage" colspan="2">-</td>
                    </tr>
                    <tr>
                        <td align="left" class="td_indicator"><span class="indic_label">'.$trad->t('Taux d\'endettement') . $all_tooltips['coeff_endet'] .'</span><span style="border-radius:15px;height:15px;width:15px;" class="indic_span '.$specNAOSController->indicatorForTE($vF_gear[0]).'"></span></td>
                        <td align="right">'. round($vF_gear[0], 2).'%' .'</td>
                        <td class="second_col_trend">'. $extensionAPITool->tendance('+', $extensionTool->convertFloat($vF_gear[0]), $extensionTool->convertFloat($vF_gear[1])) .'</td>
                        <td align="right" class="third_col">-</td>
                        <td class="third_col_trend">'. $extensionAPITool->tendance('+', $extensionTool->convertFloat($vF_gear[1]), $extensionTool->convertFloat($vF_gear[2])) .'</td>
                        <td align="right" class="no_parametage" colspan="2">-</td>
                    </tr>
                    <tr>
                        <td align="left" class="td_indicator"><span class="indic_label">'.$trad->t('Taux de solvabilité') . $all_tooltips['taux_solv'] .'</span><span style="border-radius:15px;height:15px;width:15px;" class="indic_span '.$specNAOSController->indicatorForTS($pF_solvr[0]).'"></span></td>
                        <td align="right">'.round($pF_solvr[0], 2).'%</td>
                        <td class="second_col_trend">'.$extensionAPITool->tendance('+', $extensionTool->convertFloat($pF_solvr[0]), $extensionTool->convertFloat($pF_solvr[1])).'</td>
                        <td align="right" class="third_col">'. round($pF_solvr[1], 2).'%</td>
                        <td class="third_col_trend">'.$extensionAPITool->tendance('+', $extensionTool->convertFloat($pF_solvr[1]), $extensionTool->convertFloat($pF_solvr[2])).'</td>
                        <td align="right" class="no_parametrage" colspan="2">'. round($pF_solvr[2], 2).'%</td>
                    </tr>
                    <tr>
                        <td align="left">'.$trad->t('BFR') . $all_tooltips['bfr'] .'</td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td align="left" class="td_indicator"><span class="indic_label">'.$trad->t('Fond de roulement') . $all_tooltips['fond_roul'] .'</span><span style="border-radius:15px;height:15px;width:15px;" class="indic_span '. $specNAOSController->indicatorForFRAndBFR($vF_wkca[0]) .'"></span></td>
                        <td align="right">'. round($vF_wkca[0], 2).' EUR'.'</td>
                        <td class="second_col_trend">'. $extensionAPITool->tendance('+', $extensionTool->convertFloat($vF_wkca[0]), $extensionTool->convertFloat($vF_wkca[1])) .'</td>
                        <td align="right" class="third_col">-</td>
                        <td class="third_col_trend">'. $extensionAPITool->tendance('+', $extensionTool->convertFloat($vF_wkca[1]), $extensionTool->convertFloat($vF_wkca[2])) .'</td>
                        <td align="right" class="no_parametage" colspan="2">-</td>
                    </tr>
                    <tr>
                        <td align="left" class="td_indicator"><span class="indic_label">'.$trad->t('Stabilité financière') . $all_tooltips['stab_fin'] .'</span><span class="indic_span"><strong>'.$specNAOSController->indicatorForSTF($specNAOSController->calculateCRIFRate($vF_crif[0])).'</strong></span></td>
                        <td align="right">'.$specNAOSController->calculateCRIFRate($vF_crif[0]).'</td>
                        <td class="second_col_trend">'.$extensionAPITool->tendance('+', $specNAOSController->calculateCRIFRate($vF_crif[0]), $specNAOSController->calculateCRIFRate($vF_crif[1])).'</td>
                        <td align="right" class="third_col">'.$specNAOSController->calculateCRIFRate($vF_crif[1]).'</td>
                        <td class="third_col_trend">'.$extensionAPITool->tendance('+', $specNAOSController->calculateCRIFRate($vF_crif[1]), $specNAOSController->calculateCRIFRate($vF_crif[2])).'</td>
                        <td align="right" class="no_parametrage" colspan="2">'. $specNAOSController->calculateCRIFRate($vF_crif[2]).'</td>
                    </tr>
                    <tr>
                        <td align="left" class="td_indicator"><span class="indic_label">'.$trad->t('Santé financière') . $all_tooltips['sant_fin'] .'</span><span class="indic_span"><strong>'.$specNAOSController->indicatorForSAF($vF_more[0]).'</strong></span></td>
                        <td align="right">'.$vF_more[0].'</td>
                        <td class="second_col_trend"></td>
                        <td align="right" class="third_col">'.$vF_more[1].'</td>
                        <td class="third_col_trend"></td>
                        <td align="right" class="no_parametrage" colspan="2">'. $vF_more[2].'</td>
                    </tr>
                    <tr>
                        <td align="left" class="td_indicator"><span class="indic_label">'.$trad->t('Risque commercial') . $all_tooltips['risq_com'] .'</span><span style="border-radius:15px;height:15px;width:15px;" class="indic_span '.$specNAOSController->indicatorForRCOM($vF_compass[0]).'"></span></td>
                        <td align="right">'.$vF_compass[0].'</td>
                        <td class="second_col_trend"></td>
                        <td align="right" class="third_col">-</td>
                        <td class="third_col_trend"></td>
                        <td align="right" colspan="2">-</td>
                    </tr>
                    <tr>
                        <td align="left" class="td_indicator"><span class="indic_label">'.$trad->t('Risque environnemental') . $all_tooltips['risq_env'] .'</span><span style="border-radius:15px;height:15px;width:15px;" class="indic_span '.$specNAOSController->indicatorForRENV($vF_trucost[0]).'"></span></td>
                        <td align="right">'.$vF_trucost[0].'%</td>
                        <td class="second_col_trend"></td>
                        <td align="right" class="third_col">-</td>
                        <td class="third_col_trend"></td>
                        <td align="right" colspan="2">-</td>
                    </tr>
                </tbody>
            </table>';

        
        return $html;
    }
}
