#include "Player.h"
#include "Game.h"
#include "functions.h"
#include "DefenseWall.h"
#include "Fire.h"

#include <OgreRoot.h>
#include <OgreSceneManager.h>
#include <OgreEntity.h>
#include <OIS/OIS.h>

#include "OgreOverlay.h"
#include "OgreOverlayManager.h"
#include "OgreOverlayContainer.h"
#include "OgreTextAreaOverlayElement.h"

#include <Windows.h>

using namespace Engine;
using namespace Ogre;
using namespace OIS;

Player::Player()
{
	mEntGood = Game::mOgreSceneManager->createEntity("Good player", "GoodGuy.mesh");
	mEntBad = Game::mOgreSceneManager->createEntity("Bad player", "BadGuy.mesh");
	mEntCharge = Game::mOgreSceneManager->createEntity("Charging player", "Together.mesh");

	mNodeGood = Game::mOgreSceneManager->getRootSceneNode()->createChildSceneNode();
	mNodeGood->attachObject(mEntGood);
	mNodeBad = Game::mOgreSceneManager->getRootSceneNode()->createChildSceneNode();
	mNodeBad->attachObject(mEntBad);
	mNodeCharge = Game::mOgreSceneManager->getRootSceneNode()->createChildSceneNode();
	mNodeCharge->attachObject(mEntCharge);

	mNodeGood->setScale(2.0f,2.0f,2.0f);
	mNodeBad->setScale(2.0f,2.0f,2.0f);

	mPosition = Vector3(0,0,Rand(-0.2f,0.2f));
	mNodeCharge->setPosition(0,0,0.1f);
	mSpeed = Vector3::ZERO;
	mAccel = Vector3(0.05f,0.05f,0);

	mMaxSpeed = Vector3(3.0f,3.0f,1);
	mWidth = mHeight = 18;
	onGround = false;

	mNodeGood->setVisible(false);
	mNodeBad->setVisible(false);
	mPlayMode = 0;
	mLastPlayMode = -1;

	mHoldingSpace = false;

	mFireArray = new FireArray(MAXBULLETS);

	mDir = -1;
	mType = 3;
	tempWallMat = 0; //0 = default, 1 = alpha, 2 = redalpha
	tempMat = 0; //0 = standard, 1 = happy, 2 = still

	mHealth = 100;
	mGoodEnergy = 100;
	mBadEnergy = 100;

}

