/** @file MOMDeviceCollection.cpp
 *
 * Copyright (C) Espial Limited 2014 - Company confidential - All rights reserved.
 *
 */

#include "config.h"

#if ENABLE(RMAI)

#include "MOMDeviceCollection.h"
#include "MOMDevice.h"
#include "MOMSource.h"
#include "MOMRecorder.h"

#include "MOMException.h"

extern "C" {
#include "layers.h"
#include "cdf/mom_driver.h"
}

namespace WebCore {

/* filter definitions for creating device collections */

static const mom_driver_filter s_device_filter_all[] =
{
    { MDA_SOURCE, MDA_SUBTYPE_UNDEFINED },
    { MDA_RECORDER, MDA_SUBTYPE_UNDEFINED },
    { MDA_UNDEFINED, MDA_SUBTYPE_UNDEFINED } /* terminate */
};

static const mom_driver_filter s_device_filter_source[] =
{
    { MDA_SOURCE, MDA_SUBTYPE_UNDEFINED },
    { MDA_UNDEFINED, MDA_SUBTYPE_UNDEFINED } /* terminate */
};

static const mom_driver_filter s_device_filter_recorder[] =
{
    { MDA_RECORDER, MDA_SUBTYPE_UNDEFINED },
    { MDA_UNDEFINED, MDA_SUBTYPE_UNDEFINED } /* terminate */
};

static const mom_driver_filter s_device_filter_rf[] =
{
    { MDA_SOURCE, MDA_SUBTYPE_DVBS },
    { MDA_SOURCE, MDA_SUBTYPE_DVBT },
    { MDA_SOURCE, MDA_SUBTYPE_DVBC },
    { MDA_SOURCE, MDA_SUBTYPE_ATSC },
    { MDA_SOURCE, MDA_SUBTYPE_OPENCABLE },
    { MDA_UNDEFINED, MDA_SUBTYPE_UNDEFINED } /* terminate */
};

static const mom_driver_filter s_device_filter_iptv[] =
{
    { MDA_SOURCE, MDA_SUBTYPE_IPTV },
    { MDA_UNDEFINED, MDA_SUBTYPE_UNDEFINED } /* terminate */
};

static const mom_driver_filter s_device_filter_upnp[] =
{
    { MDA_SOURCE, MDA_SUBTYPE_UPNP },
    { MDA_UNDEFINED, MDA_SUBTYPE_UNDEFINED } /* terminate */
};

static const mom_driver_filter s_device_filter_removable[] =
{
    { MDA_REMOVABLE, MDA_SUBTYPE_UNDEFINED },
    { MDA_UNDEFINED, MDA_SUBTYPE_UNDEFINED } /* terminate */
};

static const mom_driver_filter s_device_filter_vod[] =
{
    { MDA_SOURCE, MDA_SUBTYPE_VOD },
    { MDA_UNDEFINED, MDA_SUBTYPE_UNDEFINED } /* terminate */
};

static const mom_driver_filter * const s_device_filter_list[] =
{
    NULL,
    s_device_filter_all,
    s_device_filter_source,
    s_device_filter_recorder,
    s_device_filter_rf,
    s_device_filter_iptv,
    s_device_filter_upnp,
    s_device_filter_vod
};

#define FILTER_COUNT    (sizeof(s_device_filter_list)/sizeof(s_device_filter_list[0]))

/* ------------------------------------------------------------------------ */

PassRefPtr<MOMDeviceCollection> MOMDeviceCollection::create(ScriptExecutionContext* context, CollectionStrategy strategy, bool localOnly)
{
    return adoptRef(new MOMDeviceCollection(context, strategy, NULL, localOnly));
}

PassRefPtr<MOMDeviceCollection> MOMDeviceCollection::create(ScriptExecutionContext* context, layers_string matchUri, bool localOnly)
{
    return adoptRef(new MOMDeviceCollection(context, STRATEGY_ALL, matchUri, localOnly));
}

MOMDeviceCollection::MOMDeviceCollection(ScriptExecutionContext* context, CollectionStrategy strategy, layers_string matchUri, bool localOnly)
    : m_strategy(strategy),
      m_matchUri(layers_string_copy(matchUri)),
      m_localOnly(localOnly),
      m_context(context)
{
}

MOMDeviceCollection::~MOMDeviceCollection()
{
    layers_string_dispose(m_matchUri);
}

/* ------------------------------------------------------------------------ */

int MOMDeviceCollection::length() const
{
    return mom_driver_count(s_device_filter_list[m_strategy], m_matchUri, m_localOnly);
}

PassRefPtr<MOMDevice> MOMDeviceCollection::item(int index)
{
    mom_driver mdh = mom_driver_get_device(s_device_filter_list[m_strategy], m_matchUri, m_localOnly, index);

    //exception on [] index not supported
    //MOMException::setFromMOMError(mom_err_NOTPRESENT, ec);
    if (!mdh)
        return nullptr;
    
    return MOMDevice::create(m_context, mdh);
}

} // namespace WebCore

#endif // ENABLE(RMAI)
