/*
--------------------------------------------------------------------------------
This source file is part of SkyX.

Copyright (C) 2011 I-maginer <contact@i-maginer.com>

Adapted from Xavier Verguín González initial work:
Copyright (C) 2009 Xavier Verguín González <xavierverguin@hotmail.com>
                                           <xavyiy@gmail.com>

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser 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 Lesser General Public License for more details.

You should have received a copy of the GNU Lesser 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, USA, or go to
http://www.gnu.org/copyleft/lesser.txt.
--------------------------------------------------------------------------------
*/

#include "SkyxAstronomicalModelBasic.h"
#include "Skyx.h"

namespace SkyX
{

	AstronomicalModelBasic::AstronomicalModelBasic()
	{
    Time = Ogre::Vector3(14.0f, 7.50f, 20.50f);
		EastPosition = Ogre::Vector2(0, 1);
	}

	AstronomicalModelBasic::~AstronomicalModelBasic()
	{
	}

  const Ogre::Vector3 AstronomicalModelBasic::getSunDirection()
  {
    // 24h day: 
    // 0______A(Sunrise)_______B(Sunset)______24

    float y;
    float X = Time.x;
    float A = Time.y;
    float B = Time.z;
    float AB  = A+24-B;
    float AB_ = B-A;
    float XB  = X+24-B;

    if (X<A || X>B)
    {
      if (X<A)
        y = -XB / AB;
      else
        y = -(X-B) / AB;
            
      if (y > -0.5f)
        y *= 2;
      else
        y = -(1 + y)*2;
    }
    else
    {
      y = (X-A)/(B-A);
      if (y < 0.5f)
        y *= 2;
      else
        y = (1 - y)*2;
    }

    // Orient East
    Ogre::Vector2 East = EastPosition.normalisedCopy();
    if (X > A && X < B)
    {
      if (X > (A + AB_/2))
        East = -East;
    }
    else
    {
      if (X<A)
      {
        if (XB < (24-AB_)/2)
	        East = -East;
      }
      else
      {
        if ((X-B) < (24-AB_)/2)
	        East = -East;
      }
    }

    float ydeg = (Ogre::Math::PI/2)*y;
    float sn = Ogre::Math::Sin(ydeg);
    float cs = Ogre::Math::Cos(ydeg);

    Ogre::Vector3 SPos = Ogre::Vector3(East.x*cs, sn, East.y*cs);
    return -SPos;
  }

  const Ogre::Vector3 AstronomicalModelBasic::getMoonDirection()
  {
    return Ogre::Vector3::ZERO;
  }

  const Ogre::Real AstronomicalModelBasic::getMoonPhase()
  {
    // Full moon
    return 0.0f;
  }

  void AstronomicalModelBasic::update(const Ogre::Real& advanceTime)
  {
    Time.x += advanceTime;
		if (Time.x > 24)
			Time.x -= 24;
		else if (Time.x < 0)
			Time.x += 24;
  }

}
