/* GSequencer - Advanced GTK Sequencer
 * Copyright (C) 2005-2017 Joël Krähemann
 *
 * This file is part of GSequencer.
 *
 * GSequencer 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 3 of the License, or
 * (at your option) any later version.
 *
 * GSequencer 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 GSequencer.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <ags/audio/task/ags_reset_audio_connection.h>

#include <ags/audio/ags_audio_connection.h>
#include <ags/audio/ags_channel.h>
#include <ags/audio/ags_output.h>
#include <ags/audio/ags_input.h>

#include <ags/i18n.h>

void ags_reset_audio_connection_class_init(AgsResetAudioConnectionClass *reset_audio_connection);
void ags_reset_audio_connection_connectable_interface_init(AgsConnectableInterface *connectable);
void ags_reset_audio_connection_init(AgsResetAudioConnection *reset_audio_connection);
void ags_reset_audio_connection_set_property(GObject *gobject,
					     guint prop_id,
					     const GValue *value,
					     GParamSpec *param_spec);
void ags_reset_audio_connection_get_property(GObject *gobject,
					     guint prop_id,
					     GValue *value,
					     GParamSpec *param_spec);
void ags_reset_audio_connection_connect(AgsConnectable *connectable);
void ags_reset_audio_connection_disconnect(AgsConnectable *connectable);
void ags_reset_audio_connection_dispose(GObject *gobject);
void ags_reset_audio_connection_finalize(GObject *gobject);

void ags_reset_audio_connection_launch(AgsTask *task);

/**
 * SECTION:ags_reset_audio_connection
 * @short_description: reset audio connection task
 * @title: AgsResetAudioConnection
 * @section_id:
 * @include: ags/audio/task/ags_reset_audio_connection.h
 *
 * The #AgsResetAudioConnection resets audio connection.
 */

static gpointer ags_reset_audio_connection_parent_class = NULL;
static AgsConnectableInterface *ags_reset_audio_connection_parent_connectable_interface;

enum{
  PROP_0,
  PROP_SOUNDCARD,
  PROP_AUDIO,
  PROP_CHANNEL_TYPE,
  PROP_PAD,
  PROP_AUDIO_CHANNEL,
  PROP_MAPPED_LINE,
};

GType
ags_reset_audio_connection_get_type()
{
  static GType ags_type_reset_audio_connection = 0;

  if(!ags_type_reset_audio_connection){
    static const GTypeInfo ags_reset_audio_connection_info = {
      sizeof (AgsResetAudioConnectionClass),
      NULL, /* base_init */
      NULL, /* base_finalize */
      (GClassInitFunc) ags_reset_audio_connection_class_init,
      NULL, /* class_finalize */
      NULL, /* class_data */
      sizeof (AgsResetAudioConnection),
      0,    /* n_preallocs */
      (GInstanceInitFunc) ags_reset_audio_connection_init,
    };

    static const GInterfaceInfo ags_connectable_interface_info = {
      (GInterfaceInitFunc) ags_reset_audio_connection_connectable_interface_init,
      NULL, /* interface_finalize */
      NULL, /* interface_data */
    };
    
    ags_type_reset_audio_connection = g_type_register_static(AGS_TYPE_TASK,
							     "AgsResetAudioConnection",
							     &ags_reset_audio_connection_info,
							     0);
    
    g_type_add_interface_static(ags_type_reset_audio_connection,
				AGS_TYPE_CONNECTABLE,
				&ags_connectable_interface_info);
  }
  
  return (ags_type_reset_audio_connection);
}

