#include <iostream>
#include <cmath>
#include "Box.hpp"

namespace nbody {
	using namespace std;

	Box::Box() {
	}

	Box::~Box() {
	}

	//min/max getter/setters
	double Box::getMin(int index) {
		return this->min[index];
	}

	double Box::getMax(int index) {
		return this->max[index];
	}

	void Box::setMin(int index, double value) {
		this->min[index] = value;
	}

	void Box::setMax(int index, double value) {
		this->max[index] = value;
	}

	void Box::extendToCube() {
		int longestSide = -1;
		double sidelength = 0.0;

		for (int i = 0; i < 3; i++) {
			if (this->max[i] - this->min[i] >= sidelength) {
				longestSide = i;
				sidelength = this->max[i] - this->min[i];
			}
		}
		if (longestSide == -1) {
			return;
		}
		for (int i = 0; i < 3; i++) {
			if (i != longestSide) {
				double extend = (sidelength - (this->max[i] - this->min[i])) / 2.0;

				this->min[i] -= extend;
				this->max[i] += extend;
			}
		}
	}

	vector<Body> Box::copyBodies(vector<Body> bodies) {
		vector<Body> result;

		for (vector<Body>::iterator it = bodies.begin(); it != bodies.end(); it++) {
			if (it->position[0] >= this->min[0] && it->position[0] <= this->max[0] &&
					it->position[1] >= this->min[1] && it->position[1] <= this->max[1] &&
					it->position[2] >= this->min[2] && it->position[2] <= this->max[2]) {
				result.push_back(*it);
			}
		}
		return result;
	}

	vector<Body> Box::extractBodies(vector<Body>& bodies) {
		vector<Body> result;
		vector<Body>::iterator it = bodies.begin();

		while (it != bodies.end()) {
			if (it->position[0] >= this->min[0] && it->position[0] <= this->max[0] &&
					it->position[1] >= this->min[1] && it->position[1] <= this->max[1] &&
					it->position[2] >= this->min[2] && it->position[2] <= this->max[2]) {
				result.push_back(*it);
				it = bodies.erase(it);
			} else {
				it++;
			}
		}
		return result;
	}

	bool Box::isContained(Body body) {
		for (int i = 0; i < 3; i++) {
			if (body.position[i] < this->min[i] || body.position[i] > this->max[i]) {
				return false;
			}
		}
		return true;
	}

	//TODO: check semantics
	bool Box::isContained(Box box) {
		for (int i = 0; i < 3; i++) {
			if (box.min[i] < this->min[i] || box.max[i] > this->max[i]) {
				return false;
			}
		}
		return true;
	}

	double Box::volume() {
		double result = 1.0;

		for (int i = 0; i < 3; i++) {
			result *= this->max[i] - this->min[i];
		}
		return result;
	}

	bool Box::overlapsSphere(double* sphereCenter, double sphereRadius) {
		double dmin = 0.0;

		for (int i = 0; i < 3; i++) {
			if (sphereCenter[i] < this->min[i]) {
				double dist = sphereCenter[i] - this->min[i];

				dmin += dist * dist;
			} else if (sphereCenter[i] > this->max[i]) {
				double dist = sphereCenter[i] - this->max[i];

				dmin += dist * dist;
			}
		}
		return dmin <= sphereRadius * sphereRadius;
	}

	double Box::maxSidelength() {
		double max = 0.0;

		for (int i = 0; i < 3; i++) {
			if (this->max[i] - this->min[i] > max) {
				max = this->max[i] - this->min[i];
			}
		}
		return max;
	}

	double Box::distanceToPosition(double* position) {
		int inside = 0;
		double nextPosition[3] = {position[0], position[1], position[2]};

		for (int i = 0; i < 3; i++) {
			if (nextPosition[i] < this->min[i]) {
				nextPosition[i] = this->min[i];
			} else if (nextPosition[i] > this->max[i]) {
				nextPosition[i] = this->max[i];
			} else {
				inside++;
			}
		}
		if (inside == 3) {
			return 0.0;
		} else {
			double dist = 0.0;

			for (int i = 0; i < 3; i++) {
				dist += (nextPosition[i] - position[i]) * (nextPosition[i] - position[i]);
			}
			return sqrt(dist);
		}

	}

	bool Box::isCorrect() {
		for (int i = 0; i < 3; i++) {
			if (this->max[i] < this->min[i]) {
				cout << "inverted bb" << endl;
				return false;
			}
		}
		return true;
	}

	void Box::print() {
		for (int i = 0; i < 3; i++) {
			cout << i << " min " << this->min[i] << " max " << this->max[i] << endl;
		}
	}
}
