#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include "AST/AllTypesOfASTNodes.h"

using namespace std;

SenchaObject sleep(vector<ASTExpression *> arguments)
{
	SenchaObject argument = arguments[0]->evaluate();
	if(argument.type == SenchaObject::integer_number && argument.integer >= 0)
	{
		cout << "valid argument for sleep: " << argument.integer << endl;
		usleep(1000 * argument.integer);
		return SenchaObject();
	}
	else
	{
		cout << "invalid argument for sleep";
		argument.type = SenchaObject::invalid;
		return argument;
	}

}

SenchaObject print(vector<ASTExpression *> arguments)
{
		for (auto argument: arguments)
		{
			auto value = argument->evaluate();

			std::cout  << value.str();

		}
		return SenchaObject();
}

SenchaObject println(vector<ASTExpression *> arguments)
{
		for (auto argument: arguments)
		{
			auto value = argument->evaluate();

			std::cout  << value.str();

		}
		std::cout << std::endl;
		return SenchaObject();
}

SenchaObject s_sin(vector<ASTExpression *> arguments)
{
	SenchaObject result;
	SenchaObject sin_argument = arguments[0]->evaluate();
	if(sin_argument.type == SenchaObject::integer_number)
	{
		result = SenchaObject(sin(sin_argument.integer));
	}
	else if(sin_argument.type == SenchaObject::float_number)
	{
		result = SenchaObject(sin(sin_argument.number));
	}
	else
	{
		result.type = SenchaObject::invalid;
	}

	return result;
}

SenchaObject s_cos(vector<ASTExpression *> arguments)
{
	SenchaObject result;
	SenchaObject cos_argument = arguments[0]->evaluate();
	if(cos_argument.type == SenchaObject::integer_number)
	{
		result = SenchaObject(cos(cos_argument.integer));
	}
	else if(cos_argument.type == SenchaObject::float_number)
	{
		result = SenchaObject(cos(cos_argument.number));
	}
	else
	{
		result.type = SenchaObject::invalid;
	}

	return result;
}

SenchaObject s_tan(vector<ASTExpression *> arguments)
{
	SenchaObject result;
	SenchaObject tan_argument = arguments[0]->evaluate();
	if(tan_argument.type == SenchaObject::integer_number)
	{
		result = SenchaObject(tan(tan_argument.integer));
	}
	else if(tan_argument.type == SenchaObject::float_number)
	{
		result = SenchaObject(tan(tan_argument.number));
	}
	else
	{
		result.type = SenchaObject::invalid;
	}

	return result;
}

SenchaObject len(vector<ASTExpression *> arguments)
{
	SenchaObject result;
	SenchaObject argument = arguments[0]->evaluate();
	if(argument.type == SenchaObject::string_literal)
	{
		result = SenchaObject(int(argument.text.size()));
	}
	return result;
}