void
ags_reset_audio_connection_class_init(AgsResetAudioConnectionClass *reset_audio_connection)
{
  GObjectClass *gobject;
  AgsTaskClass *task;
  GParamSpec *param_spec;

  ags_reset_audio_connection_parent_class = g_type_class_peek_parent(reset_audio_connection);

  /* gobject */
  gobject = (GObjectClass *) reset_audio_connection;

  gobject->set_property = ags_reset_audio_connection_set_property;
  gobject->get_property = ags_reset_audio_connection_get_property;

  gobject->dispose = ags_reset_audio_connection_dispose;
  gobject->finalize = ags_reset_audio_connection_finalize;

  /* properties */
  /**
   * AgsResetAudioConnection:soundcard:
   *
   * The assigned #AgsSoundcard
   * 
   * Since: 1.0.0
   */
  param_spec = g_param_spec_object("soundcard",
				   i18n_pspec("soundcard of reset audio connection"),
				   i18n_pspec("The soundcard of reset audio connection task"),
				   G_TYPE_OBJECT,
				   G_PARAM_READABLE | G_PARAM_WRITABLE);
  g_object_class_install_property(gobject,
				  PROP_SOUNDCARD,
				  param_spec);

  /**
   * AgsResetAudioConnection:audio:
   *
   * The assigned #AgsAudio
   * 
   * Since: 1.0.0
   */
  param_spec = g_param_spec_object("audio",
				   i18n_pspec("audio of reset audio connection"),
				   i18n_pspec("The audio of reset audio connection task"),
				   AGS_TYPE_AUDIO,
				   G_PARAM_READABLE | G_PARAM_WRITABLE);
  g_object_class_install_property(gobject,
				  PROP_AUDIO,
				  param_spec);

  /**
   * AgsResetAudioConnection:channel-type:
   *
   * The connection's channel type.
   * 
   * Since: 1.0.0
   */
  param_spec = g_param_spec_gtype("channel-type",
				  i18n_pspec("channel type"),
				  i18n_pspec("The channel type of connection"),
				  G_TYPE_NONE,
				  G_PARAM_READABLE | G_PARAM_WRITABLE);
  g_object_class_install_property(gobject,
				  PROP_CHANNEL_TYPE,
				  param_spec);

  
  /**
   * AgsResetAudioConnection:pad:
   *
   * The nth pad of audio.
   * 
   * Since: 1.0.0
   */
  param_spec = g_param_spec_uint("pad",
				 i18n_pspec("nth pad"),
				 i18n_pspec("The nth pad of audio"),
				 0,
				 G_MAXUINT,
				 0,
				 G_PARAM_READABLE | G_PARAM_WRITABLE);
  g_object_class_install_property(gobject,
				  PROP_PAD,
				  param_spec);


  /**
   * AgsResetAudioConnection:audio-channel:
   *
   * The nth audio channel of audio.
   * 
   * Since: 1.0.0
   */
  param_spec = g_param_spec_uint("audio-channel",
				 i18n_pspec("nth audio channel"),
				 i18n_pspec("The nth audio channel of audio"),
				 0,
				 G_MAXUINT,
				 0,
				 G_PARAM_READABLE | G_PARAM_WRITABLE);
  g_object_class_install_property(gobject,
				  PROP_AUDIO_CHANNEL,
				  param_spec);
  
  /**
   * AgsResetAudioConnection:mapped-line:
   *
   * The nth mapped line of connection.
   * 
   * Since: 1.0.0
   */
  param_spec = g_param_spec_uint("mapped-line",
				 i18n_pspec("nth mapped line"),
				 i18n_pspec("The nth mapped line of connection"),
				 0,
				 G_MAXUINT,
				 0,
				 G_PARAM_READABLE | G_PARAM_WRITABLE);
  g_object_class_install_property(gobject,
				  PROP_MAPPED_LINE,
				  param_spec);

  /* task */
  task = (AgsTaskClass *) reset_audio_connection;

  task->launch = ags_reset_audio_connection_launch;
}

void
ags_reset_audio_connection_connectable_interface_init(AgsConnectableInterface *connectable)
{
  ags_reset_audio_connection_parent_connectable_interface = g_type_interface_peek_parent(connectable);

  connectable->connect = ags_reset_audio_connection_connect;
  connectable->disconnect = ags_reset_audio_connection_disconnect;
}

void
ags_reset_audio_connection_init(AgsResetAudioConnection *reset_audio_connection)
{
  reset_audio_connection->soundcard = NULL;

  reset_audio_connection->audio = NULL;
  reset_audio_connection->channel_type = G_TYPE_NONE;
  
  reset_audio_connection->pad = 0;
  reset_audio_connection->audio_channel = 0;

  reset_audio_connection->mapped_line = 0;
}

