#include <windows.h>
#include <stdio.h>
#include "kbmod_config.h"

static const wchar_t SEC_GENERAL[] = L"General";

static const wchar_t KEY_DISABLE_EXTS[] = L"DisableExts";
static const wchar_t KEY_SAMPLERATE[] = L"SampleRate";
static const wchar_t KEY_STEREO_SEPARATION[] = L"StereoSeparation";
static const wchar_t KEY_INTERPOLATION[] = L"Interpolation";
static const wchar_t KEY_USE_AMIGA_RESAMPLER[] = L"UseAmigaResampler";
static const wchar_t KEY_MULTISONG_EXTS[] = L"MultisongExts";

static const DWORD SAMPLERATE_DEFAULT = 44100;
static const wchar_t SZ_SAMPLERATE_DEFAULT[] = L"44100";
static const DWORD STEREO_SEPARATION_DEFAULT = 100;
static const wchar_t SZ_STEREO_SEPARATION_DEFAULT[] = L"100";
static const DWORD INTERPOLATION_DEFAULT = 8;
static const wchar_t SZ_INTERPOLATION_DEFAULT[] = L"8";
static const bool USE_AMIGA_RESAMPLER_DEFAULT = false;
static const wchar_t SZ_USE_AMIGA_RESAMPLER_DEFAULT[] = L"0";

extern const wchar_t *kbmod_MultiSongExts;

