/*
    Copyright (C) 2012 Bartosz Broda, Jacek Bochenek, Łukasz Biłeńkij, 
    Damian Jaworski, Wojciech Mazur, Dominik Piasecki, Maciej Piasecki, 
    Radosław Ramocki
    
    This program is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your option)
any later version.

    This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. 

    See the LICENSE and COPYING files for more details.
*/

#include "lex_headers.h" 
#include "FixME.h"

namespace LexicalUnits
{
	FixME::FixME(int baseId,
		const std::vector<boost::shared_ptr<Word> > &constituents,
			JOSKIPI::Operators::CCondition* head_search,
			JOSKIPI::Operators::CCondition* operator_constraint,
			Corpus::TFlexClass fc  )
			: LexicalUnit(constituents,baseId,fc ),
		  m_pHeadSearch(head_search),
		  m_pCondition(operator_constraint)

	{
	}
	 FixME::~FixME()
     {
          delete m_pHeadSearch;
          delete m_pCondition;
     }

	std::string FixME::ToXmlString()
	{
		throw std::string("MultiwordExpression::ToXmlString not implemented");
		//TODO FixME::ToXmlString()
		return "";
	}

	bool FixME::CheckCurrentPosition ( Corpus::CSentence &sentence, size_t &head_index,
											std::set<size_t> &constituents )
	{
		head_index = 0;
		constituents.clear();

		if( (sentence.Size() - sentence.GetPosition() ) < (int)m_vConstituents.size() )
			return false;

		int currPos = sentence.GetPosition();			

		for(size_t i = 0; i < m_vConstituents.size(); i++)
		{
			bool found = false;
			Corpus::CToken * pToken = sentence.GetToken((int) i + currPos);

			for(size_t lex_no = 0; lex_no < pToken->GetLexCount() && !found; lex_no++)
			{
				const Corpus::CLexem *pDisamb = pToken->GetLexem(lex_no);
				if(! pDisamb->GetDisamb() )
					continue;				
				if( *m_vConstituents[i] == *pDisamb)
				{
					found = true;
					constituents.insert((size_t) currPos + i);
				}
			}
			if(!found)
				return false;
		}

		if( !m_pCondition->Evaluate(sentence) )
			return false;

		bool headFound = false;
		for(size_t i = 0; i < m_vConstituents.size() && !headFound; i++)
		{
			int actualPos = currPos+(int) i;
			sentence.Goto( actualPos );
			if( m_pHeadSearch->Evaluate(sentence) )
			{
				sentence.Goto(currPos);
				constituents.erase((size_t) actualPos);
				head_index = actualPos;					
				headFound = true;
			}
		}

		if( !headFound )
		{
			constituents.clear();
			return false;
		}

		return true;
	}

}//namespace LexicalUnits