void
ags_reset_audio_connection_set_property(GObject *gobject,
					guint prop_id,
					const GValue *value,
					GParamSpec *param_spec)
{
  AgsResetAudioConnection *reset_audio_connection;

  reset_audio_connection = AGS_RESET_AUDIO_CONNECTION(gobject);

  switch(prop_id){
  case PROP_SOUNDCARD:
    {
      GObject *soundcard;

      soundcard = (GObject *) g_value_get_object(value);

      if(reset_audio_connection->soundcard == (GObject *) soundcard){
	return;
      }

      if(reset_audio_connection->soundcard != NULL){
	g_object_unref(reset_audio_connection->soundcard);
      }

      if(soundcard != NULL){
	g_object_ref(soundcard);
      }

      reset_audio_connection->soundcard = (GObject *) soundcard;
    }
    break;
  case PROP_AUDIO:
    {
      AgsAudio *audio;

      audio = (AgsAudio *) g_value_get_object(value);

      if(reset_audio_connection->audio == (GObject *) audio){
	return;
      }

      if(reset_audio_connection->audio != NULL){
	g_object_unref(reset_audio_connection->audio);
      }

      if(audio != NULL){
	g_object_ref(audio);
      }

      reset_audio_connection->audio = (GObject *) audio;
    }
    break;
  case PROP_CHANNEL_TYPE:
    {
      reset_audio_connection->channel_type = g_value_get_gtype(value);
    }
    break;
  case PROP_PAD:
    {
      reset_audio_connection->pad = g_value_get_uint(value);
    }
    break;
  case PROP_AUDIO_CHANNEL:
    {
      reset_audio_connection->audio_channel = g_value_get_uint(value);
    }
    break;
  case PROP_MAPPED_LINE:
    {
      reset_audio_connection->mapped_line = g_value_get_uint(value);
    }
    break;
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, prop_id, param_spec);
    break;
  }
}

void
ags_reset_audio_connection_get_property(GObject *gobject,
					guint prop_id,
					GValue *value,
					GParamSpec *param_spec)
{
  AgsResetAudioConnection *reset_audio_connection;

  reset_audio_connection = AGS_RESET_AUDIO_CONNECTION(gobject);

  switch(prop_id){
  case PROP_SOUNDCARD:
    {
      g_value_set_object(value, reset_audio_connection->soundcard);
    }
    break;
  case PROP_AUDIO:
    {
      g_value_set_object(value, reset_audio_connection->audio);
    }
    break;
  case PROP_CHANNEL_TYPE:
    {
      g_value_set_gtype(value, reset_audio_connection->channel_type);
    }
    break;
  case PROP_PAD:
    {
      g_value_set_uint(value, reset_audio_connection->pad);
    }
    break;
  case PROP_AUDIO_CHANNEL:
    {
      g_value_set_uint(value, reset_audio_connection->audio_channel);
    }
    break;
  case PROP_MAPPED_LINE:
    {
      g_value_set_uint(value, reset_audio_connection->mapped_line);
    }
    break;
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, prop_id, param_spec);
    break;
  }
}

void
ags_reset_audio_connection_connect(AgsConnectable *connectable)
{
  ags_reset_audio_connection_parent_connectable_interface->connect(connectable);

  /* empty */
}

void
ags_reset_audio_connection_disconnect(AgsConnectable *connectable)
{
  ags_reset_audio_connection_parent_connectable_interface->disconnect(connectable);

  /* empty */
}

void
ags_reset_audio_connection_dispose(GObject *gobject)
{
  AgsResetAudioConnection *reset_audio_connection;

  reset_audio_connection = AGS_RESET_AUDIO_CONNECTION(gobject);

  if(reset_audio_connection->soundcard != NULL){
    g_object_unref(reset_audio_connection->soundcard);

    reset_audio_connection->soundcard = NULL;
  }

  if(reset_audio_connection->audio != NULL){
    g_object_unref(reset_audio_connection->audio);

    reset_audio_connection->audio = NULL;
  }
    
  /* call parent */
  G_OBJECT_CLASS(ags_reset_audio_connection_parent_class)->dispose(gobject);
}

void
ags_reset_audio_connection_finalize(GObject *gobject)
{
  AgsResetAudioConnection *reset_audio_connection;

  reset_audio_connection = AGS_RESET_AUDIO_CONNECTION(gobject);

  if(reset_audio_connection->soundcard != NULL){
    g_object_unref(reset_audio_connection->soundcard);
  }

  if(reset_audio_connection->audio != NULL){
    g_object_unref(reset_audio_connection->audio);
  }

  /* call parent */
  G_OBJECT_CLASS(ags_reset_audio_connection_parent_class)->finalize(gobject);
}

