/*	Copyright (C) G. Jespersen
	See Licence.txt.*/

/////////////////////////////////////////////////////////////////////////////
// DsLavDoc.h : interface of the CDsLavDoc class
//

#if !defined(AFX_DSLAVDOC_H__FCE679B9_527D_47E9_98CF_0282BACE5F6F__INCLUDED_)
#define AFX_DSLAVDOC_H__FCE679B9_527D_47E9_98CF_0282BACE5F6F__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

// For GetMainFrame()
#include "MainFrm.h"

// Media data
#include "MediaDefs.h"

// LAV Setup
#include "LavSetup.h"

// RadioBrowser - test
#include "RadioBrowseDb.h"
class CRadioBrowseUpdateDlg; //foreward for dlg ptr.

// Tell linker to link with version.lib.
// For GetVersion().
#pragma comment(linker, "/defaultlib:version.lib")

// View Update enums.
#define LIST_UPDATE			90 //0x5A
#define TREE_UPDATE			91
#define LIST_FONT_UPDATE		92 
#define TREE_FONT_UPDATE		93 
#define LIST_DISC_UPDATE		94 
#define TREE_DISC_INSERT		95 
#define TREE_DISC_REMOVE		96 

// 7/22
// Radio Browser update progress ctrl steps - 
//  about 33 MB download 32,513 stations.
#define RB_UPDATE_PROG_STEPS 40

// EditMediaItem dlg strings
#define MEDIA_NEW_STR _T("New Media Item")
#define MEDIA_EDIT_STR _T("Edit Media Item")

// EditTopic dlg strings
#define TOPIC_NEW_STR _T("New Topic")
#define TOPIC_EDIT_STR _T("Edit Topic")

// Forward for GetView()
class CDsLavSplitterView;

// Worker Thread RB update function.
UINT RadioBrowseUpdateThread(LPVOID pParam);

// Worker Thread param for RB update.
struct THREADPARAM
{
	THREADPARAM() { Zero(); };
	void Zero()
	{
		hwndMain	= NULL; 
		hwndDlg		= NULL;
		pEvent		= NULL;
		pTempPath	= NULL;
		pRbUrl		= NULL;
		pFileSz		= NULL;
		pTempRB		= NULL;
	};
	HWND hwndMain;
	HWND hwndDlg;
    CEvent * pEvent;
	CString * pTempPath;
	CString * pRbUrl;
	DWORD * pFileSz;
	CRadioBrowseDb * pTempRB;
};


class CDsLavDoc : public CDocument
{
protected: // create from serialization only
	CDsLavDoc();
	DECLARE_DYNCREATE(CDsLavDoc)

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CDsLavDoc)
	public:
	virtual BOOL OnNewDocument();
	virtual void Serialize(CArchive& ar);
	virtual void OnCloseDocument();
	protected:
	virtual BOOL SaveModified();
	//}}AFX_VIRTUAL

// Generated message map functions
protected:
	//{{AFX_MSG(CDsLavDoc)
	afx_msg void OnOpenMediaFile();
	afx_msg void OnOpenMediaUrl();
	afx_msg void OnUpdateFileOpen(CCmdUI* pCmdUI);
	afx_msg void OnTestTest();
	afx_msg void OnToolsExport();
	afx_msg void OnToolsImport();
	afx_msg void OnFileSaveContents();
	afx_msg void OnSetupLavFilters();
	afx_msg void OnSetupListFont();
	afx_msg void OnSetupContentsFont();
	afx_msg void OnDvdNavigation();
	afx_msg void OnDvdPlayDvd();
	afx_msg void OnHelpDsLavHelp();
	afx_msg void OnToolsUpdateRb();
	afx_msg void OnToolsSearchRb();
	afx_msg void OnCheckForUpdates();
	afx_msg void OnSetupFolderLocations();
	afx_msg void OnUpdateDvdNavigation(CCmdUI* pCmdUI);
	afx_msg void OnUpdateDvdPlayDvd(CCmdUI* pCmdUI);
	afx_msg void OnDvdPlayCd();
	afx_msg void OnUpdateDvdPlayCd(CCmdUI* pCmdUI);
	//}}AFX_MSG

	/* Facilitate message routing from CControlsBar Dialog Bar. out 8/22-6
	afx_msg void OnPlayPause();
	afx_msg void OnMute();
	afx_msg void OnVolume();
	afx_msg void OnVolumeStatic();
	afx_msg void OnStop();
	afx_msg void OnTimeStatic();
	afx_msg void OnSliderVid();
	afx_msg void OnHidePane();*/

	DECLARE_MESSAGE_MAP()

// Attributes
public:

// Operations
public:
	CMainFrame * GetMainFrame();

