// gcc cluttering3.c -o clutterapp `pkg-config clutter-1.0 --cflags --libs` 
#include <clutter/clutter.h>
#include <cairo/cairo.h>

#define SPACING         6

gboolean svg_draw(ClutterCanvas *canvas, cairo_t *cr, gint width, gint height, gchar* image)
{
  cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
  cairo_paint(cr);
  cairo_set_operator(cr, CAIRO_OPERATOR_OVER); 
  cairo_set_source_rgb (cr, 0.5, 0.5, 1);
  cairo_rectangle(cr, SPACING, SPACING, width, height);
  cairo_fill(cr);
  return TRUE;
}

gboolean card_clicked (ClutterActor *card, ClutterButtonEvent *event, gpointer user_data)
{
  
  ClutterContent *svg_content = clutter_actor_get_content(card);
  // TODO uncomment this to see the content toggled
  //if (svg_content) {
  //  printf("Clicked to hide\n");
  //  clutter_actor_set_content(card, NULL);
  //} else {
    printf("Clicked to show\n");
    svg_content = clutter_canvas_new();
    gfloat width, height;
    clutter_actor_get_size(card, &width, &height);
    g_signal_connect(svg_content, "draw", G_CALLBACK(svg_draw), NULL);
    clutter_canvas_set_size(CLUTTER_CANVAS(svg_content), width, height );
    clutter_actor_set_content(card, svg_content);
    clutter_content_invalidate (svg_content);
  //}
}

void card_resized (ClutterActor *actor, ClutterActorBox *box, ClutterAllocationFlags flags, gpointer user_data)
{
  ClutterCanvas* content = NULL;
  if ((content = CLUTTER_CANVAS(clutter_actor_get_content(actor))) != NULL) {
    clutter_canvas_set_size(content, box->x2 - box->x1, box->y2 - box->y1);
  }
}

ClutterActor* prepare_card ()
{
    ClutterActor *card = clutter_actor_new ();
    clutter_actor_set_background_color (card, CLUTTER_COLOR_Plum);
    clutter_actor_set_reactive(card, TRUE);
    // XXX this is the line breaking first-time display
    clutter_actor_set_content_gravity(card, CLUTTER_CONTENT_GRAVITY_CENTER);
    // XXX without the line above image is displayed at first
    g_signal_connect(card, "allocation-changed", G_CALLBACK(card_resized), NULL);
    g_signal_connect(card, "button-press-event", G_CALLBACK(card_clicked), NULL);
    printf("Card prepared\n");
    return card;
}

ClutterActor* prepare_stage () 
{
  ClutterActor* stage, *card;
  
  stage = clutter_stage_new();
  clutter_actor_set_background_color (stage, CLUTTER_COLOR_LightSkyBlue);
  card = prepare_card();
  clutter_actor_add_constraint (card, clutter_bind_constraint_new(stage, CLUTTER_BIND_SIZE, -20));
  clutter_actor_add_constraint (card, clutter_bind_constraint_new(stage, CLUTTER_BIND_POSITION, 10));
  clutter_actor_add_child(stage, card);
  clutter_stage_set_title (CLUTTER_STAGE (stage), "Content center problem");
  clutter_stage_set_user_resizable (CLUTTER_STAGE (stage), TRUE);
  g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL);
  return stage;
}

int
main (int argc, char *argv[])
{
  GError *error;

  error = NULL;
  if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
    {
      if (error)
        {
          g_critical ("Unable to initialize Clutter: %s", error->message);
          g_error_free (error);
          return 1;
        }
      else
        g_error ("Unable to initialize Clutter");
    }

  ClutterActor *stage = prepare_stage();
  
  clutter_actor_show(stage);

  clutter_main ();

  return 0;
}
