/// Copyright (c) 2009 Ruben Penalva. All rights reserved.
/// Redistribution and use in source and binary forms, with or without modification, 
/// are permitted provided that the following conditions are met:
///    1. Redistributions of source code must retain the above copyright notice, this 
///    list of conditions and the following disclaimer.
///    2. Redistributions in binary form must reproduce the above copyright notice,
///    this list of conditions and the following disclaimer in the documentation and/or
///    other materials provided with the distribution.
///    3. The name of the author may not be used to endorse or promote products derived
///    from this software without specific prior written permission.
///
/// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
/// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
/// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 
/// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
/// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
/// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
/// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 
/// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

/// @file Logic.cpp
/// This source file is part of Gpu Quadratic Curve Render Test.
/// The file holds the Logic class implementation.
/// @author Ruben Penalva (ruben.penalva@rpenalva.com)

#include "Logic.h"

#include <assert.h>

using namespace GpuQuadraticCurveRender;

Logic::Logic(void)	:	m_enabledPoints(true), m_enabledLines(true), m_enabledTriangles(true)
{	
	Point quadraticControlPointA(-10.0f, -6.0f);
	Point quadraticControlPointB(0.0f, 4.0f);
	Point quadraticControlPointC(10.0f, 8.0f);

	m_quadraticControlPoints.push_back(quadraticControlPointA);
	m_quadraticControlPoints.push_back(quadraticControlPointB);
	m_quadraticControlPoints.push_back(quadraticControlPointC);

	m_quadraticLines.push_back(Line(quadraticControlPointA, quadraticControlPointB));
	m_quadraticLines.push_back(Line(quadraticControlPointB, quadraticControlPointC));

	m_quadraticIndices.push_back(0);
	m_quadraticIndices.push_back(1);
	m_quadraticIndices.push_back(2);
}

Logic::~Logic(void)
{
}

void Logic::EnablePoints(void)
{	
	m_enabledPoints = !m_enabledPoints;
}

void Logic::EnableLines(void)
{	
	m_enabledLines = !m_enabledLines;
}

void Logic::EnableTriangles(void)
{
	m_enabledTriangles = !m_enabledTriangles;
}
		
bool Logic::ArePointsEnabled(void)
{
	return m_enabledPoints;
}

bool Logic::AreLinesEnabled(void)
{
	return m_enabledLines;
}
	
bool Logic::AreTrianglesEnabled(void)
{
	return m_enabledTriangles;
}

const std::vector<Point>& Logic::GetQuadraticControlPoints(void) const
{
	return m_quadraticControlPoints;
}

const std::vector<Line>& Logic::GetQuadraticLines(void) const
{
	return m_quadraticLines;
}

const std::vector<int>& Logic::GetQuadraticIndices(void) const
{
	return m_quadraticIndices;
}
		
void Logic::SetQuadraticControlPoint(int pointIndex, const Point& point)
{
	assert(pointIndex < m_quadraticControlPoints.size());
	
	if (point.m_x > 10.0f || point.m_x < -10.0f || 
		point.m_y > 8.0f || point.m_y < -8.0f)
	{
		return;
	}

	m_quadraticControlPoints[pointIndex] = point;
	
	if (pointIndex == 0)
	{
		m_quadraticLines[0].m_start = point;
	}
	else if (pointIndex == 1)
	{
		m_quadraticLines[0].m_end = point;
		m_quadraticLines[1].m_start = point;
	}
	else if (pointIndex == 2)
	{
		m_quadraticLines[1].m_end = point;
	}
}

bool Logic::AreQuadraticControlPointsIntersected(const Point& point, int& controlPointIndex, float pointSize)
{
	return AreControlPointsIntersected(m_quadraticControlPoints, point, controlPointIndex, pointSize);
}

bool Logic::AreControlPointsIntersected(const std::vector<Point>& points, const Point& point, int& controlPointIndex, float pointSize)
{	
	float offset = pointSize / 50.0f;

	int pointsCount = points.size();

	for (int i = 0; i < pointsCount; ++i)
	{
		const Point& controlPoint = points[i];
		float maxX = controlPoint.m_x + offset;
		float minX = controlPoint.m_x - offset;
		float maxY = controlPoint.m_y + offset;
		float minY = controlPoint.m_y - offset;
		if (point.m_x > minX && point.m_x < maxX && 
			point.m_y > minY && point.m_y < maxY)
		{
			controlPointIndex = i;
			return true;
		}
	}

	return false;
}