#include <windows.h>
#include <wchar.h>

#ifdef _DEBUG
#define LEAK_CHECK //メモリリーク検出（しない場合はコメントアウト）
#endif
#ifdef LEAK_CHECK
#include <crtdbg.h>
#endif

#include "kbmpg123_module.h"
#include "kbmpg123_decoder.h"
#include "kbmpg123_config.h"

#ifdef _DEBUG
static const DWORD kbmpg123_Version = 0x7FFFFFFF; //デバッグ時は優先度最大
#define KBMPG123_VERSION_STR L"0x7FFFFFFF(Debug)"
#else
static const DWORD kbmpg123_Version = 229;
#define KBMPG123_VERSION_STR L"2.29"
#endif

static const wchar_t kbmpg123_Description[] = L"mpg123 - MPEG Audio Decoder v" KBMPG123_VERSION_STR " based on libmpg123 1.32.6";
static const wchar_t kbmpg123_Copyright[] =   L"kbmpg123.kpi Copyright (C) 2015-2024 Kobarin\n"
                                              L"libmpg123 Copyright(C) 1995-2024 Thomas Orgis, Michael Hipp";
// {F1019B7F-9B4B-42E3-9E89-51F52ABE967E}
static const GUID kbmpg123_GUID = 
{ 0xf1019b7f, 0x9b4b, 0x42e3, { 0x9e, 0x89, 0x51, 0xf5, 0x2a, 0xbe, 0x96, 0x7e } };

static const DWORD kbmpg123_MultipleInstance = KPI_MULTINST_INFINITE;//同時に何個でも作成可能
                                            //KPI_MULTINST_ZERO;  //常に別プロセス起動
                                            //KPI_MULTINST_ONE;   //2個目以降は別プロセス起動
                                            //KPI_MULTINST_UNIQUE;//プロセスをまたいでも１つまで
                                            //KPI_MULTINST_ZERO + N;//(N+1)個目以降は別プロセス起動
                                            //↑どれか１つを選択
static const wchar_t kbmpg123_SupportExts[] = L".mp1/.mp2/.mp3/.rmp";
static const wchar_t kbmpg123_MultiSongExts[] = L"";
static const DWORD kbmpg123_SupportTagInfo = KPI_DECODER_MODULEINFO::TAG_TITLE | //未対応ならコメントアウト
                                             KPI_DECODER_MODULEINFO::TAG_EXTRA | //未対応ならコメントアウト
                                             0;
static const DWORD kbmpg123_SupportConfig = 1;
static void* kbmpg123_Reserved1 = NULL;
static void* kbmpg123_Reserved2 = NULL;
//////////////////////////////////////////////////////////////////////////////
KbMpg123DecoderModule::KbMpg123DecoderModule(IKpiConfig *pConfig)
    :m_ModuleInfo{
        sizeof(KPI_DECODER_MODULEINFO), //cb(=sizeof(KPI_DECODER_MODULEINFO))
        KPI_DECODER_MODULE_VERSION,     //dwModuleVersion(=KPI_DECODER_MODULE_VERSION)
        kbmpg123_Version,
        kbmpg123_MultipleInstance,
        kbmpg123_GUID,
        kbmpg123_Description,
        kbmpg123_Copyright,
        kbmpg123_SupportExts,
        kbmpg123_MultiSongExts,
        kbmpg123_Reserved1,
        kbmpg123_Reserved2,
        kbmpg123_SupportTagInfo,
        kbmpg123_SupportConfig,
        0, 0, 0, 0                      //reserved

     }
{
    mpg123_init();
    m_pConfig = pConfig;
    pConfig->AddRef();
}
//////////////////////////////////////////////////////////////////////////////
KbMpg123DecoderModule::~KbMpg123DecoderModule(void)
{
    mpg123_exit();
    m_pConfig->Release();
}
//////////////////////////////////////////////////////////////////////////////
void  WINAPI KbMpg123DecoderModule::GetModuleInfo(const KPI_DECODER_MODULEINFO **ppInfo)
{
    *ppInfo = &m_ModuleInfo;
}
//////////////////////////////////////////////////////////////////////////////
DWORD WINAPI KbMpg123DecoderModule::Open(const KPI_MEDIAINFO *cpRequest,
    IKpiFile     *pFile,   //音楽データ
    IKpiFolder   *pFolder, //音楽データがあるフォルダ
    IKpiDecoder **ppDecoder)
{
    KbMpg123Decoder *pDecoder = new KbMpg123Decoder;
    DWORD dwCount = pDecoder->Open(cpRequest, pFile, pFolder, m_pConfig);
    if(dwCount == 0){
        *ppDecoder = NULL;
        delete pDecoder;
        return 0;
    }
    *ppDecoder = pDecoder;
    return dwCount;
}
//////////////////////////////////////////////////////////////////////////////
BOOL  WINAPI KbMpg123DecoderModule::EnumConfig(IKpiConfigEnumerator *pEnumerator)
{
    return kbmpg123_EnumConfig(m_pConfig, pEnumerator);
}
//////////////////////////////////////////////////////////////////////////////
DWORD WINAPI KbMpg123DecoderModule::ApplyConfig(const wchar_t *cszSection,//セクション名
                                                const wchar_t *cszKey,    //キー名
                                                INT64  nValue, //変更後の値(BOOL/INT の場合)
                                                double dValue, //変更後の値(FLOAT の場合)
                                                const wchar_t *cszValue)//変更後の値(文字列)
{
    return kbmpg123_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 = NULL;
    kpi_CreateConfig(pUnknown, &kbmpg123_GUID, NULL, &pConfig);
    IKpiDecoderModule *pDecoderModule = new KbMpg123DecoderModule(pConfig);
    *ppvObject = pDecoderModule;
    pConfig->Release();
    return S_OK;
}
/////////////////////////////////////////////////////////////////////////////
//DllMain
/////////////////////////////////////////////////////////////////////////////
BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
    switch (ul_reason_for_call) {
    case DLL_PROCESS_ATTACH:
    {
#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_PROCESS_DETACH:
        break;
    }
    return TRUE;
}
