#pragma once

#include "mac_sdk/source/shared/all.h"
#include "mac_sdk/source/shared/io.h"
#include "mac_sdk/source/maclib/maclib.h"
#include "kpi_decoder.h"
#include "kpi_impl.h"

class CApeIO : public APE::CIO
{
public:
//CIO からの派生
    // construction / destruction
    CApeIO();
    ~CApeIO();
    // open / close
    int Open(const wchar_t *wcszFileName, bool bOpenReadOnly = false) { return 1; }//呼ばれない
    int Close();

    // read / write
    int Read(void * pBuffer, unsigned int nBytesToRead, unsigned int * pBytesRead);
    int Write(const void * pBuffer, unsigned int nBytesToWrite, unsigned int * pBytesWritten);

    // seek
    int Seek(APE::int64 nPosition, APE::SeekMethod nMethod);

    // creation / destruction
    int Create(const wchar_t * pName) { return ERROR_UNDEFINED; }//呼ばれない
    int Delete();

    // other functions
    int SetEOF();
    unsigned char* GetBuffer(int* pnBufferBytes){return NULL;}

    // attributes
    APE::int64 GetPosition();
    APE::int64 GetSize();
    int GetName(wchar_t *pBuffer);//{return ERROR_UNDEFINED;}
//CIO からの派生ここまで
    int Open(IKpiFile *pFile);
private:
    IKpiFile *m_pFile;
};

class KbMacDecoder : public KbKpiUnknownImpl<IKpiDecoder>
{
    CApeIO m_IO;
    APE::IAPEDecompress *m_pApeDecompress;
    KPI_MEDIAINFO m_MediaInfo;
    UINT64 m_qwStartSample;
    UINT64 m_qwEndSample;
    UINT64 m_qwCurrentSample;
public:
    DWORD  __fastcall Open(const KPI_MEDIAINFO *cpMediaInfo, 
                           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 *Buffer, DWORD dwSizeSample);
    DWORD  WINAPI UpdateConfig(void *pvReserved){return 0;}
    KbMacDecoder(void);
    ~KbMacDecoder(void);
};

