/*****************************************************************
 * gmerlin - a general purpose multimedia framework and applications
 *
 * Copyright (c) 2001 - 2012 Members of the Gmerlin project
 * gmerlin-general@lists.sourceforge.net
 * http://gmerlin.sourceforge.net
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * *****************************************************************/

#include <string.h>

#include <gavl/gavl.h>
#include <gavl/utils.h>

#include "renderer.h"

#define LOG_DOMAIN "renderer"

static const bg_upnp_icon_t icons[] =
  {
    {
    .mimetype = "image/png",
    .width  = 16,
    .height = 16,
    .depth = 32,
    .location = "/static/icons/renderer_16.png"
    },
    {
    .mimetype = "image/png",
    .width  = 48,
    .height = 48,
    .depth = 32,
    .location = "/static/icons/renderer_48.png"
    },
    {
    .mimetype = "image/bmp",
    .width  = 48,
    .height = 48,
    .depth = 24,
    .location = "/static/icons/renderer_48.bmp"
    },
    {
    .mimetype = "image/jpeg",
    .width  = 48,
    .height = 48,
    .depth = 24,
    .location = "/static/icons/renderer_48.jpg"
    },
    {
    .mimetype = "image/png",
    .width  = 96,
    .height = 96,
    .depth = 32,
    .location = "/static/icons/renderer_96.png"
    },
    {
    .mimetype = "image/bmp",
    .width  = 96,
    .height = 96,
    .depth = 24,
    .location = "/static/icons/renderer_96.bmp"
    },
    {
    .mimetype = "image/jpeg",
    .width  = 96,
    .height = 96,
    .depth = 24,
    .location = "/static/icons/renderer_96.jpg"
    },
    { /* End */ },
  };


static const bg_parameter_info_t parameters[] =
  {
    {
      .name =      "loglevel",
      .type = BG_PARAMETER_INT,
      .long_name =  TRS("Log level"),
      .val_min = GAVL_VALUE_INIT_INT(0),
      .val_max = GAVL_VALUE_INIT_INT(4),
      .val_default = GAVL_VALUE_INIT_INT(3),
    },
    {
      .name =      "syslog",
      .long_name = TRS("Syslog name"),
      .type = BG_PARAMETER_STRING,
    },
    {
      .name =      "name",
      .long_name = TRS("upnp name"),
      .type = BG_PARAMETER_STRING,
      .val_default = GAVL_VALUE_INIT_STRING("Gmerlin renderer"),
    },
    {
      .name =      "uuid",
      .type = BG_PARAMETER_STRING,
      .flags = BG_PARAMETER_HIDE_DIALOG,
      .long_name =  TRS("UUID"),
    },
    {
      .name =      "mode",
      .type = BG_PARAMETER_STRINGLIST,
      .long_name =  TRS("Mode"),
      .multi_names = (const char*[]){ "audio", "video", "av", NULL },
      .multi_labels = (const char*[]){ TRS("Audio"), TRS("Video"), TRS("Audio/Video"), NULL },
      .val_default = GAVL_VALUE_INIT_STRING("av"),
    },
    { /* End */ },
  };

const bg_parameter_info_t * renderer_get_parameters(renderer_t * s)
  {
  if(!s->parameters)
    {
    const bg_parameter_info_t * arr[3];
    arr[0] = bg_http_server_get_parameters(s->s);
    arr[1] = parameters;
    arr[2] = NULL;
    s->parameters = bg_parameter_info_concat_arrays(arr);
    }
  
  return s->parameters;
  }

