#pragma once
#include "resource.h"
#include <vector>
#include "resource.h"
#include "PreviewPane.h"
#include "CaptureManager.h"

class CHwsCaptureMainDlg : public CDialogEx
{
public:
    enum { IDD = IDD_MAIN_DIALOG };
    CHwsCaptureMainDlg(CWnd* parent = nullptr);

protected:
    BOOL OnInitDialog() override;
    void OnOK() override {}
    void OnCancel() override;
    afx_msg void OnPaint();
    afx_msg void OnSize(UINT nType, int cx, int cy);
    afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
    afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
    afx_msg void OnRButtonUp(UINT nFlags, CPoint point);
    afx_msg LRESULT OnPreviewClick(WPARAM wParam, LPARAM lParam);
    afx_msg LRESULT OnPreviewDblClick(WPARAM wParam, LPARAM lParam);
    afx_msg LRESULT OnPreviewRButton(WPARAM wParam, LPARAM lParam);
    afx_msg void OnTimer(UINT_PTR nIDEvent);
    afx_msg void OnDestroy();
    afx_msg void OnClose();
    afx_msg BOOL OnEraseBkgnd(CDC* pDC);
    afx_msg void OnSourceVideo();
    afx_msg void OnSourceAudio();
    afx_msg void OnSourceCloseAll();
    afx_msg void OnView1();
    afx_msg void OnView4();
    afx_msg void OnView8();
    afx_msg void OnView16();
    afx_msg void OnOsdText();
    afx_msg void OnOsdImage();
    afx_msg void OnOsdDateTime();
    afx_msg void OnOsdRemoveLast();
    afx_msg void OnOsdClear();
    afx_msg void OnRecorderStart();
    afx_msg void OnRecorderStop();
    afx_msg void OnRecorderVideoCallback();
    afx_msg void OnRecorderAudioCallback();
    afx_msg void OnSnapshotSettings();
    afx_msg void OnSnapshotCapture();
    afx_msg void OnCropAdd();
    afx_msg void OnCropRemove();
    afx_msg void OnAudioMonitorOn();
    afx_msg void OnAudioMonitorOff();
    afx_msg void OnExitApp();
    afx_msg void OnContextVideoSource();
    afx_msg void OnContextAudioSource();
    afx_msg void OnContextAudioMonitorOn();
    afx_msg void OnContextAudioMonitorOff();
    afx_msg void OnContextRemoveVideo();
    afx_msg void OnSourceSimpleTest();
    DECLARE_MESSAGE_MAP()

private:
    void BuildMainMenu();
    void LayoutSlots();
    enum { MAX_PREVIEW_PANES = 16 };
    void EnsurePreviewPanes(int count);
    void SetViewCount(int count);
    int HitTestSlot(CPoint pt) const;
    void SelectSlot(int index);
    void ShowSlotContextMenu(int index, CPoint ptClient);
    void ToggleFullscreenSlot(int index);
    void UpdateTitle();
    void DestroyPreviewPanes();
    void SafeShutdown();
    bool ShowVideoSourceDialog(HWS_VIDEO_INPUT_CONFIG& cfg);
    bool ShowAudioSourceDialog(int& deviceIndex);
    bool ShowCropDialog(HWS_VIDEO_CROP_RECT& rc);

    HMENU m_hMainMenu = NULL;
    std::vector<CPreviewPane*> m_panes;
    CaptureManager m_mgr;
    int m_viewCount = 1;
    int m_selectedSlot = 0;
    int m_fullscreenSlot = -1;
    CRect m_restoreRect;
    bool m_shutdownDone = false;
};
