#include "ThreadSet.h"

namespace cosi {

std::string ThreadSet::sQuantityName="ThreadSet";
std::string ThreadSet::sQuantityType="Set of threads";

ThreadSet::ThreadSet() {
}

ThreadSet::~ThreadSet() {
}

ThreadSet ThreadSet::Get() {
	return (*this);
}

void ThreadSet::Set(ThreadSet pSet) {
	(*this) = pSet ;
	IsBot(false) ;
}

std::set<Thread> ThreadSet::GetValue() {
	return mSet ;
}

void ThreadSet::SetValue(std::set<Thread> pSet) {
	mSet = pSet ;
	IsBot(false) ;
}

void ThreadSet::AddThread(Thread pT) {
	mSet.insert(pT) ;
	IsBot(false) ;
}

int ThreadSet::Size() {
	return mSet.size( ) ;
}

Thread ThreadSet::GetLongestMessage() {

	if ( IsBot() ) {
		Thread T ;
		T.SetMessageLength(0) ;
		return T ;
	}

	iterator It , Longest ;
	int MaxLength = 0 ;
	for( It = ThreadSetBegin() ; It != ThreadSetEnd(); It++) {
		if ( It->GetMessageLength() >= MaxLength ) {
			Longest = It ;
			MaxLength = It->GetMessageLength() ;
		}
	}
	return (*Longest) ;
}

ThreadSet::iterator ThreadSet::ThreadSetBegin()  {
	return mSet.begin() ;
}

ThreadSet::iterator ThreadSet::ThreadSetEnd() {
	return mSet.end() ;
}

bool ThreadSet::operator<(ThreadSet pSet) {
	//check containement
	if ( std::includes(pSet.ThreadSetBegin(),pSet.ThreadSetEnd(),ThreadSetBegin(),ThreadSetEnd()) ) {
		return true ;
	} else {
		return false;
	}
}

bool ThreadSet::operator==(ThreadSet pSet) {
	return (mSet == pSet.GetValue());
}

ThreadSet ThreadSet::operator+(ThreadSet pSet) {
	//take union
	std::set<Thread> RetVal ;
	std::set_union(pSet.ThreadSetBegin(),pSet.ThreadSetEnd(),ThreadSetBegin(),ThreadSetEnd(),std::insert_iterator<std::set<Thread> >(RetVal,RetVal.begin())) ;
	ThreadSet T ;
	T.SetValue(RetVal);
	return T ;
}

std::ostream& operator <<(std::ostream &pOs , const ThreadSet &pT) {
	std::set<Thread>::iterator It ;
	for(It=pT.mSet.begin() ; It!=pT.mSet.end() ; It++) {
		pOs << (*It) ;
	}
	return pOs ;
}

}