void renderer_set_parameter(void * priv, const char * name,
                          const gavl_value_t * val)
  {
  renderer_t * s = priv;
  if(!name)
    {
    bg_http_server_set_parameter(s->s, NULL, NULL);
    return;
    }
  else if(!strcmp(name, "uuid"))
    s->uuid_str = gavl_strrep(s->uuid_str, val->v.str);
  else if(!strcmp(name, "name"))
    s->upnp_name = gavl_strrep(s->upnp_name, val->v.str);
  else if(!strcmp(name, "syslog"))
    {
    if(val->v.str)
      gavl_log_syslog_init(val->v.str);
    }
  else if(!strcmp(name, "loglevel"))
    gavl_log_set_verbose_level(val->v.i);
  else if(!strcmp(name, "mode"))
    {
    if(!strcmp(val->v.str, "audio"))
      {
      s->do_audio = 1;
      s->do_video = 0;
      }
    else if(!strcmp(val->v.str, "video"))
      {
      s->do_audio = 0;
      s->do_video = 1;
      }
    else
      {
      s->do_audio = 1;
      s->do_video = 1;
      }
    }
  else
    bg_http_server_set_parameter(s->s, name, val);
  }

int renderer_get_parameter(void * priv, const char * name,
                          gavl_value_t * val)
  {
  renderer_t * s = priv;
  if(!name)
    return 0;
  if(!strcmp(name, "uuid"))
    {
    val->v.str = gavl_strrep(val->v.str, s->uuid_str);
    return 1;
    }
  else 
    return bg_http_server_get_parameter(s->s, name, val);
  }

void renderer_init(renderer_t * r)
  {
  memset(r, 0, sizeof(*r));
  r->s = bg_http_server_create();
  r->p = bg_embed_player_create();
  
  bg_embed_player_set_window_options(r->p,
                                     "gmerlin-renderer",
                                     "gmerlin-renderer",
                                     "renderer_icon.png");

  
  }

static int server_handle_root(bg_http_connection_t * conn, void * data)
  {
  if(!strcmp(conn->path, "/"))
    conn->path = "/static/mr.html";
  return 0;
  }

int renderer_start(renderer_t * r, const char * state_file)
  {
  if(state_file)
    bg_embed_player_set_state_file(r->p, state_file);

  /* Remember UUID */
  if(r->uuid_str)
    uuid_parse(r->uuid_str, r->uuid);
  else
    {
    uuid_clear(r->uuid);
    uuid_generate(r->uuid);
    r->uuid_str = calloc(37, 1);
    uuid_unparse(r->uuid, r->uuid_str);
    gavl_log(GAVL_LOG_INFO, LOG_DOMAIN, "Generated UUID: %s",
           r->uuid_str);
    }

  bg_embed_player_run(r->p);
    
  r->player_ctrl = bg_embed_player_get_ctrl(r->p);
  r->ws = bg_websocket_context_create(BG_REMOTE_DEVICE_RENDERER, r->s, NULL, r->player_ctrl);
  
  bg_http_server_start(r->s);
  
  /* Create UPNP device(s) */
  r->dev = bg_upnp_create_media_renderer(r->s,
                                         r->uuid,
                                         r->upnp_name,
                                         icons, r->do_audio, r->do_video);

  bg_controllable_connect(bg_embed_player_get_ctrl(r->p),
                          bg_upnp_device_get_control(r->dev));
  
  bg_http_server_add_handler(r->s, server_handle_root, BG_HTTP_PROTO_HTTP, NULL, r);
  bg_http_server_set_static_path(r->s, "/static/");

  
  return 1;
  }

int renderer_iteration(renderer_t * r)
  {
  int ret = 0;
  gavl_time_t delay_time = GAVL_TIME_SCALE / 20; // 50 ms
  
  /* Ping upnp device so it can do it's ssdp stuff */
  ret += bg_http_server_iteration(r->s);

  //  if(r->p)
  //    bg_embed_player_iteration(r->p);
  
  if(r->dev)
    ret += bg_upnp_device_ping(r->dev);

  if(r->ws)
    ret += bg_websocket_context_iteration(r->ws);

  if(!ret)
    gavl_time_delay(&delay_time);
 
  return 1;
  }

void renderer_cleanup(renderer_t * r)
  {
  if(r->uuid_str)
    free(r->uuid_str);
  if(r->upnp_name)
    free(r->upnp_name);

  if(r->p)
    bg_embed_player_destroy(r->p);

  if(r->s)
    bg_http_server_destroy(r->s);
  if(r->dev)
    bg_upnp_device_destroy(r->dev);
  }