void Player::Update()
{
	SceneNode* curNode;
	SceneNode* waitNode;
	if(mPlayMode == 1)
	{
		curNode = mNodeGood;
		waitNode = mNodeBad;
	}
	else
	{
		curNode = mNodeBad;
		waitNode = mNodeGood;
	}

	//Get GUI items
	OverlayManager& overlayManager = OverlayManager::getSingleton();
	OverlayElement* PressToRelease = overlayManager.getOverlayElement("GUI_PressToRelease");
	OverlayElement* PressToCharge = overlayManager.getOverlayElement("GUI_PressToCharge");
	OverlayElement* GoodEnergyBar = overlayManager.getOverlayElement("GUI_GoodEnergy");
	OverlayElement* BadEnergyBar = overlayManager.getOverlayElement("GUI_BadEnergy");
	OverlayElement* HealthBar = overlayManager.getOverlayElement("GUI_HealthBar");

	if(mPlayMode)
	{
		if(Game::mKeyboard->isKeyDown(KC_RIGHT))
		{
			mDir = 1;
			curNode->setScale(-1*mPlayMode*2,2,2);
		}
		if(Game::mKeyboard->isKeyDown(KC_LEFT))
		{
			mDir = -1;
			curNode->setScale(1*mPlayMode*2,2,2);
		}

		if(!Game::mKeyboard->isKeyDown(KC_LSHIFT))
		{
			//update movement
			if(Game::mKeyboard->isKeyDown(KC_D))
			{
				mSpeed += Vector3(mAccel.x,0,0);
			}

			if(Game::mKeyboard->isKeyDown(KC_A))
			{
				mSpeed -= Vector3(mAccel.x,0,0);
			}

			if(Game::mKeyboard->isKeyDown(KC_W))
			{
				mSpeed += Vector3(0,mAccel.y,0);
			}

			if(Game::mKeyboard->isKeyDown(KC_S))
			{
				mSpeed -= Vector3(0,mAccel.y,0);
			}

			if(mSpeed.x > mMaxSpeed.x) mSpeed.x = mMaxSpeed.x;
			if(mSpeed.x < -mMaxSpeed.x) mSpeed.x = -mMaxSpeed.x;
			if(mSpeed.y > mMaxSpeed.y) mSpeed.y = mMaxSpeed.y;
			if(mSpeed.y < -mMaxSpeed.y) mSpeed.y = -mMaxSpeed.y;

			mSpeed *= 0.95f;
		}
		
		//playmode-specific stuff:
		if(mPlayMode == 1) //good
		{
			DefenseWall* possibleWall;

			bool done = false;
			for(int i = 0; i < MAXWALLS && !done; i++)
			{
				if(Game::mDefenseWalls[i]->mActive != 2)
				{
					possibleWall = Game::mDefenseWalls[i];
					done = true;
				}
			}

			if(done)
			{
				if(Game::mKeyboard->isKeyDown(KC_SPACE) && mGoodEnergy >= 30)
				{
					if(!mHoldingSpace)
					{
						mHoldingSpace = true;
						possibleWall->mActive = 1;
						possibleWall->mNode->setVisible(true);
					}

					if(tempMat != 1)
					{
						mEntGood->setMaterialName("GoodHappy");
						tempMat = 1;
					}

					possibleWall->mPosition = mPosition+Vector3(20*mDir,0,0);

					if(possibleWall->CheckCollision())
					{
						if(tempWallMat != 2)
						{
							possibleWall->mEnt->setMaterialName("DieMauerRedAlpha");
							tempWallMat = 2;
						}	
					}
					else
					{
						if(tempWallMat != 1)
						{
							possibleWall->mEnt->setMaterialName("DieMauerAlpha");
							tempWallMat = 1;
						}
					}
				}
				else if(mHoldingSpace)
				{	
					mHoldingSpace = false;
					if(!possibleWall->CheckCollision())
					{
						possibleWall->mEnt->setMaterialName("DieMauer");
						tempWallMat = 0;
						possibleWall->mHealth = 100;
						possibleWall->mSpeed = mSpeed;
						possibleWall->mActive = 2;
						mGoodEnergy -= 30;
					}
					else
					{
						possibleWall->mNode->setVisible(false);
						tempWallMat = 0;
					}
					mEntGood->setMaterialName("Good001");
					tempMat = 0;
				}
			}
		}
		else //evil
		{
			if(Game::mKeyboard->isKeyDown(KC_SPACE) && mBadEnergy >= 10)
			{
				if(!mHoldingSpace)
				{
					mHoldingSpace = true;
					Vector3 speed = mSpeed;
					speed += Vector3(mDir*1.0f,0.5f,0);
					mFireArray->AddBullet(mPosition,speed);
					mBadEnergy -= 10;
					mEntBad->setMaterialName("BadGuyHappy");
					tempMat = 1;
				}
			}
			else
			{
				mHoldingSpace = false;
				if(tempMat != 0)
				{
					mEntBad->setMaterialName("BadGuy");
					tempMat = 0;
				}
			}
		}

		//show the press shift button
		if(mPosition.length() < 20)
		{
			PressToCharge->show();

			if((Game::mKeyboard->isKeyDown(KC_LSHIFT) || Game::mKeyboard->isKeyDown(KC_RSHIFT)) && !mHoldingShift)
			{
				//goto charge mode
				mLastPlayMode = mPlayMode;
				mPlayMode = 0;
				mHoldingShift = true;
				mSpeed = Vector3::ZERO;
				mPosition.x = mPosition.y = 0;
				mNodeGood->setVisible(false);
				mNodeBad->setVisible(false);
				mNodeCharge->setVisible(true);
				PressToCharge->hide();
			}
		}
		else
		{
			PressToCharge->hide();
		}
	}
	else //Charging
	{
		PressToRelease->show();

		mSpeed = Vector3::ZERO;
		mPosition.x = mPosition.y = 0;

		mGoodEnergy += 0.1f;
		mBadEnergy += 0.1f;

		if(mGoodEnergy > 100) mGoodEnergy = 100;
		if(mBadEnergy > 100) mBadEnergy = 100;

		if((Game::mKeyboard->isKeyDown(KC_LSHIFT) || Game::mKeyboard->isKeyDown(KC_RSHIFT)) && !mHoldingShift)
		{
			mPlayMode = mLastPlayMode*-1;

			mNodeCharge->setVisible(false);
			mNodeGood->setVisible(true);
			mNodeBad->setVisible(true);
			mNodeGood->setPosition(Vector3::ZERO);
			mNodeBad->setPosition(Vector3::ZERO);

			if(mPlayMode == 1)
			{
				mEntGood->setMaterialName("Good001");
				mEntBad->setMaterialName("BadGuyStill");
			}
			else
			{
				mEntGood->setMaterialName("GoodStill");
				mEntBad->setMaterialName("BadGuy");
			}

			tempMat = 0;

			mHoldingShift = true;
			PressToRelease->hide();
		}
	}

	if(!(Game::mKeyboard->isKeyDown(KC_LSHIFT) || Game::mKeyboard->isKeyDown(KC_RSHIFT)))
		mHoldingShift = false;

	mPosition += mSpeed;

	UpdateCollision();

	curNode->setPosition(mPosition);

	mFireArray->Update();

	GoodEnergyBar->setWidth(mGoodEnergy*4.0f);
	BadEnergyBar->setWidth(mBadEnergy*4.0f);
	HealthBar->setWidth(mHealth*4.0f);
}