// Media data.
public:
	CTopicTree m_topicTree;
	CTopicStrAliasAr m_aliasAr;

	BOOL LoadTopicTree();

	// Current Topic - used by TreeView and ListView.
	// Set in UpdateCurrentTopicMediaList().
	CTopic m_curTopic;
	CMediaArray m_favsMediaAr;			// all TopicTree media items flagged fav
	CNodesArArray m_favsNodesAr;		// nodes from fav media item's CTopic parent
	BOOL m_bCurTopicDrive;
	BOOL m_bCurTopicFavs;

	// Serialize TopicTree flag.
	BOOL m_bSaveTopicTree;

	// Version.
	BOOL GetVersion(CString & strVersion);

	// LAV Setup.
	CLavSetup m_lavSetup;

	// User settings.
	LOGFONT m_listLf;					// ListView font
	LOGFONT m_treeLf;					// TreeView font
	// Version 2 - 9/27/2021
	CString m_pathVideoFolder;
	CString m_pathContentsFiles;

	// User App Data Folder path.
	CString m_userAppDataPath;

	// Drive handling
	CStringArray m_extensionsAr;
	CString m_playDvdRootPath;			// Play from DVD menu item 8/22-3
	CString m_playCdDrive;				// Play from DVD menu item 8/22-3

	// Import playlist - from ListView context.
	void ImportMediaItemPlaylist(const CNodeArray & nodesAr, CMediaItem & mediaItem);

	// RadioBrowser - test
	CRadioBrowseDb m_radioBrowse;
	// See also: 
	//  OnToolsUpdateRb() 
	//  OnToolsSearchRb() 
	//  RadioBrowseUpdateThread()
	//  ReadRadioBrowserData()
	//  WriteRadioBrowserData() - todo path;
	//
	// Update RB.
	CRadioBrowseDb	m_tempRB;
	THREADPARAM		m_rbThreadParam;
	CEvent			m_rbThreadEvent;
	HANDLE			m_hRbThread;
	CString			m_tempPath;
	CString			m_rbUrl;
	BOOL			m_bRbUpdateInstalled;
	CString			m_rbServer;
	HWND			m_hwndDlg;
	void UpdateRadioBrowse(HWND hwndDlg);
	void CancelUpdate();
	void UpdateFinished();		// via CMainFrame WM_USER_THREAD_FINISHED 
	void UpdateAborted();		// via CMainFrame WM_USER_THREAD_ABORTED
	// Search RB
	int ProccessRbSearchResults(CString topic, int maxResults = 0);


	// Drive handlers - DVD USB
	void DriveDeviceChanged(CString drive, BOOL inserted);
	void TreeViewSelectedDrive(CString drivePath);
	BOOL AddDriveMediaItem(CString drivePath, CString mediaName, CMediaArray & mediaAr);
	BOOL AddDriveDirectory(CString discPath, CTopicArray & topicAr);
	void BuildExtensionsArray();
	BOOL ExtensionValid(CString filePath);
	BOOL DiskInDrive(BOOL isDVD);													// 8/22-3
	BOOL GetDiskPath(const CString & drivePath, CString & diskPath, BOOL isDVD);	// 8/22-3

	// Updates.
	int  RebuildFavoritesList();
	void ListViewSelectedTopic(CNodeArray nodesAr);
	void ListViewSortMediaItems(CNodeArray nodesAr, BOOL ascend);
	void TreeViewSelectedTopic(CNodeArray nodesAr);
	void TreeViewSelectedFavorites();
	void UpdateCurrentTopic(BOOL rebuildFavs, BOOL setListFavs,
							CTopic * pSetToTopic, BOOL rebuildTreeView, 
							BOOL rebuildListView);
	// File I/O
	BOOL ReadMediaRecordFormat(CString filePath, CString topic, int & nItemsRead);
	BOOL ReadPls(CString filePathUrl, CString topic, int & nItemsRead);
	BOOL ReadM3u(CString filePathUrl, CString topic, int & nItemsRead);
	BOOL ReadTopicTree();
	BOOL ReadTopicTree(CString path);
	BOOL ReadUserData();
	BOOL ReadUserData(CString path);
	BOOL WriteTopicTree();
	BOOL WriteTopicTree(CString path);
	BOOL WriteUserData();
	BOOL WriteUserData(CString path);
	BOOL WriteMediaRecordFormat(CString filePath, BOOL excludeDisc, BOOL writeUnicode,
								BOOL writeAlias, int & nWritten);
	BOOL WriteM3uFormat(CString filePath, BOOL writeUnicode, int & nWritten);
	// test radio browser
	BOOL WriteRadioBrowserData();
	BOOL ReadRadioBrowserData(CString path);


	// TopicTree handling
	BOOL SetMediaItemFavorites(const CNodeArray & nodesAr,
							   const CMediaItem & mediaItem, 
							   BOOL isFavorite, BOOL dropOnFavs = FALSE);
	BOOL DeleteMediaItem(const CNodeArray & nodesAr, CMediaItem & mediaItem);
	BOOL DeleteMediaItemMulti(const CNodeArray & nodesAr, CMediaArray & mediaAr);
	BOOL MoveMediaItem(const CMediaItem & mediaItem, const CNodeArray & nodesArSrc, 
					   const CNodeArray & nodesArDest);
	BOOL MoveMediaItemMulti(const CMediaArray & mediaAr, const CNodeArray & nodesArSrc, 
							const CNodeArray & nodesArDest);
	BOOL MoveTopic(const CNodeArray & nodesArSrc, const CNodeArray & nodesArDest, 
				   MOVE_TYPE moveType); // 9/22-1
	BOOL MoveTopicSibling(const CNodeArray & nodesArSrc, const CNodeArray & nodesArDest,
						  BOOL setAfter);
	void EditMediaItem(CNodeArray nodesArSrc, CMediaItem mediaItem, BOOL isNewMedia = FALSE);
	void EditTopic(const CNodeArray & nodesAr, MEDIA_TYPE type, BOOL isNewTopic);
	void DeleteTopic(const CNodeArray & nodesAr);
	void AddMediaFilesToTopic(const CNodeArray & nodesAr, MEDIA_TYPE type);

	// Misc - helpers
public:
	BOOL RouteCmdToAllViews(CView *pView, UINT nID, int nCode, void *pExtra,
							AFX_CMDHANDLERINFO *pHandlerInfo);
// Implementation
public:
	virtual ~CDsLavDoc();
#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif


};


/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_DSLAVDOC_H__FCE679B9_527D_47E9_98CF_0282BACE5F6F__INCLUDED_)
