/*  DS
 *  Copyright (C) Anders Asplund and Joakim Kolsjö 2005
 *	This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 
 */

#include "hspikeball.h"
#include "../ds/handler.h"
#include "utils/quad.h"

using namespace std;

namespace DS
{	
	HSpikeBall::HSpikeBall()
	{
		// Set defaults
		m_pHandleAttr->Insert("sticks", "32");
		m_pHandleAttr->Insert("states", "1");
		m_pDefaultNodeAttr->Insert("time", "required");
		m_pDefaultNodeAttr->Insert("r", "1");
		m_pDefaultNodeAttr->Insert("g", "1");
		m_pDefaultNodeAttr->Insert("b", "1");
		m_pDefaultNodeAttr->Insert("x", "0");
		m_pDefaultNodeAttr->Insert("y", "0");
		m_pDefaultNodeAttr->Insert("z", "0");
		m_pDefaultNodeAttr->Insert("xs", "1");
		m_pDefaultNodeAttr->Insert("ys", "1");
		m_pDefaultNodeAttr->Insert("zs", "1");
		m_pDefaultNodeAttr->Insert("xr", "0");
		m_pDefaultNodeAttr->Insert("yr", "0");
		m_pDefaultNodeAttr->Insert("zr", "0");
		m_pDefaultNodeAttr->Insert("light", "1");
		m_pDefaultNodeAttr->Insert("state", "0");
	}

	void HSpikeBall::Init()
	{
		for(int i = 0; i < (int)m_NodeAttrList.size(); i++)
		{
			Map* pNodeAttr = m_NodeAttrList[i];
			Attrib temp;
			temp.time = atol(pNodeAttr->Get("time"));
			temp.r = atof(pNodeAttr->Get("r"));
			temp.g = atof(pNodeAttr->Get("g"));
			temp.b = atof(pNodeAttr->Get("b"));
			temp.x = atof(pNodeAttr->Get("x"));
			temp.y = atof(pNodeAttr->Get("y"));
			temp.z = atof(pNodeAttr->Get("z"));
			temp.xs = atof(pNodeAttr->Get("xs"));
			temp.ys = atof(pNodeAttr->Get("ys"));
			temp.zs = atof(pNodeAttr->Get("zs"));
			temp.xr = atof(pNodeAttr->Get("xr"));
			temp.yr = atof(pNodeAttr->Get("yr"));
			temp.zr = atof(pNodeAttr->Get("zr"));
			temp.light = atof(pNodeAttr->Get("light"));
			temp.state = atof(pNodeAttr->Get("state"));
			m_attr.push_back(temp);
		}		
		
		m_sticks = atoi(m_pHandleAttr->Get("sticks"));
		m_states = atoi(m_pHandleAttr->Get("states"));
		
		SetupRange();
	}
	
	void HSpikeBall::Reset()
	{
		m_current = 1;
	}
	
	void HSpikeBall::CacheData()
	{
		for(int i = 0; i < m_states; i++)
		{
			std::vector<Rotation> ctemp;
			for(int j = 0; j < m_sticks; j++)
			{
				Rotation r;
				r.x = (((float)rand()) / RAND_MAX)*360;
				r.y = (((float)rand()) / RAND_MAX)*360;
				r.z = (((float)rand()) / RAND_MAX)*360;
				
				ctemp.push_back(r);
				/*		std::vector<cord> m_current_state;
		std::vector<std::vector<cord> > m_all_states;*/
			//m_x.push_back();
			//m_y.push_back((((float)rand()) / RAND_MAX)*360);
			//m_z.push_back((((float)rand()) / RAND_MAX)*360);
			
			}
			m_all_states.push_back(ctemp);
		}
		
		m_pModelLoader->Load("models/double_cone.ms3d");
	}
	
	void HSpikeBall::LoadData()
	{
		m_pModel = m_pModelLoader->GetModel("models/double_cone.ms3d");
	}
	
