/* dxdiagn
 *
 * Copyright 2003 TransGaming Technologies
 *
 * David Hammerton (david@transgaming.com)
 */

#include "windef.h"
#include "winbase.h"
#include "winuser.h"
#include "winreg.h"
#include "objbase.h"
#include "wine/debug.h"
#include "winerror.h"

#include "dxdiag.h"
#include "dxdiagn_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(dxdiagn);

static ICOM_VTABLE(IDxDiagProvider) dxDiagProviderVT;
extern IDxDiagContainerImpl container_root;

HRESULT WINAPI DxDiagProvider_QueryInterface(PDXDIAGPROVIDER iface,
                                             REFIID riid, LPVOID *obj)
{
    ICOM_THIS(IDxDiagProviderImpl, iface);
    TRACE("(%p)->(%s, %p\n", iface, debugstr_guid(riid), obj);
    if (IsEqualGUID(&IID_IUnknown, riid) ||
        IsEqualGUID(&IID_IDxDiagProvider, riid))
    {
        This->ref++;
        *obj = This;
        return S_OK;
    }
    else
    { /* it could be IID_IDxDiagContainer */
        FIXME("(%p)->(%s, %p): no interface\n", iface, debugstr_guid(riid), obj);
        return E_NOINTERFACE;
    }

}

ULONG WINAPI DxDiagProvider_AddRef(PDXDIAGPROVIDER iface)
{
    ICOM_THIS(IDxDiagProviderImpl, iface);
    TRACE("(%p)->()\n", iface);
    return ++This->ref;
}

ULONG WINAPI DxDiagProvider_Release(PDXDIAGPROVIDER iface)
{
    ICOM_THIS(IDxDiagProviderImpl, iface);
    TRACE("(%p)->()\n", iface);
    if (--This->ref) return This->ref;

    HeapFree(GetProcessHeap(), 0, This);
    return 0;
}

HRESULT WINAPI DxDiagProvider_Initialize(PDXDIAGPROVIDER iface, DXDIAG_INIT_PARAMS *pParams)
{
    ICOM_THIS(IDxDiagProviderImpl, iface);
    TRACE("(%p)->(%p)\n", iface, pParams);

    if (!pParams)
        return E_POINTER;

    This->initilized = TRUE;
    return S_OK;
}

HRESULT WINAPI DxDiagProvider_GetRootContainer(PDXDIAGPROVIDER iface, IDxDiagContainer **ppInstance)
{
    ICOM_THIS(IDxDiagProviderImpl, iface);
    TRACE("(%p)->(%p)\n", iface, ppInstance);

/*    if (!This->initilized) return E_CO_UNINITIALIZED;*/
    if (!This->initilized) return E_NOINTERFACE;
    /* we should be returning E_CO_UNINITIALIZED here but I can't find it anywhere */

    *ppInstance = (IDxDiagContainer *)&container_root;
    return S_OK;
}

HRESULT DXDIAGN_CreateDxDiagProvider(IUnknown *pOuter, REFIID riid, LPVOID *ppobj)
{
    HRESULT hr;
    IDxDiagProviderImpl *ipDXDP;
    TRACE("InterfaceId: %s\n", debugstr_guid(riid));
    ipDXDP = (IDxDiagProviderImpl *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
                                               sizeof(IDxDiagProviderImpl));
    if (ipDXDP == NULL) return E_OUTOFMEMORY;
    ICOM_VTBL(ipDXDP) = &dxDiagProviderVT;
    IDxDiagProvider_AddRef((IDxDiagProvider *)ipDXDP);
    hr = IDxDiagProvider_QueryInterface((IDxDiagProvider *)ipDXDP, riid, ppobj);
    IDxDiagProvider_Release((IDxDiagProvider *)ipDXDP);

    if (SUCCEEDED(hr))
        return S_OK;
    else
        return E_NOINTERFACE;
}

static ICOM_VTABLE(IDxDiagProvider) dxDiagProviderVT =
{
    ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
    DxDiagProvider_QueryInterface,
    DxDiagProvider_AddRef,
    DxDiagProvider_Release,
    DxDiagProvider_Initialize,
    DxDiagProvider_GetRootContainer
};

