/****************************************************************************
 * This file is part of the project AqHome.
 * AqHome (c) by 2023 Martin Preuss, all rights reserved.
 *
 * The license for this file can be found in the file COPYING which you
 * should have received along with this file.
 ****************************************************************************/

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif


#include "./aqhome_mqtt_p.h"
#include "aqhome/ipc/endpoint_ipc.h"
#include "aqhome/ipc/endpoint_ipcclient.h"
#include "aqhome/mqtt/endpoint_mqttc.h"
#include "aqhome/mqtt/msg_mqtt_publish.h"

#include <gwenhywfar/misc.h>
#include <gwenhywfar/debug.h>





AQHOME_MQTT *AqHomeMqtt_new(void)
{
  AQHOME_MQTT *aqh;

  GWEN_NEW_OBJECT(AQHOME_MQTT, aqh);

  return aqh;
}



void AqHomeMqtt_free(AQHOME_MQTT *aqh)
{
  if (aqh) {
    AQHMQTT_Device_List_free(aqh->availableDeviceList);
    AQHMQTT_Device_List_free(aqh->registeredDeviceList);
    GWEN_MsgEndpoint_free(aqh->rootEndpoint);
    GWEN_DB_Group_free(aqh->dbArgs);
    free(aqh->pidFile);

    GWEN_FREE_OBJECT(aqh);
  }
}



GWEN_MSG_ENDPOINT *AqHomeMqtt_GetBrokerEndpoint(const AQHOME_MQTT *aqh)
{
  return aqh?(aqh->brokerEndpoint):NULL;
}



GWEN_MSG_ENDPOINT *AqHomeMqtt_GetMqttEndpoint(const AQHOME_MQTT *aqh)
{
  return aqh?(aqh->mqttEndpoint):NULL;
}



GWEN_DB_NODE *AqHomeMqtt_GetDbArgs(const AQHOME_MQTT *aqh)
{
  return aqh?(aqh->dbArgs):NULL;
}



const char *AqHomeMqtt_GetPidFile(const AQHOME_MQTT *aqh)
{
  return aqh?aqh->pidFile:NULL;
}



void AqHomeMqtt_SetPidFile(AQHOME_MQTT *aqh, const char *s)
{
  if (aqh) {
    free(aqh->pidFile);
    aqh->pidFile=s?strdup(s):NULL;
  }
}



int AqHomeMqtt_GetTimeout(const AQHOME_MQTT *aqh)
{
  return aqh?aqh->timeout:0;
}



const char *AqHomeMqtt_GetDeviceFile(const AQHOME_MQTT *aqh)
{
  return aqh?aqh->deviceFile:NULL;
}



void AqHomeMqtt_SetDeviceFile(AQHOME_MQTT *aqh, const char *s)
{
  if (aqh) {
    free(aqh->deviceFile);
    aqh->deviceFile=s?strdup(s):NULL;
  }
}



AQHMQTT_DEVICE_LIST *AqHomeMqtt_GetAvailableDeviceList(const AQHOME_MQTT *aqh)
{
  return aqh?aqh->availableDeviceList:NULL;
}



void AqHomeMqtt_SetAvailableDeviceList(AQHOME_MQTT *aqh, AQHMQTT_DEVICE_LIST *dl)
{
  if (aqh) {
    AQHMQTT_Device_List_free(aqh->availableDeviceList);
    aqh->availableDeviceList=dl;
  }
}



void AqHomeMqtt_SetRegisteredDeviceList(AQHOME_MQTT *aqh, AQHMQTT_DEVICE_LIST *dl)
{
  if (aqh) {
    AQHMQTT_Device_List_free(aqh->registeredDeviceList);
    aqh->registeredDeviceList=dl;
  }
}



AQHMQTT_DEVICE *AqHomeMqtt_FindRegisteredDevice(AQHOME_MQTT *aqh, const char *wantedDeviceId)
{
  if (aqh && aqh->registeredDeviceList) {
    return AQHMQTT_Device_List_GetById(aqh->registeredDeviceList, wantedDeviceId);
  }
  else {
    DBG_ERROR(NULL, "No registered devices");
  }

  return NULL;
}



void AqHomeMqtt_DumpRegisteredDevices(const AQHOME_MQTT *aqh)
{
  if (aqh && aqh->registeredDeviceList) {
    AQHMQTT_DEVICE *device;

    device=AQHMQTT_Device_List_First(aqh->registeredDeviceList);
    if (device) {
      fprintf(stderr, "Registered Devices:\n");
      while(device) {
        const char *sDeviceName;
        const char *sDeviceId;

        sDeviceName=AQHMQTT_Device_GetName(device);
        sDeviceId=AQHMQTT_Device_GetId(device);
        fprintf(stderr, "  %s (%s)\n", sDeviceId?sDeviceId:"<no id>", sDeviceName?sDeviceName:"<no name>");
        device=AQHMQTT_Device_List_Next(device);
      }
    }
    else {
      fprintf(stderr, "No registered devices\n");
    }
  }
  else {
    fprintf(stderr, "No registered devices\n");
  }
}








