#include "LReductor.h"


LReductor::LReductor()
{
	L_sentence = "T";
	branchHeight = 5;
	leafHeight = 20;
	branchNumber = 2;
	randomSeed = 10;
	srand(randomSeed);
}

LReductor::LReductor(int bh, int lh, int bn, int seed, float p_furcate, float p_leaf)
{
	L_sentence = "T";
	branchHeight = bh;
	leafHeight = lh;
	branchNumber = bn;
	randomSeed = seed;
	p1 = p_furcate;
	p2 = p_leaf;
	srand(randomSeed);
}

LReductor::~LReductor()
{
}

int posibility(float p) {
	int random = rand();
	return random <= (int)(p * 32767);
}

void LReductor::reduction() {
	int index = 0;
	L_sentence = reduction(0, 0, index);
}

std::string LReductor::getLSentence() {
	return L_sentence;
}

void LReductor::setLSentence(std::string sentence) {
	L_sentence = sentence;
}

std::string LReductor::reduction(int branchCounter, int leafCounter, int& index) {
	int branchHeightCounter = branchCounter;
	int leafHeightCounter = leafCounter;
	std::string res = "";

	for (; index < L_sentence.length(); index++)
	{
		char word = L_sentence[index];
		switch (word)
		{
		case 'A':	res += "A";	branchHeightCounter += 1; leafHeightCounter += 1;	 break;
		case 'L':	res += "L";	break;
		case 'T':
			if (branchHeightCounter >= branchHeight && posibility(p1)) {
				for (int i = 0; i < branchNumber; i++)
				{
					res += "[T]";
				}
				//res += "T";
				branchHeightCounter = 0;
			}
			else if (leafHeightCounter >= leafHeight && posibility(p2)) {
				res += "ALT";
			}
			else {
				res += "AT";
			}
			break;
		case '[':
			index += 1;
			res += "[";
			res += reduction(0, leafHeightCounter, index);
			break;
		case ']':
			//index+=1;
			res += "]";
			return res;
		default:
			break;
		}
	}
	return res;
}