<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Session;

use App\Custom\SQLConnection;

class TraduccionServiceProvider extends ServiceProvider {
    /**
     * Register services.
     */
    public function register(): void {
        //
    }

    /**
     * Bootstrap services.
     */
    public function boot(): void {
        Blade::directive('traducir', function ($array) {
            $vista = app('view')->getEngineResolver()->resolve('blade')->getCompiler()->getPath();
            $vista = (string) substr($vista, strrpos($vista, '/') + 1);
            list($key, $value) = explode(',', $array);
            $vista = substr($vista, 0, strpos($vista, '.'));
            $traducciones = [];

            if (!isset(Session::get('traducciones')[$vista])) {
                $trad = SQLConnection::backendStoreProcedure('traducciones_web', ['get', env('APP_ID'), $vista, null, null], 'select');
                $idioma = Session::get('identificacion')['codigo_idioma'] ?? 'spa';

                foreach ($trad as $n => $expr) {
                    $traducciones[$vista][strtoupper($expr->app_key)] = (object) [
                        'spa' => trim(str_replace("'", '', str_replace('"', '', $expr->spa))),
                        'por' => trim(str_replace("'", '', str_replace('"', '', $expr->por))),
                        'eng' => trim(str_replace("'", '', str_replace('"', '', $expr->eng))),
                    ];
                }
                Session::put('traducciones', $traducciones);
            }

            $key = trim(str_replace("'", '', str_replace('"', '', $key)));

            if (!isset(Session::get('traducciones')[$vista][strtoupper($key)])) {
                $value = trim(str_replace("'", '', str_replace('"', '', $value)));
                SQLConnection::backendStoreProcedure('traducciones_web', ['insert', env('APP_ID'), $vista, $key, $value], 'selectOne');

                $traducciones = [];
                $traducciones[$vista][$key] = $value;
                Session::put('traducciones', $traducciones);
                $value = (object) ['spa' => $value, 'por' => $value, 'eng' => $value];
            } else {
                $value = Session::get('traducciones')[$vista][strtoupper($key)];
            }

            if (!is_object($value)) {
                return "<?php echo '$value'; ?>";
            } else {
                return "<?php if (isset(Session::get('identificacion')['codigo_idioma']) && Session::get('identificacion')['codigo_idioma'] == 'eng'): echo('" . $value->eng . "'); elseif (isset(Session::get('identificacion')['codigo_idioma']) && Session::get('identificacion')['codigo_idioma'] == 'por'): echo('" . $value->por . "'); else: echo('" . $value->spa . "'); endif; ?>";
            }
        });


        Blade::directive('traducirVar', function ($keyvalue) {
            $vista = app('view')->getEngineResolver()->resolve('blade')->getCompiler()->getPath();
            $vista = (string) substr($vista, strrpos($vista, '/') + 1);
            $vista = substr($vista, 0, strpos($vista, '.'));
            $app_id = isset(Session::get('webdata')['app_id']) ? Session::get('webdata')['app_id'] : env('APP_ID');
            //
            list($key, $value) = explode(',', $keyvalue);
            $traducciones = [];
            if (!isset(Session::get('traducciones')[$vista])) {
                $trad = SQLConnection::backendStoreProcedure('traducciones_web', array('get', $app_id, $vista, NULL, NULL), 'select');
                $idioma = (isset(Session::get('usuario')->codigo_idioma)) ? Session::get('usuario')->codigo_idioma : 'spa';
                //
                foreach ($trad as $n => $expr) {
                    $traducciones[$vista][strtoupper($expr->app_key)] = (object) ['spa' => trim(str_replace("'", '', str_replace('"', '', $expr->spa))), 'por' => trim(str_replace("'", '', str_replace('"', '', $expr->por))), 'eng' => trim(str_replace("'", '', str_replace('"', '', $expr->eng)))];
                }
                Session::put('traducciones', $traducciones);
            }
            //
            if (!isset(Session::get('traducciones')[$vista][$key])) {
                $value = trim(str_replace("'", '', str_replace('"', '', $value)));
                $temp = SQLConnection::backendStoreProcedure('traducciones_web', array('insert', $app_id, $vista, $key, $value), 'selectOne');
                $traducciones = [];
                $traducciones[$vista][$key] = $value;
                Session::put('traducciones', $traducciones);
                $value = (object) ['spa' => $value, 'por' => $value, 'eng' => $value];
            }
            //
            return "<?php if( isset(Session::get('usuario')->codigo_idioma) && Session::get('usuario')->codigo_idioma == 'eng' ): echo('" . $value->eng . "'); elseif( isset(Session::get('usuario')->codigo_idioma) && Session::get('usuario')->codigo_idioma == 'por' ): echo('" . $value->por . "'); else: echo('" . $value->spa . "'); endif; ?>";
        });
    }
}
