// INKSDoc.h : interface of the CINKSDoc class
//

#pragma once

#include <map>
#include "MouseSnap.h"
class CMouseTracker;
class CSpacialHashing;
class CZoomView;
class CMouseTracker;
class CElementContainer;
class CSideBar;
class CCommonObject;
class CINKSView;
class CTreeItem;
class CBlock;

class CINKSDoc : public CDocument
{
public:
protected:
	CINKSDoc();
	DECLARE_DYNCREATE(CINKSDoc)
	
public:
	virtual ~CINKSDoc();
	void InitNewDocument();
	
	BOOL OnOpenFile(CString strFile, int nType, bool bInProject = false);
	bool IsProjectDoc() {return m_nDocumentType == 0;}

	bool SOListContains(CCommonObject *pObj);
	bool RemoveFromCOList(CCommonObject *pObj);

	bool AddNewCommonObject(CPoint point);
	bool AddNewCommonObjectWire(CPoint point);

	void DeleteSelection();

	void UpdateObjecInfo();	// when selected some object(s)

	void TryMovedConnection();

private:
	CCommonObject* CreateNewCommonObject(CPoint point, CString &strOrigin, int nSource);
	CINKSView* GetAssociatedView();
	void ConnectElements(CList<CString*, CString*> *inList, CList<CString*, CString*> *outList);
	CCommonObject* GetCommonObject(CString strName);
	CINKSDoc* GetParentDocument() {return m_pParentDoc;}

public:
	CElementContainer *m_pElementCont;

	CMouseTracker *m_MouseTracker;

	GraphicsPath gPath;
	GraphicsPath gPathText;

	CList<CCommonObject*, CCommonObject*> m_COList;
	CList<CCommonObject*, CCommonObject*> m_SOList;
	//CMap<CString, LPCTSTR, CCommonObject*, CCommonObject*> m_COMap;
	//CMap<CString, LPCTSTR, CCommonObject*, CCommonObject*> m_SOMap;

	CSideBar *m_pCreateOptionsDialog;
	CMouseSnap m_SnapMouse;
	bool m_bSelectMultiple;

	CSpacialHashing *m_pSpaceHash;
	CMouseTracker *m_pMouseTracker;
	int m_nDocumentType; // 0-project document; 1-scheme document; 2-block document;

	struct ProjectDoc{
		CString m_strProjectName;
		CString m_strProjectPath;
		int m_nSchemeCount;
		void Reset();
	}m_ProjectDoc;

	struct SchemeDoc{
		CString m_strSchemeName; // display name
		int m_nGatesCount;
		int m_nWiresCount;
		int m_nBlocksCount;
		int m_nTagsCount;
		int m_nInvCount;
		void Reset();
	}m_SchemeDoc;

	struct BlockDoc{
		CString m_strBlockName; // display name
		CString m_strBlockOrigin; // block origin
		int m_nGatesCount;
		int m_nWiresCount;
		int m_nBlocksCount;
		int m_nTagsCount;
		int m_nInvCount;
		bool m_bAppBlock;
		CINKSDoc *m_pParentDoc;
		void Reset();
	}m_BlockDoc;

	int GetNextGateID();
	int GetNextBlockID();
	int GetNextWireID();
	int GetNextTagID();
	int GetNextInvID();
	void SetInvCount(int nCount);
	void SetTagsCount(int nCount);
	void SetWiresCount(int nCount);
	void SetBlocksCount(int nCount);
	void SetGatesCount(int nCount);


	CTreeItem *m_pTreeItem; // associated tree item (NULL if any)
	CINKSDoc *m_pParentDoc; // for hierarchy monitoring
	bool m_bNeedReload;

private:
	
	

public:
	afx_msg void OnSelectSelect();
	afx_msg void OnSelectDrawGate();
	afx_msg void OnSelectDrawWire();
	afx_msg void OnSelectDrawBlock();
	afx_msg void OnSelectDrawTag();
	afx_msg void OnSelectDrawInv();

	afx_msg void OnUpdateSelectSelect(CCmdUI *pCmdUI);
	afx_msg void OnUpdateSelectDrawGate(CCmdUI *pCmdUI);
	afx_msg void OnUpdateSelectDrawBlock(CCmdUI *pCmdUI);
	afx_msg void OnUpdateSelectDrawWire(CCmdUI *pCmdUI);
	afx_msg void OnUpdateSelectDrawTag(CCmdUI *pCmdUI);
	afx_msg void OnUpdateSelectDrawInv(CCmdUI *pCmdUI);

	BOOL OnCloseProject();

// overrides:
public:
	virtual BOOL OnNewDocument();
	virtual void Serialize(CArchive& ar);
	BOOL OnSaveDocument(LPCTSTR lpszPathName);
	virtual BOOL SaveModified();
	virtual BOOL OnOpenProject(LPCTSTR lpszPathName);
	virtual BOOL OnNewProject(LPCTSTR lpszPathName, LPCTSTR lpszProjectName);
	void OnCloseDocument();
	void CloseDocument(bool bClose);

#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

protected:

// Generated message map functions
protected:
	DECLARE_MESSAGE_MAP()
public:
	afx_msg void OnSelectioncontextmenuDeleteitem();
	afx_msg void OnBlockmenuDeleteitem();
	afx_msg void OnBlockmenuPushintotheblock();
	afx_msg void OnBlockmenuPushoutoftheblock();
};


