#pragma once

#include "kpi_decoder.h"
#include "kpi_impl.h"

#include "kbvgm_config.h"
#include "player/playera.hpp"
#include "utils/DataLoader.h"

class KbVgmDecoder : public KbKpiUnknownImpl<IKpiDecoder>
{
private:
    KPI_MEDIAINFO  m_MediaInfo;
    PlayerA*       m_pPlayer;
    DATA_LOADER   *m_pLoader;
    IKpiFolder    *m_pFolder;
    IKpiFile      *m_pFile;
    IKpiConfig    *m_pConfig;
    kbvgm_Settings m_Settings;
    BOOL           m_bEndPlay;//PlayerEventCallback の evtParam に PLREVT_END が渡されたら true
    void  __fastcall GetTagInfo(IKpiTagInfo *pTagInfo, DWORD dwTagGetFlags);

    static DATA_LOADER* FileRequestCallback(void* userParam, PlayerBase* player, const char* fileName)
    {
        KbVgmDecoder *_this = (KbVgmDecoder*)userParam;
        return _this->FileRequestCallback(player, fileName);
    }
    DATA_LOADER* FileRequestCallback(PlayerBase* player, const char* fileName);
    static UINT8 PlayerEventCallback(PlayerBase* player, void* userParam, UINT8 evtType, void* evtParam)
    {
        KbVgmDecoder* _this = (KbVgmDecoder*)userParam;
        return _this->PlayerEventCallback(player, evtType, evtParam);
    }
    UINT8  PlayerEventCallback(PlayerBase* player, UINT8 evtType, void* evtParam);

    void ApplyCfg_General(void);
    void ApplyCfg_Chip(void);

public:
    DWORD __fastcall Open(const KPI_MEDIAINFO* cpRequest,//再生周波数/ループ数要求等
                          IKpiConfig *pConfig, 
                          IKpiFile   *pFile,
                          IKpiFolder *pFolder);
    DWORD  WINAPI Select(DWORD dwNumber,
                         const KPI_MEDIAINFO **ppMediaInfo,
                         IKpiTagInfo *pTagInfo,
                         DWORD dwTagGetFlags);
    UINT64 WINAPI Seek(UINT64 qwPosSample, DWORD dwFlag);
    DWORD  WINAPI Render(BYTE *pBuffer, DWORD dwSizeSample);
    DWORD  WINAPI UpdateConfig(void *pvReserved){return 0;}
    KbVgmDecoder(void);
    ~KbVgmDecoder(void);

};