	void HSpikeBall::Draw(long time)
	{		
		// Find current state
		if(m_attr.size() > 2 && m_current < m_attr.size()-1)
			if(time > m_attr[m_current].time)
				m_current++;
			
		float s = (float)(time - m_attr[m_current-1].time)/(m_attr[m_current].time - m_attr[m_current-1].time);			
		m_state.x =  m_attr[m_current-1].x + (m_attr[m_current].x-m_attr[m_current-1].x)*s;
		m_state.y =  m_attr[m_current-1].y + (m_attr[m_current].y-m_attr[m_current-1].y)*s;
		m_state.z =  m_attr[m_current-1].z + (m_attr[m_current].z-m_attr[m_current-1].z)*s;
		m_state.r =  m_attr[m_current-1].r + (m_attr[m_current].r-m_attr[m_current-1].r)*s;
		m_state.g =  m_attr[m_current-1].g + (m_attr[m_current].g-m_attr[m_current-1].g)*s;
		m_state.b =  m_attr[m_current-1].b + (m_attr[m_current].b-m_attr[m_current-1].b)*s;
		m_state.xs =  m_attr[m_current-1].xs + (m_attr[m_current].xs-m_attr[m_current-1].xs)*s;
		m_state.ys =  m_attr[m_current-1].ys + (m_attr[m_current].ys-m_attr[m_current-1].ys)*s;
		m_state.zs =  m_attr[m_current-1].zs + (m_attr[m_current].zs-m_attr[m_current-1].zs)*s;
		m_state.xr =  m_attr[m_current-1].xr + (m_attr[m_current].xr-m_attr[m_current-1].xr)*s;
		m_state.yr =  m_attr[m_current-1].yr + (m_attr[m_current].yr-m_attr[m_current-1].yr)*s;
		m_state.zr =  m_attr[m_current-1].zr + (m_attr[m_current].zr-m_attr[m_current-1].zr)*s;
		m_state.light =  m_attr[m_current-1].light + (m_attr[m_current].light - m_attr[m_current-1].light)*s;
	
		// Calculate current cordinates
		int prev = (int)m_attr[m_current-1].state;
		int next = (int)m_attr[m_current].state;
	
		//if(prev != next)
		{
			m_current_state.clear();
			
			for(int i = 0; i < m_sticks; i++)
			{
				Rotation r;
				r.x	= m_all_states[prev][i].x + (m_all_states[next][i].x - m_all_states[prev][i].x)*s;
				r.y	= m_all_states[prev][i].y + (m_all_states[next][i].y - m_all_states[prev][i].y)*s;
				r.z	= m_all_states[prev][i].z + (m_all_states[next][i].z - m_all_states[prev][i].z)*s;
				m_current_state.push_back(r);
			}
			
		}
			
		glPushMatrix();
			glTranslatef(m_state.x, m_state.y, m_state.z);
			glRotatef(m_state.xr, 1, 0, 0);
			glRotatef(m_state.yr, 0, 1, 0);
			glRotatef(m_state.zr, 0, 0, 1);
			glScalef(m_state.xs, m_state.ys, m_state.zs);
			glColor3f(m_state.light, m_state.light*m_state.g, m_state.light*m_state.b);

			// Draw spikeball
			glEnable(GL_LIGHT1);
			glEnable(GL_LIGHTING);
			//glEnable(GL_TEXTURE_2D);
			GLfloat diffuse[] = { m_state.light*0.3*m_state.r, m_state.light*0.3*m_state.g, m_state.light*0.3*m_state.b, 0 };
			glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
			GLfloat ambient[] = { m_state.light*0.3*m_state.r, m_state.light*0.3*m_state.g, m_state.light*0.3*m_state.b, 1 };
			glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);

			//glDisable(GL_TEXTURE_2D);
			glPushMatrix();
			for(int i = 0; i < m_sticks; i++)
			{
				glPushMatrix();
					glRotatef(m_current_state[i].x, 1, 0, 0);
					glRotatef(m_current_state[i].y, 0, 1, 0);
					glRotatef(m_current_state[i].z, 0, 0, 1);
					glScalef(0.05, 0.05, 0.05);
					m_pModel->Draw();
				glPopMatrix();
			}
			glPopMatrix();
			glDisable(GL_LIGHT1);
			glDisable(GL_LIGHTING);	
		glPopMatrix();
	}
}
