#include <windows.h>
#include <stdio.h>
#include "kbsid_module.h"
#include "kbsid_decoder.h"
#include "kbsid_config.h"

#ifdef _DEBUG
#define LEAK_CHECK //メモリリーク検出（しない場合はコメントアウト）
#endif
#ifdef LEAK_CHECK
#include <crtdbg.h>
#endif
//////////////////////////////////////////////////////////////////////////////
#ifdef _DEBUG
static const DWORD kbsid_Version = 0x7FFFFFFF;//デバッグ時は優先度最大
#define KBSID_VERSION_STR L"0x7FFFFFFF(Debug)"
#else
static const DWORD kbsid_Version = 4;
#define KBSID_VERSION_STR L"0.04"
#endif

static const wchar_t kbsid_Description[] = L"SID Decoder v" KBSID_VERSION_STR " based on sidplay.kpi v1.36.57r7, libsidplay 1.36.60, reSID 0.16";
static const wchar_t kbsid_Copyright[] =   L"kbsid.kpi Copyright(C) 2016-2019 Kobarin\n"
                                           L"sidplay.kpi v1.36.57r7 Copyright(C) 2002 Mamiya\n"
                                           L"SIDPLAY, a C64 music player and SID chip emulator Copyright (C) 1994-1997 Michael Schwendtc\n"
                                           L"reSID, a MOS6581 SID emulator engine. Copyright (C) 2002 Dag Lem";

// {6FD5EE4E-6147-4B70-9F57-5A259438D688}
static const GUID kbsid_GUID = 
{ 0x6fd5ee4e, 0x6147, 0x4b70, { 0x9f, 0x57, 0x5a, 0x25, 0x94, 0x38, 0xd6, 0x88 } };

static const DWORD kbsid_MultipleInstance = KPI_MULTINST_ONE;
static const wchar_t kbsid_SupportExts[] = L".sid/.c64";
static const wchar_t kbsid_MultiSongExts[] = L".sid/.c64";
static const DWORD kbsid_SupportTagInfo = KPI_DECODER_MODULEINFO::TAG_TITLE | //未対応ならコメントアウト
                                          //KPI_DECODER_MODULEINFO::TAG_EXTRA | //未対応ならコメントアウト
                                          0;
static const DWORD kbsid_SupportConfig = 1;
static void* kbsid_Reserved1 = NULL;
static void* kbsid_Reserved2 = NULL;

HINSTANCE g_hKpi = NULL;
//////////////////////////////////////////////////////////////////////////////
KbSidDecoderModule::KbSidDecoderModule(IKpiConfig *pConfig)
    :m_ModuleInfo{
        sizeof(KPI_DECODER_MODULEINFO), //cb(=sizeof(KPI_DECODER_MODULEINFO))
        KPI_DECODER_MODULE_VERSION,     //dwModuleVersion(=KPI_DECODER_MODULE_VERSION)
        kbsid_Version,
        kbsid_MultipleInstance,
        kbsid_GUID,
        kbsid_Description,
        kbsid_Copyright,
        kbsid_SupportExts,
        kbsid_MultiSongExts,
        kbsid_Reserved1,
        kbsid_Reserved2,
        kbsid_SupportTagInfo,
        kbsid_SupportConfig,
        0, 0, 0, 0                   //reserved
     }
{
    pConfig->AddRef();
    m_pConfig = pConfig;
}
//////////////////////////////////////////////////////////////////////////////
KbSidDecoderModule::~KbSidDecoderModule(void)
{
    m_pConfig->Release();
}
//////////////////////////////////////////////////////////////////////////////
void  WINAPI KbSidDecoderModule::GetModuleInfo(const KPI_DECODER_MODULEINFO **ppInfo)
{
    *ppInfo = &m_ModuleInfo;
}
//////////////////////////////////////////////////////////////////////////////
DWORD WINAPI KbSidDecoderModule::Open(const KPI_MEDIAINFO *cpRequest,
                                      IKpiFile     *pFile,   //音楽データ
                                      IKpiFolder   *pFolder, //音楽データがあるフォルダ
                                      IKpiDecoder **ppDecoder)
{
    KbSidDecoder *pDecoder = new KbSidDecoder(m_pConfig);
    DWORD dwCount = pDecoder->Open(cpRequest, pFile, pFolder);
    if(dwCount == 0){
        *ppDecoder = NULL;
        delete pDecoder;
        return 0;
    }
    *ppDecoder = pDecoder;
    return dwCount;
}
/////////////////////////////////////////////////////////////////////////////
BOOL  WINAPI KbSidDecoderModule::EnumConfig(IKpiConfigEnumerator *pEnumerator)
{
    return kbsid_EnumConfig(pEnumerator);
}
/////////////////////////////////////////////////////////////////////////////
DWORD WINAPI KbSidDecoderModule::ApplyConfig(const wchar_t *cszSection,//セクション名
                             const wchar_t *cszKey,    //キー名
                             INT64  nValue, //変更後の値(BOOL/INT の場合)
                             double dValue, //変更後の値(FLOAT の場合)
                             const wchar_t *cszValue)////変更後の値(文字列)
{
    return kbsid_ApplyConfig(m_pConfig, cszSection, cszKey, nValue, dValue, cszValue);
}
/////////////////////////////////////////////////////////////////////////////
HRESULT WINAPI kpi_CreateInstance(REFIID riid, void **ppvObject, IKpiUnknown *pUnknown)
{
    *ppvObject = NULL;
    if(!IsEqualIID(riid, IID_IKpiDecoderModule)){
        return E_NOINTERFACE;
    }
    IKpiConfig *pConfig;
    kpi_CreateConfig(pUnknown, &kbsid_GUID, NULL, &pConfig);
    IKpiDecoderModule *pDecoderModule = new KbSidDecoderModule(pConfig);
    *ppvObject = pDecoderModule;
    pConfig->Release();
    return S_OK;
}
/////////////////////////////////////////////////////////////////////////////
#if 0
BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:{
        g_hKpi = hModule;
#ifdef LEAK_CHECK
        //メモリリーク検出
        int tmpDbgFlag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
        //tmpDbgFlag |= _CRTDBG_DELAY_FREE_MEM_DF;
        tmpDbgFlag |= _CRTDBG_LEAK_CHECK_DF;
        //tmpDbgFlag |= _CRTDBG_CHECK_ALWAYS_DF;
        _CrtSetDbgFlag(tmpDbgFlag);
#endif
        DisableThreadLibraryCalls(hModule);
        break;
    }
	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		break;
	}
	return TRUE;
}
#endif