#ifndef __TEXTMERGER__
#define __TEXTMERGER__

#include "ChipmonkTypes.h"
#include "TextContainer.h"

#define MAX_LINE_LENGTH  1024

typedef struct{
	SHORT nVal, nIndx;
} MatchTableElement;

struct MatchTable{
	int m_nW, m_nH, m_nSize;
	MatchTableElement *m_pData;
	MatchTable(int nW = 100, int nH = 100) {
		m_pData = NULL;
		m_nSize = 0;
		SetSize(nW, nH);
	}
	~MatchTable(){ if(m_pData) delete [] m_pData;}
	void SetSize(int nW, int nH){
		m_nW = nW;
		m_nH = nH;
		if(m_nSize < m_nW * m_nH){
			m_nSize = m_nW * m_nH;
			if(m_pData) delete [] m_pData;
			m_pData = new MatchTableElement[m_nSize];
		}
	}
};

class Dictionary;

class CTextMerger{
	int m_nMaxLines;
	WCHAR *m_pLine0, *m_pLine1, *m_pM0, *m_pM1, *m_pLine;
	PWCHAR *m_pLines0, *m_pLines1, *m_pRes0, *m_pRes1;
	TextContainer m_text;
	void SetSize(int nMaxLines);
	
	MatchTable m_MatchTable;
	const WCHAR *GetMergedText(PWCHAR *pLines0, PWCHAR *pLines1, Dictionary *pDict, int nInsertion = -10, int nDeletion  = -10);
	const WCHAR *GetMergedLines(const WCHAR *pLn0, const WCHAR *pLn1, Dictionary *pDict = NULL, int nInsertion = -10, int nDeletion  = -10);
	int GetMergeScore(WCHAR *pText0, WCHAR *pText1);

public:
	static PWCHAR EMPTYPCHAR;
	WCHAR *MarkCorrectMatches(const WCHAR *pLine, const WCHAR *pCorrectLine, WCHAR *pRes = NULL);
	static PWCHAR *GetLines(WCHAR *pText);
	const PWCHAR *GetLines(WCHAR *pText, PWCHAR *pLines);
	int MatchLines(const WCHAR *pLine0, const WCHAR *pLine1, WCHAR *pRes0, WCHAR *pRes1, int nInsertion = -10, int nDeletion  = -10);
	const WCHAR *Merge(WCHAR *pText0, WCHAR *pText1, Dictionary *pDict = NULL, int nCharInsertion = -10, int nCharDeletion  = -10);
	const WCHAR *MergeTheBestPair(WCHAR **ppTexts,  int nTexts, int nThreshold, Dictionary *pDict, int &mi, int &mj);
	WCHAR *OptimalMerge(WCHAR **ppTexts,  int nTexts, int nThreshold, Dictionary *pDict, BOOL *pNotOverlap);

	CTextMerger(int nMaxLines = 56);
	~CTextMerger();
};

#endif
