# include "main.h"

/*********************************************gdbus*************************************************/
GDBusConnection *global_connection;
static GDBusNodeInfo *introspection_data = NULL;

/* Introspection data for the service we are exporting */
static const gchar introspection_xml[] =
  "<node>"
  "  <interface name='com.kylin.MaintainMode.interface'>"
  "    <method name='OpenMaintainMode'>"
  "      <annotation name='com.kylin.MaintainMode.Annotation' value='OnMethod'/>"
  "      <arg type='b' name='result' direction='out'/>"
  "    </method>"
  "    <method name='CloseMaintainMode'>"
  "      <annotation name='com.kylin.MaintainMode.Annotation' value='OnArg'/>"
  "      <arg type='b' name='result' direction='out'>"
  "      </arg>"
  "    </method>"
  "    <method name='GetBootMode'>"
  "      <annotation name='com.kylin.MaintainMode.Annotation' value='OnArg'/>"
  "      <arg type='s' name='bootmode' direction='out'>"
  "      </arg>"
  "    </method>"
  "    <signal name='BootModeChanged'>"
  "      <annotation name='com.kylin.MaintainMode.Annotation' value='Onsignal'/>"
  "      <arg type='u' name='bootmode'>"
  "        <annotation name='com.kylin.MaintainMode.Annotation' value='OnArg_NonFirst'/>"
  "      </arg>"
  "    </signal>"
  "  </interface>" 
  "</node>";

static void
handle_method_call (GDBusConnection       *connection,
                    const gchar           *sender,
                    const gchar           *object_path,
                    const gchar           *interface_name,
                    const gchar           *method_name,
                    GVariant              *parameters,
                    GDBusMethodInvocation *invocation,
                    gpointer               user_data)
{
  if (g_strcmp0 (method_name, "CloseMaintainMode") == 0)
  {
      gboolean result;
      result = mm_exit_maintain_mode ();
      g_dbus_method_invocation_return_value (invocation, g_variant_new ("(b)", result));
      
  } else if (g_strcmp0 (method_name, "OpenMaintainMode") == 0)
  {
     
      gboolean result;
      result = mm_enter_maintain_mode ();
       g_print("come open %s.", result? "true":"false");
      g_dbus_method_invocation_return_value (invocation, g_variant_new ("(b)", result));

  } else if (g_strcmp0 (method_name, "GetBootMode") == 0)
  {
      gchar *boot_mode = mm_get_boot_mode();
      g_dbus_method_invocation_return_value (invocation,
                                                 g_variant_new ("(s)", boot_mode));
      g_free (boot_mode);
  }
    
}

void mm_emit_mode_changed_signal (guint mode)
{
  GError *error=NULL;
   g_dbus_connection_emit_signal (global_connection,
                                 NULL,
                                 "/com/kylin/MaintainMode",
                                 "com.kylin.MaintainMode.interface",
                                 "BootModeChanged",
                                 g_variant_new("(u)",mode),
                                 &error);
  g_assert_no_error (error);
}
/* for now */
static const GDBusInterfaceVTable interface_vtable =
{
  handle_method_call
};
/* ---------------------------------------------------------------------------------------------------- */
static void
on_bus_acquired (GDBusConnection *connection,
                 const gchar     *name,
                 gpointer         user_data)
{
  guint registration_id;

  registration_id = g_dbus_connection_register_object (connection,
                                                       "/com/kylin/MaintainMode/Object",
                                                       introspection_data->interfaces[0],
                                                       &interface_vtable,
                                                       NULL,  /* user_data */
                                                       NULL,  /* user_data_free_func */
                                                       NULL); /* GError** */
  g_assert (registration_id > 0);
  global_connection = connection;

}

static void
on_name_acquired (GDBusConnection *connection,
                  const gchar     *name,
                  gpointer         user_data)
{

}

static void
on_name_lost (GDBusConnection *connection,
              const gchar     *name,
              gpointer         user_data)
{
  g_print("dbus name lost.\n");
  exit (1);
}

int main(int argc, char **argv)
{
	GMainLoop *loop;
	guint16 owner_id;

  /*init log*/
  mm_log_init ();

	/*init gdbus*/
	introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
	g_assert (introspection_data != NULL);

	owner_id = g_bus_own_name (G_BUS_TYPE_SYSTEM,
								 "com.kylin.MaintainMode",
								 G_BUS_NAME_OWNER_FLAGS_NONE,
								 on_bus_acquired,
								 on_name_acquired,
								 on_name_lost,
								 NULL,
								 NULL );
   
	/*mainloop*/
	loop = g_main_loop_new (NULL, TRUE);

	g_main_loop_run (loop);
  
	g_bus_unown_name (owner_id);
	g_dbus_node_info_unref (introspection_data);
  
	g_main_loop_unref (loop);

	return 0;	

}