//extern HINSTANCE g_hKpi;
/////////////////////////////////////////////////////////////////////////////
BOOL __fastcall kbmod_EnumConfig(IKpiConfigEnumerator *pEnumerator)
{
    static const KPI_CFG_SECTION sec[] = {
        {SEC_GENERAL, NULL, L"kbmod.kpi に関する一般的な設定です。"},
        {NULL, NULL, NULL}
    };
    static const KPI_CFG_KEY keys[] = {
    //   type, section, key, keydesc, default, min, max, list, listdesc, help
        {KPI_CFG_TYPE_INT, SEC_GENERAL, KEY_SAMPLERATE, L"再生周波数",
            SZ_SAMPLERATE_DEFAULT, NULL, NULL,
            L"44100\t48000\t88200\t96000\t176000\t192000\t"
            L"-44100\t-48000\t-88200\t-96000\t-176000\t-192000",//list
            L"44100\t48000\t88200\t96000\t176000\t192000\t"
            L"44100(要求を無視)\t48000(要求を無視)\t88200(要求を無視)\t96000(要求を無視)\t176000(要求を無視)\t192000(要求を無視)",//listdesc
            //NULL,//listdesc
            L"プレイヤーからの要求が 0 のときの再生周波数です。\r\n"
            L"負の値を指定するとプレイヤーからの要求を無視します。\r\n"
            L""}, //help
        /*{KPI_CFG_TYPE_EXT, SEC_GENERAL, KEY_MULTISONG_EXTS, L"1ファイル複数曲の拡張子",
            kbmod_MultiSongExts, NULL, L"4096",
            NULL,//list
            NULL,//listdesc
            L"1ファイル複数曲と見なす形式の拡張子を指定します。\r\n"
            L"1ファイル複数曲と見なさないが実際はそうである場合、\r\n"
            L"曲の選択は出来ませんが、全曲を1回ずつ再生します。\r\n"
            L"曲長は全曲の合計になります。\r\n"
            L""}, //help*/
        {KPI_CFG_TYPE_INT, SEC_GENERAL, KEY_STEREO_SEPARATION, L"Stereo Separation(%)",
            SZ_STEREO_SEPARATION_DEFAULT, L"0", L"200", //default/min/max
            L"0\t20\t40\t60\t80\t100\t120\t140\t160\t180\t200", //list
            L"0%\t20%\t40%\t60%\t80%\t100%\t120%\t140%\t160%\t180%\t200%", //listdesc
            L""},//help
        {KPI_CFG_TYPE_INT, SEC_GENERAL, KEY_INTERPOLATION, L"Interpolation filter length",
            SZ_INTERPOLATION_DEFAULT, NULL, NULL, //default/min/max
            L"1\t2\t4\t8", L"1 Tap (Nearest)\t2 Tap (Linear)\t4 Tap (Cubic)\t8-Tap (Polyphase FIR)", //list/listdesc
            L"1: 1 Tap (Nearest)\r\n"
            L"2: 2 Tap (Linear)\r\n"
            L"4: 4 Tap (Cubic)\r\n"
            L"8: 8 Tap (Polyphase FIR)\r\n"
            L"\r\n"
            L"値が大きいほど高音質となります。\r\n"
            L""},//help
        {KPI_CFG_TYPE_BOOL, SEC_GENERAL, KEY_USE_AMIGA_RESAMPLER, L"Use Amiga Resampler",
            SZ_USE_AMIGA_RESAMPLER_DEFAULT, NULL, NULL, //default/min/max
            NULL, NULL, //list/listdesc
            L""},//help
        {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}
    };
    for(int i = 0; sec[i].cszSection; i++){
        pEnumerator->EnumSection(&sec[i]);
    }
    for(int i = 0; keys[i].cszSection; i++){
        pEnumerator->EnumKey(&keys[i]);
    }
    return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
DWORD __fastcall kbmod_ApplyConfig(IKpiConfig *pConfig,
                               const wchar_t *cszSection,//セクション名
                               const wchar_t *cszKey,    //キー名
                               INT64  nValue, //変更後の値(BOOL/INT の場合)
                               double dValue, //変更後の値(FLOAT の場合)
                               const wchar_t *cszValue)////変更後の値(文字列)
{
    if(_wcsicmp(cszSection, SEC_GENERAL) == 0){
        if(_wcsicmp(cszSection, KEY_MULTISONG_EXTS) == 0){
            pConfig->SetStr(cszSection, cszKey, cszValue);
            return KPI_CFGRET_RELOAD_KPI;
        }
    }
    pConfig->SetStr(cszSection, cszKey, cszValue);
    return KPI_CFGRET_RELOAD_DATA;
}
/////////////////////////////////////////////////////////////////////////////
void __fastcall kbmod_GetSettings(IKpiConfig *pConfig, kbmod_Settings *pSettings)
{
    pSettings->nSampleRate = 
        pConfig->GetInt(SEC_GENERAL, KEY_SAMPLERATE, SAMPLERATE_DEFAULT);
    pSettings->nInterpolation =
        pConfig->GetInt(SEC_GENERAL, KEY_INTERPOLATION, INTERPOLATION_DEFAULT);
    pSettings->bUseAmigaResampler = pConfig->GetInt(SEC_GENERAL, KEY_USE_AMIGA_RESAMPLER, USE_AMIGA_RESAMPLER_DEFAULT)!=0;
    pSettings->nStereoSeparation = pConfig->GetInt(SEC_GENERAL, KEY_STEREO_SEPARATION, STEREO_SEPARATION_DEFAULT);
    //範囲チェック
    switch(pSettings->nSampleRate){
        case 44100: case 48000: case 88200: case 96000: case 176400: case 192000:
        case -44100: case -48000: case -88200: case -96000: case -176400: case -192000:break;
        default: pSettings->nSampleRate = 44100;break;
    }
    switch (pSettings->nInterpolation) {
        case 1: case 2: case 4: case 8: break;
        default: pSettings->nInterpolation = INTERPOLATION_DEFAULT;break;
    }
    if(pSettings->nStereoSeparation < 0){
        pSettings->nStereoSeparation = 0;
    }
    else if(pSettings->nStereoSeparation > 200){
        pSettings->nStereoSeparation = 200;
    }
}
/////////////////////////////////////////////////////////////////////////////
/*
void __fastcall kbmod_GetMultiSongExts(IKpiConfig *pConfig, wchar_t **ppszMultiSongExts)
{
    wchar_t szMultiSongExts[1024];
    pConfig->GetStr(SEC_GENERAL, KEY_MULTISONG_EXTS, szMultiSongExts, sizeof(szMultiSongExts), kbmod_MultiSongExts);
    *ppszMultiSongExts = _wcsdup(szMultiSongExts);
}
*/
/////////////////////////////////////////////////////////////////////////////
