<?php


namespace App\Repositories;


class LegalRepository {
    const VARIABLE_PATTERN = "#%[a-zA-Z|0-9|_|\-]+%#";

    public static function terminosCondiciones() {
        return preg_replace_callback(LegalRepository::VARIABLE_PATTERN, array("App\\Repositories\\LegalRepository", "replacePreferenceVariable"), PreferenceRepository::find("terminos-condiciones")->value);
    }

    public static function politicaPrivacidad() {
        return preg_replace_callback(LegalRepository::VARIABLE_PATTERN, array("App\\Repositories\\LegalRepository", "replacePreferenceVariable"), PreferenceRepository::find("politica-privacidad")->value);
    }

    public static function avisoLegalBasico() {
        $pdp_url = PreferenceRepository::find("public_politica_privacidad_url")->value;
        return [
            [
                "header" => "Responsable",
                "data" => preg_replace_callback(LegalRepository::VARIABLE_PATTERN, array("App\\Repositories\\LegalRepository", "replacePreferenceVariable"), PreferenceRepository::find("proteccion_datos_basico_responsable")->value),
                "link" => $pdp_url
            ],
            [
                "header" => "Finalidad",
                "data" => preg_replace_callback(LegalRepository::VARIABLE_PATTERN, array("App\\Repositories\\LegalRepository", "replacePreferenceVariable"), PreferenceRepository::find("proteccion_datos_basico_finalidad")->value),
                "link" => $pdp_url
            ],
            [
                "header" => "Legitimación",
                "data" => preg_replace_callback(LegalRepository::VARIABLE_PATTERN, array("App\\Repositories\\LegalRepository", "replacePreferenceVariable"), PreferenceRepository::find("proteccion_datos_basico_legitimacion")->value),
                "link" => $pdp_url
            ],
            [
                "header" => "Destinatarios",
                "data" => preg_replace_callback(LegalRepository::VARIABLE_PATTERN, array("App\\Repositories\\LegalRepository", "replacePreferenceVariable"), PreferenceRepository::find("proteccion_datos_basico_destinatarios")->value),
                "link" => $pdp_url
            ],
            [
                "header" => "Derechos",
                "data" => preg_replace_callback(LegalRepository::VARIABLE_PATTERN, array("App\\Repositories\\LegalRepository", "replacePreferenceVariable"), PreferenceRepository::find("proteccion_datos_basico_derechos")->value),
                "link" => $pdp_url
            ],
            [
                "header" => "Información adicional",
                "data" => preg_replace_callback(LegalRepository::VARIABLE_PATTERN, array("App\\Repositories\\LegalRepository", "replacePreferenceVariable"), PreferenceRepository::find("proteccion_datos_basico_adicional")->value),
                "link" => $pdp_url
            ],
        ];
    }

    public static function avisoLegalBasicoAtleta($company_id) {
        $pdp_url = PreferenceRepository::find("public_politica_privacidad_url")->value;
        return [
            [
                "header" => "Responsable",
                "data" => preg_replace_callback(LegalRepository::VARIABLE_PATTERN, array("App\\Repositories\\LegalRepository", "replacePreferenceVariable"), PreferenceRepository::find("proteccion_datos_basico_responsable")->value),
                "link" => $pdp_url
            ],
            [
                "header" => "Finalidad",
                "data" => preg_replace_callback(LegalRepository::VARIABLE_PATTERN, array("App\\Repositories\\LegalRepository", "replacePreferenceVariable"), PreferenceRepository::find("proteccion_datos_basico_finalidad")->value),
                "link" => $pdp_url
            ],
            [
                "header" => "Legitimación",
                "data" => preg_replace_callback(LegalRepository::VARIABLE_PATTERN, array("App\\Repositories\\LegalRepository", "replacePreferenceVariable"), PreferenceRepository::find("proteccion_datos_basico_legitimacion")->value),
                "link" => $pdp_url
            ],
            [
                "header" => "Destinatarios",
                "data" => preg_replace_callback(LegalRepository::VARIABLE_PATTERN, array("App\\Repositories\\LegalRepository", "replacePreferenceVariable"), str_replace("%company_subscription%", CompanyRepository::find($company_id)->name,PreferenceRepository::find("proteccion_datos_basico_destinatarios_atleta")->value)),
                "link" => $pdp_url
            ],
            [
                "header" => "Derechos",
                "data" => preg_replace_callback(LegalRepository::VARIABLE_PATTERN, array("App\\Repositories\\LegalRepository", "replacePreferenceVariable"), PreferenceRepository::find("proteccion_datos_basico_derechos")->value),
                "link" => $pdp_url
            ],
            [
                "header" => "Información adicional",
                "data" => preg_replace_callback(LegalRepository::VARIABLE_PATTERN, array("App\\Repositories\\LegalRepository", "replacePreferenceVariable"), PreferenceRepository::find("proteccion_datos_basico_adicional")->value),
                "link" => $pdp_url
            ],
        ];
    }

    public static function avisoLegalCompleto() {
        return preg_replace_callback(LegalRepository::VARIABLE_PATTERN, array("App\\Repositories\\LegalRepository", "replacePreferenceVariable"), PreferenceRepository::find("proteccion_datos_completo")->value);
    }

    public static function avisoLegalCompletoAtleta($company_id) {
        $value = PreferenceRepository::find("proteccion_datos_completo_atleta")->value;
        $value = str_replace("%company_subscription%", CompanyRepository::find($company_id)->name,$value);
        return preg_replace_callback(LegalRepository::VARIABLE_PATTERN, array("App\\Repositories\\LegalRepository", "replacePreferenceVariable"), $value);
    }


    public static function replacePreferenceVariable($val) {
        $key = str_replace("%", "", $val[0]);
        return PreferenceRepository::find($key)->value;
    }
}
