#pragma once

#include "kpi_player.h"
#include "kpi_impl.h"
#include <strmif.h>
#include <uuids.h>
#include <control.h>

class KbMovPlayerBase : public KbKpiUnknownImpl<IKpiPlayer>
{
protected:
    static bool s_bMaximized;
    enum {
        COMMAND_CREATEWINDOW = 0,
        COMMAND_STOP,
        COMMAND_PAUSE,
        COMMAND_RESTART,
        COMMAND_RUN,
        COMMAND_QUIT,
        COMMAND_GET_STATUS,
        COMMAND_GET_POSITION,
        COMMAND_GET_VIDEOPOS,
        COMMAND_SET_VIDEOPOS,
        COMMAND_SET_VOLUME,
        COMMAND_SET_TOPMOST,
        COMMAND_SET_TEMPO,
        COMMAND_SET_VISIBLE,
    };
    struct{
        int    nCommand;
        WPARAM wParam;
        LPARAM lParam;
    }m_Command;
    KPI_MEDIAINFO   m_MediaInfo;
    IKpiFile       *m_pFile;
    IKpiConfig     *m_pConfig;
    HANDLE          m_hThread;
    DWORD           m_dwThreadId;
    HANDLE          m_hEvent;
    HANDLE          m_hEventDone;
    HWND            m_hWnd;
    HWND            m_hWndDraw;//描画するウィンドウ
    wchar_t         m_szClassName[128];
    int             m_nLeft;
    int             m_nTop;
    int             m_nWidth;
    int             m_nHeight;
    int             m_nStatus;
    int             m_nTempo;
    int             m_nVolume;
    int             m_nRestoredLeft;
    int             m_nRestoredTop;
    int             m_nRestoredWidth;
    int             m_nRestoredHeight;
    bool            m_bRestoredTopMost;
    bool            m_bFullScreen;
    bool            m_bVisible;
    bool            m_bWindowInitialized;
    void __fastcall RegisterWndClass(void);
    void __fastcall UnregisterWndClass(void);
    void __fastcall UpdateWindowPosition(int nLeft, int nTop, int nWidth, int nHeight, bool bTopMost);
    void __fastcall GetVideoPos(int *pnLeft, int *pnTop, int *pnVideoWidth, int *pnVideoHeight);
    void __fastcall CreateVideoWindow(void);
    void __fastcall CreateVideoWindow(int X, int Y, int nWidth, int nHeight);
    static unsigned __stdcall ThreadProc(void* pvParam);
    virtual void __fastcall ThreadProc(void) = 0;

    void __fastcall SendCommand(int nCommand, WPARAM wParam, LPARAM lParam);
    static LRESULT CALLBACK VideoWindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
    virtual LRESULT CALLBACK VideoWindowProcInternal(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
    void __fastcall SetFullScreen(bool bFullScreen);
public:
    BOOL __fastcall Open(IKpiConfig *pConfig, IKpiFile *pFile, IKpiFolder *pFolder);
    DWORD WINAPI Select(DWORD dwNumber,
                        const KPI_MEDIAINFO **ppInfo,
                        IKpiTagInfo *pTagInfo,
                        DWORD dwTagGetFlags);
    BOOL  WINAPI Play(DWORD dwStartpos, BOOL bVisible);
    BOOL  WINAPI Pause(void);
    BOOL  WINAPI Restart(void);
    void  WINAPI Stop(void);
    DWORD WINAPI GetPosition(void);
    DWORD WINAPI GetStatus(void);
    void  WINAPI SetTempo(int Tempo);
    void  WINAPI SetVolume(int Volume);
    void  WINAPI SetVisible(BOOL bVisible);
    void  WINAPI GetWindowPosition(int *pLeft,  int *pTop,
                                   int *pVideoWidth, int *pVideoHeight);
    void  WINAPI SetWindowPosition(int nLeft, int nTop,
                                   int nVideoWidth, int nVideoHeight);
    void  WINAPI SetTopMost(BOOL bTopMost);
    void  WINAPI SetRepeat(BOOL bRepeat){}
    DWORD WINAPI UpdateConfig(void *pvReserved){return 0;}//設定が変更されたら呼ばれる
    KbMovPlayerBase(void);
    ~KbMovPlayerBase(void);
};