void
ags_reset_audio_connection_launch(AgsTask *task)
{
  AgsAudio *audio;
  AgsAudioConnection *audio_connection;
  AgsChannel *input, *output;
  AgsChannel *channel;
  
  AgsResetAudioConnection *reset_audio_connection;

  AgsMutexManager *mutex_manager;

  AgsConnectionManager *connection_manager;

  GParameter *parameter;
  GList *list_start, *list;

  guint audio_channels;
  
  pthread_mutex_t *application_mutex;
  pthread_mutex_t *audio_mutex;

  /* get mutex manager and application mutex */
  mutex_manager = ags_mutex_manager_get_instance();
  application_mutex = ags_mutex_manager_get_application_mutex(mutex_manager);
  
  reset_audio_connection = AGS_RESET_AUDIO_CONNECTION(task);

  audio = reset_audio_connection->audio;

  connection_manager = ags_connection_manager_get_instance();

  /* find audio connection */
  audio_connection = NULL;
  
  list_start =
    list = g_list_copy(audio->audio_connection);
	  
  while((list = ags_audio_connection_find(list,
					  AGS_TYPE_INPUT,
					  reset_audio_connection->pad,
					  reset_audio_connection->audio_channel)) != NULL){
    GObject *data_object;

    pthread_mutex_lock(AGS_CONNECTION(list->data)->mutex);
    
    g_object_get(G_OBJECT(list->data),
		 "data-object", &data_object,
		 NULL);

    pthread_mutex_unlock(AGS_CONNECTION(list->data)->mutex);
    	    
    if(AGS_IS_SOUNDCARD(data_object)){
      audio_connection = list->data;
      
      break;
    }

    list = list->next;
  }

  g_list_free(list_start);

  /* create audio connection if needed */
  if(audio_connection == NULL){
    audio_connection = g_object_new(AGS_TYPE_AUDIO_CONNECTION,
				    NULL);
    ags_audio_add_audio_connection(audio,
				   (GObject *) audio_connection);
    ags_connection_manager_add_connection(connection_manager,
					  (AgsConnection *) audio_connection);
  }

  /* reset audio connection */
  pthread_mutex_lock(AGS_CONNECTION(list->data)->mutex);
  
  g_object_set(audio_connection,
	       "data-object", reset_audio_connection->soundcard,
	       "audio", audio,
	       "channel-type", reset_audio_connection->channel_type,
	       "pad", reset_audio_connection->pad,
	       "audio-channel", reset_audio_connection->audio_channel,
	       "mapped-line", reset_audio_connection->mapped_line,
	       NULL);

  pthread_mutex_unlock(AGS_CONNECTION(list->data)->mutex);

  /* get audio mutex */
  pthread_mutex_lock(application_mutex);

  audio_mutex = ags_mutex_manager_lookup(mutex_manager,
					 (GObject *) audio);

  pthread_mutex_unlock(application_mutex);

  /* get some fields */
  pthread_mutex_lock(audio_mutex);

  output = audio->output;
  input = audio->input;

  audio_channels = audio->audio_channels;
  
  pthread_mutex_unlock(audio_mutex);

  /* get channel */
  if(g_type_is_a(reset_audio_connection->channel_type,
		 AGS_TYPE_INPUT)){
    channel = ags_channel_nth(input,
			      reset_audio_connection->pad * audio_channels + reset_audio_connection->audio_channel);
  }else{
    channel = ags_channel_nth(output,
			      reset_audio_connection->pad * audio_channels + reset_audio_connection->audio_channel);
  }

  parameter = g_new0(GParameter,
		     1);

  parameter[0].name = "soundcard";

  g_value_init(&(parameter[0].value),
	       G_TYPE_OBJECT);
  g_value_set_object(&(parameter[0].value),
		     reset_audio_connection->soundcard);
  
  if(channel != NULL){
    ags_channel_recursive_set_property(channel,
				       parameter, 1);
  }
}
  
/**
 * ags_reset_audio_connection_new:
 * @soundcard: the data-object
 * @audio: the #AgsAudio
 * @channel_type: the #GType
 * @pad: the pad
 * @audio_channel: the channel
 * @mapped_line: the mapped-line
 *
 * Creates an #AgsResetAudioConnection.
 *
 * Returns: an new #AgsResetAudioConnection.
 *
 * Since: 1.0.0
 */
AgsResetAudioConnection*
ags_reset_audio_connection_new(GObject *soundcard,
			       AgsAudio *audio,
			       GType channel_type,
			       guint pad,
			       guint audio_channel,
			       guint mapped_line)
{
  AgsResetAudioConnection *reset_audio_connection;

  reset_audio_connection = (AgsResetAudioConnection *) g_object_new(AGS_TYPE_RESET_AUDIO_CONNECTION,
								    "soundcard", soundcard,
								    "audio", audio,
								    "channel_type", channel_type,
								    "pad", pad,
								    "audio_channel", audio_channel,
								    "mapped_line", mapped_line,
								    NULL);

  return(reset_audio_connection);
}
