#pragma once
#include "commonobject.h"
#include <vector>
using namespace std;


class Anchor
{
public:
	Anchor(){};
	Anchor(CPoint point, CPoint *pointPrev = NULL);
	~Anchor() {};

	void MoveAnchor(CPoint &point, int nDirection); // nDirection: horizontal 0; vertical 1;
	bool operator==(const Anchor&) const;
	bool operator!=(const Anchor&) const;
	const Anchor& operator=(const Anchor&);

private:

public:
	CPoint m_PointMain;
	CPoint *m_pPointPrev;
	CPoint *m_pPointNext;
private:

};
typedef CList<Anchor> AnchorList;

typedef vector<Anchor> AnchorVector;
typedef vector<Anchor>::iterator AnchorIter;
typedef vector<Anchor>::const_iterator AnchorCit;


class CWire : public CCommonObject
{
public:
	CWire(int nIn, int nOut, AnchorList &anchors, CCommonObject* from, int nFromID, CCommonObject *to, int nToID, CString strName);
	~CWire(void);

public:
	void OutScreen(Graphics &g);
	void ResetGatePath();
	void TransformCObject(Matrix* pMat);
	bool HitTest(CPoint point);
	CCommonObject* ConnectHitTest(CPoint point, int &nNumber);

	void ExpandInsCount();
	void ExpandOutsCount();

	void DrawAnchors(CDC *pDC);
	void SetMoveParts(bool bOn) {m_bMoveParts = bOn;}
	void PrepareForEdit(CPoint &point);
	void PrepareForEditEdge(int nWhich);
	void MoveAnchorPair(CPoint &point);
	void MoveAnchorEdge(CPoint &point);
	void DrawMovedLine(CDC *pDC); // NOT XOR pDC !!!!
	void DrawMovedEdge(CDC *pDC);
	Anchor** GetAnchorPair() {return m_aEditAnchors;}
	void FinishWireEdit(bool bCommit = true);
	void FinishWireEdgeEdit(bool bCommit = true);
	void OutXml(stringstream &ss);
	CCommonObject* Clone(CPoint TopLeftOrigin, CString strNewName, int nRotation); // returns the same object; never cloning wire
	void SetConnection(bool bIn, int nPos, CCommonObject* pConnectTo);
	void Disconnect();
	void DisconnectCObject(CCommonObject *pObject);

	int EditEdgePoint(CPoint point); // 1 - edit first (head) anchor; 2 - edit last (tail) anchor; -1 - no edit
private:
	void UpdateGraphicsPath(); // according to points!
	CPoint RestrictPoint(CPoint point, CPoint pntBase);

public:
	Point *m_Points;
	AnchorList m_Anchors;		// list of wires anchors

private:
	Brush *m_pBrush;
	
	Anchor **m_aEditAnchors;	// array of two anchors to move when editing
	POSITION *m_posEditAnchors; // positions of two moved anchors in m_Anchors
	int m_nMovementDir;			// default -1; horizontal 0; vertical 1;
	bool m_bMoveParts;			// option to move separate lines or whole wire
};