<?php

function do_options($options, $blob) {
    if ($options === 'make-preferencias-api') {
        make_preferencias_api($blob);
    }
}

function make_preferencias_api($blob) {
    global $dom, $ip_stream;
    $preferencias_json_path = $_SERVER['DOCUMENT_ROOT'] . '/api/preferencias/preferencias.json';

    $json_data = [];
    $json_data['nombre'] = isset($blob['appstore']['name']) ? $blob['appstore']['name'] : '';
    $json_data['slogan'] = isset($blob['appstore']['slogan']) ? $blob['appstore']['slogan'] : '';
    $json_data['ticker'] = isset($blob['appstore']['ticker']) ? $blob['appstore']['ticker'] : $json_data['slogan'];
    $json_data['api_url'] = $dom . '/api/v1';
    $json_data['site_url'] = $dom;
    $json_data['stream_url'] = $ip_stream;

    $json_data['social'] = [];
    if (isset($blob['social'])) {
        foreach ($blob['social'] as $nombre=>$valor) {
            if ($valor) {
                // Detallitos
                switch ($nombre) {
                    case 'email':       $icon = 'mail';         $button = 'Redactar'; $valor = "mailto:$valor"; break;
                    case 'telefono':    $icon = 'call';         $button = 'Llamar';   $valor = "tel:$valor";    break;
                    case 'whatsapp':    $icon = 'logo-whatsapp';$button = 'Mensaje';  $valor = "whatsapp://send?phone=$valor"; break;
                    case 'googleplus':  $icon = 'googleplus';   $nombre = 'Google+'; break;
                    case 'youtube':     $icon = 'logo-youtube'; $nombre = 'YouTube'; break;
                    default:
                        $icon = "logo-$nombre";
                }
                $row = [
                    'label' => ucwords($nombre),
                    'icon' => $icon,
                    'url' => $valor,
                ];
                if (isset($button)) $row['button'] = $button;

                array_push($json_data['social'], $row);
            }
        }
    }

    $file = fopen($preferencias_json_path, 'w');
    fwrite($file, json_encode($json_data));
    fclose($file);
}
