#include "MW4Headers.hpp"
#include "hudcomp.hpp"
#include "hudtarg.hpp"
#include "hudweapon.hpp"
#include "mwguimanager.hpp"

using namespace MechWarrior4;

namespace HUDTARGET
{
	const int LockLoc[4][4] = {
		{ 454,250,482,260 },
		{ 454,262,482,272 },
		{ 454,274,482,284 },
		{ 454,286,482,296 }
	};

	const int WeapLoc[6][2] = {
		{ 455,300 },
		{ 465,300 },
		{ 475,300 },
		{ 455,316 },
		{ 465,316 },
		{ 475,316 }
	};
};

using namespace HUDTARGET;

HUDReticle::HUDReticle()
{
	Color (0,250,0,255);
	m_Weapons = NULL;
	AddTexture ("hud\\hud5",2,7,10,53,30);			// lock on
	AddTexture ("hud\\hud5",3,7,10,53,30);
	AddTexture ("hud\\hud5",4,7,10,53,30);
	AddTexture ("hud\\hud5",5,7,10,53,30);
	AddTexture ("hud\\hud5",6,7,10,53,30);
	AddTexture ("hud\\hud5",7,7,10,53,30);

	AddTexture ("hud\\hud4",8,176,58,229,111);		// neutral
	AddTexture ("hud\\hud4",9,119,59,170,109);		// enemy
	AddTexture ("hud\\hud4",10,175,0,227,52);		// friendly

	AddTexture ("hud\\hud4",30,121,34,168,49);		// hit rear
	AddTexture ("hud\\hud4",31,121,34,168,49);		// hit forward
	AddTexture ("hud\\hud4",32,121,34,168,49);		// hit left
	AddTexture ("hud\\hud4",33,121,34,168,49);		// hit right

	AddTexture ("hud\\hud4",34,121,18,168,32);		// hit rear
	AddTexture ("hud\\hud4",35,121,18,168,32);		// hit forward
	AddTexture ("hud\\hud4",36,121,18,168,32);		// hit left
	AddTexture ("hud\\hud4",37,121,18,168,32);		// hit right

	AddTexture ("hud\\hud5",20,20,134,26,141);		// 0
	AddTexture ("hud\\hud5",21,27,134,33,141);		// 1
	AddTexture ("hud\\hud5",22,34,134,40,141);		// 2
	AddTexture ("hud\\hud5",23,20,142,26,150);		// 3
	AddTexture ("hud\\hud5",24,27,142,33,150);		// 4
	AddTexture ("hud\\hud5",25,34,142,40,150);		// 5

	Location (Point3D (0.5f,0.5f,0.9f));
	Point3D size;

	m_Textures[30]->Origin (24,-23);
	m_Textures[31]->Origin (24,-23);
	m_Textures[32]->Origin (24,-23);
	m_Textures[33]->Origin (24,-23);

	m_Textures[34]->Origin (24,-21);
	m_Textures[35]->Origin (24,-21);
	m_Textures[36]->Origin (24,-21);
	m_Textures[37]->Origin (24,-21);

	m_Textures[31]->Rotate (Pi);
	m_Textures[32]->Rotate (Pi_Over_2);
	m_Textures[33]->Rotate (Pi+Pi_Over_2);
	
	m_Textures[35]->Rotate (Pi);
	m_Textures[36]->Rotate (Pi_Over_2);
	m_Textures[37]->Rotate (Pi+Pi_Over_2);

	size = m_Textures[8]->Size ();
	m_Textures[8]->Origin (size.x/2.0f,size.y/2.0f);
	size = m_Textures[9]->Size ();
	m_Textures[9]->Origin (size.x/2.0f,size.y/2.0f);
	size = m_Textures[10]->Size ();
	m_Textures[10]->Origin (size.x/2.0f,size.y/2.0f);

	size = m_Textures[2]->Size ();
// MSL 5.04 Increased Hud Range Text Size
//	m_ReticleRangeText = new HUDNumberText ();
	m_ReticleRangeText = new HUDText ();
	m_ReticleRangeText->SetSize(HUDText::MEDIUM_SIZE); // old value : MEDIUM_SIZE
	m_ReticleName = new HUDText ();
	m_ReticleRangeText->Justification (HUDText::LEFT_ALIGN);
    m_ReticleName->SetSize(HUDText::MEDIUM_SIZE); // old value : none
	ReticleRange (0);
	ReticleName ("");

	m_isReticleLockedLRMText = new HUDText ();
	m_isReticleLockedLRMText->SetSize(HUDText::SMALL_SIZE);
	m_isReticleLockedLRMText->Color (0,255,0,255);
	m_isReticleLockedLRMText->UpdateText ("LRM");
	m_isReticleLockedMRMText = new HUDText ();
	m_isReticleLockedMRMText->SetSize(HUDText::SMALL_SIZE);
	m_isReticleLockedMRMText->Color (0,255,0,255);
	m_isReticleLockedMRMText->UpdateText ("MRM");
	m_isReticleLockedSRMText = new HUDText ();
	m_isReticleLockedSRMText->SetSize(HUDText::SMALL_SIZE);
	m_isReticleLockedSRMText->Color (0,255,0,255);
	m_isReticleLockedSRMText->UpdateText ("SRM");
	m_isReticleLockedSSRMText = new HUDText ();
	m_isReticleLockedSSRMText->SetSize(HUDText::SMALL_SIZE);
	m_isReticleLockedSSRMText->Color (0,255,0,255);
	m_isReticleLockedSSRMText->UpdateText ("STK");
	
	m_RotateTime = 0;
	zrot = 0;

	m_LockColor2[0] = MakeColor (0,250,0,255);
	m_LockColor2[1] = MakeColor (0,0,255,255);
	m_LockColor2[2] = MakeColor (255,0,0,255);
	
	m_ReticleAlignment = 0;
	targeting = true;
	scalexy = 6.0f;
	isReticleLocked = 0;
	isReticleLockedLRM = 0;
	isReticleLockedMRM = 0;
	isReticleLockedSRM = 0;
	isReticleLockedSSRM = 0;
	m_LockAlpha = 100;
	rotclockwise = 0;
	rotcounterclock = 0;
	m_Textures[2]->Scale (scalexy,scalexy);
	m_Textures[3]->Scale (scalexy,scalexy);
	m_Textures[4]->Scale (scalexy,scalexy);
	m_Textures[5]->Scale (scalexy,scalexy);
	m_Textures[6]->Scale (scalexy,scalexy);
	m_Textures[7]->Scale (scalexy,scalexy);
	m_Textures[2]->Origin (size.x/2.0f,size.y+35);
	m_Textures[3]->Origin (size.x/2.0f,size.y+35);
	m_Textures[4]->Origin (size.x/2.0f,size.y+35);
	m_Textures[5]->Origin (size.x/2.0f,size.y+35);
	m_Textures[6]->Origin (size.x/2.0f,size.y+35);
	m_Textures[7]->Origin (size.x/2.0f,size.y+35);
	m_Textures[2]->Clip (true);
	m_Textures[3]->Clip (true);
	m_Textures[4]->Clip (true);
	m_Textures[5]->Clip (true);
	m_Textures[6]->Clip (true);
	m_Textures[7]->Clip (true);
	m_HitTime = 0;
	m_LeftHitColor = Color ();
	m_RightHitColor = Color ();
	m_ForwardHitColor = Color ();
	m_RearHitColor = Color ();
	
	m_LeftCenterColor = 0;
	m_RightCenterColor = 0;
	m_ForwardCenterColor = 0;
	m_RearCenterColor = 0;
}

HUDReticle::~HUDReticle()
{
	delete m_ReticleRangeText;
	delete m_ReticleName;
	delete m_isReticleLockedLRMText;
	delete m_isReticleLockedMRMText;
	delete m_isReticleLockedSRMText;
	delete m_isReticleLockedSSRMText;
}

void HUDReticle::Reset (void)
{
	m_Weapons = Cast_Object (HUDWeapon *, MWGUIManager::GetInstance ()->Component (MWGUIManager::HUD_WEAPON));
}


void HUDReticle::AdjustColor (DWORD& color,DWORD alpha)
{
	DWORD dr,dg,db,da;
	DWORD r,g,b,a;
	DWORD destcolor;

	destcolor = Color ();

	r = (color>>16)&0xff;
	g = (color>>8)&0xff;
	b = (color>>0)&0xff;
	a = (color>>24)&0xff;

//	dr = ((destcolor >> 16)&0xff) - r;
//	dg = ((destcolor >> 8)&0xff) - g;
//	db = ((destcolor >> 0)&0xff) - b;
	dr = 0;
	dg = 0;
	db = 0;
	da = ((alpha >> 24)&0xff) - a;

	if (dr != 0)
		dr = dr < 0 ? 5:-5;
	if (dg != 0)
		dg = dg < 0 ? 5:-5;
	if (db != 0)
		db = db < 0 ? 5:-5;
	if (da != 0)
		da = da < 0 ? 5:-5;
	r+=dr;
	g+=dg;
	b+=db;
	a+=da;
	Clamp (r,0,255);
	Clamp (g,0,255);
	Clamp (b,0,255);
	Clamp (a,0,255);
	color = MakeColor (r,g,b,a);
}

void HUDReticle::Update (Stuff::Time till)
{
	if (m_RotateTime == 0)
		m_RotateTime = till;
	if (m_HitTime == 0)
		m_HitTime = till;

	while (m_HitTime <= till)
	{
//		AdjustColor (m_LeftHitColor,0xff000000);
//		AdjustColor (m_RightHitColor,0xff000000);
//		AdjustColor (m_ForwardHitColor,0xff000000);
//		AdjustColor (m_RearHitColor,0xff000000);

		AdjustColor (m_LeftCenterColor,0x00000000);
		AdjustColor (m_RightCenterColor,0x00000000);
		AdjustColor (m_ForwardCenterColor,0x00000000);
		AdjustColor (m_RearCenterColor,0x00000000);
		m_HitTime += 1.0/60.0;
	}
	while (m_RotateTime <= till)
	{
		zrot += 0.06f;
		if (zrot > (2*Pi))
			zrot = 0;
		if (isReticleLocked)
		{
			if (targeting)
			{
				scalexy -= 0.45f;
// MSL 5.04 Disable Missile Lock Zoom In
//				if (scalexy < 0.5f)
//				{
					scalexy = 0.5f;
//				}
				m_Textures[2]->Scale (scalexy,scalexy);
				m_Textures[3]->Scale (scalexy,scalexy);
				m_Textures[4]->Scale (scalexy,scalexy);
				m_Textures[5]->Scale (scalexy,scalexy);
				m_Textures[6]->Scale (scalexy,scalexy);
				m_Textures[7]->Scale (scalexy,scalexy);
			}			

			rotclockwise += (5.0f*2.0f*Pi)/360.0f;
			if (rotclockwise > 2.0f*Pi)
				rotclockwise = 0;

			rotcounterclock -= (3.0f*2.0f*Pi)/360.0f;
			if (rotcounterclock > 2.0f *Pi)
				rotcounterclock = 0;

			if (m_LockAlpha < 210)
			{
				m_LockAlpha +=10;
				if (m_LockAlpha > 210)
				{
					m_LockAlpha = 210;
				}
// MSL 5.04 Target Lock Color Change
//				m_AnimLockColor = MakeColor (255,52,0,m_LockAlpha);
					m_AnimLockColor = MakeColor (200,125,0,m_LockAlpha);
			}
		}
		m_RotateTime += 1.0/26.0;
	}
	inherited::Update (till);
}

void HUDReticle::AllHit (void)
{
//	m_LeftHitColor = MakeColor (255,0,0,255);
//	m_RightHitColor = MakeColor (255,0,0,255);
//	m_ForwardHitColor = MakeColor (255,0,0,255);
//	m_RearHitColor = MakeColor (255,0,0,255);
	
	m_LeftCenterColor = MakeColor (255,0,0,255);
	m_RightCenterColor = MakeColor (255,0,0,255);
	m_ForwardCenterColor = MakeColor (255,0,0,255);
	m_RearCenterColor = MakeColor (255,0,0,255);
}
void HUDReticle::LeftHit (void)
{
//	m_LeftHitColor = MakeColor (255,0,0,255);
	m_LeftCenterColor = MakeColor (255,0,0,255);
}
void HUDReticle::RightHit (void)
{
//	m_RightHitColor = MakeColor (255,0,0,255);
	m_RightCenterColor = MakeColor (255,0,0,255);
}
void HUDReticle::ForwardHit (void)
{
//	m_ForwardHitColor = MakeColor (255,0,0,255);
	m_ForwardCenterColor = MakeColor (255,0,0,255);
}
void HUDReticle::RearHit (void)
{
//	m_RearHitColor = MakeColor (255,0,0,255);
	m_RearCenterColor = MakeColor (255,0,0,255);
}
void HUDReticle::DrawImplementation(void)
{
	Point3D loc,size;
	DWORD color;
	int i;
//	Scalar oldheatlevel;

//	oldheatlevel = g_HUDHeatLevel;
//	g_HUDHeatLevel = 0;

	loc = Location ();
	Verify (m_Weapons);

	color = m_LockColor2[m_ReticleAlignment];
	switch (m_ReticleAlignment)
	{
	case 0:
		m_Textures[8]->Rotate (0);
		size = m_Textures[8]->Size ();
		m_Textures[8]->Draw (loc,size,color);
		break;
	case 1:
		size = m_Textures[10]->Size ();
		m_Textures[10]->Draw (loc,size,color);
		break;
	case 2:
		size = m_Textures[9]->Size ();
		m_Textures[9]->Rotate (zrot);
		m_Textures[9]->Draw (loc,size,color);
		size = m_Textures[8]->Size ();
		m_Textures[8]->Rotate (zrot);
		m_Textures[8]->Draw (loc,size,color);
		break;
	}

	size = m_Textures[30]->Size ();
	color = Color ();

//	m_Textures[30]->Draw (loc,size,m_RearHitColor);
//	m_Textures[31]->Draw (loc,size,m_ForwardHitColor);
//	m_Textures[32]->Draw (loc,size,m_LeftHitColor);
//	m_Textures[33]->Draw (loc,size,m_RightHitColor);
	
	m_Textures[34]->Draw (loc,size,m_RearCenterColor);
	m_Textures[35]->Draw (loc,size,m_ForwardCenterColor);
	m_Textures[36]->Draw (loc,size,m_LeftCenterColor);
	m_Textures[37]->Draw (loc,size,m_RightCenterColor);

	for (i=0;i<3;i++)//sanghoon: originally 6
	{
		size = m_Textures[20+i]->Size ();
		//CanHit is set in Hudweapon...
		if (m_Weapons->CanHit (i))
			color = MakeColor (0,255,0,255);
        else {
		// MSL 5.00
            // - original line //color = MakeColor (0,175,0,255); 
            color = MakeColor (255,255,0,255); 
        }

		my_DrawRect (WeapLoc[i][0]-1,WeapLoc[i][1]-1,(int) (WeapLoc[i][0]+size.x+1),(int) (WeapLoc[i][1]+size.y+1),color);
		m_Textures[20+i]->Draw (Point3D ((Scalar) WeapLoc[i][0],(Scalar) WeapLoc[i][1],0.9f),size,MakeColor (0,0,0,255));
	}

	color = MakeColor (0,0,0,100);
//	my_DrawRect ((int) (loc.x+55),(int) (loc.y+30),(int) (loc.x+130),(int) (loc.y+56),color);

	color = m_LockColor2[m_ReticleAlignment];
//	DrawLine ((int) (loc.x+15),(int) (loc.y+15),(int) (loc.x+40),(int) (loc.y+35),color);
//	DrawLine ((int) (loc.x+40),(int) (loc.y+35),(int) (loc.x+55),(int) (loc.y+35),color);

	m_ReticleName->Color (m_LockColor2[m_ReticleAlignment]);

// MSL 5.00
    // - original line // m_ReticleRangeText->Color (m_LockColor2[m_ReticleAlignment]); 
    if (m_ReticleAlignment==2) 
        m_ReticleRangeText->Color (m_LockColor2[m_ReticleAlignment]); 
    else  if (m_ReticleAlignment==1) 
        m_ReticleRangeText->Color (m_LockColor2[m_ReticleAlignment]); 
    else
        m_ReticleRangeText->Color (255,255,0,255);

//	m_ReticleRangeText->Color (m_LockColor2[m_ReticleAlignment]);
//	m_ReticleName->TopLeft (loc.x+57,loc.y+32);
//	m_ReticleName->BottomRight ((Scalar) 800,(Scalar) 600);
//	m_ReticleRangeText->TopLeft (loc.x+57,loc.y+32);
//	m_ReticleRangeText->BottomRight ((Scalar) 800,(Scalar) 600);

	size = m_Textures[9]->Size ();
	DWORD tsizex,tsizey;
	m_ReticleName->DrawSize (tsizex,tsizey);
	m_ReticleName->Draw (Point3D (loc.x-(tsizex/2.0f),loc.y+(size.y/2.0f)+14,0.9f));
	m_ReticleRangeText->DrawSize (tsizex,tsizey);
	m_ReticleRangeText->Draw (Point3D (loc.x-(tsizex/2.0f),loc.y-((size.y/2.0f)+14+tsizey),0.9f));

	if (isReticleLockedLRM)
	{
		color = MakeColor (10,10,10,150); 
		my_DrawRect (LockLoc[0][0],LockLoc[0][1],(int) (LockLoc[0][2]),(int) (LockLoc[0][3]),color);
		DWORD tsizex,tsizey;
		m_isReticleLockedLRMText->DrawSize (tsizex,tsizey);
		m_isReticleLockedLRMText->Draw (Point3D (loc.x-(tsizex/2.0f)+68,loc.y-((size.y/2.0f)+10+tsizey),0.9f));
	}
	if (isReticleLockedMRM)
	{
		color = MakeColor (10,10,10,150); 
		my_DrawRect (LockLoc[1][0],LockLoc[1][1],(int) (LockLoc[1][2]),(int) (LockLoc[1][3]),color);
		DWORD tsizex,tsizey;
		m_isReticleLockedMRMText->DrawSize (tsizex,tsizey);
		m_isReticleLockedMRMText->Draw (Point3D (loc.x-(tsizex/2.0f)+68,loc.y-((size.y/2.0f)-2+tsizey),0.9f));
	}
	if (isReticleLockedSRM)
	{
		color = MakeColor (10,10,10,150); 
		my_DrawRect (LockLoc[2][0],LockLoc[2][1],(int) (LockLoc[2][2]),(int) (LockLoc[2][3]),color);
		DWORD tsizex,tsizey;
		m_isReticleLockedSRMText->DrawSize (tsizex,tsizey);
		m_isReticleLockedSRMText->Draw (Point3D (loc.x-(tsizex/2.0f)+68,loc.y-((size.y/2.0f)-14+tsizey),0.9f));
	}
	if (isReticleLockedSSRM)
	{
		color = MakeColor (10,10,10,150); 
		my_DrawRect (LockLoc[3][0],LockLoc[3][1],(int) (LockLoc[3][2]),(int) (LockLoc[3][3]),color);
		DWORD tsizex,tsizey;
		m_isReticleLockedSSRMText->DrawSize (tsizex,tsizey);
		m_isReticleLockedSSRMText->Draw (Point3D (loc.x-(tsizex/2.0f)+68,loc.y-((size.y/2.0f)-26+tsizey),0.9f));
	}

	if (isReticleLocked)
	{
		Point3D size;
		size = m_Textures[2]->Size ();
		m_Textures[2]->Rotate (rotclockwise);
		m_Textures[2]->Draw (loc,size,m_AnimLockColor);

		m_Textures[3]->Rotate (rotcounterclock+(60.0f*2.0f*Pi/360.0f));
		m_Textures[3]->Draw (loc,size,m_AnimLockColor);

		m_Textures[4]->Rotate (rotclockwise+(120.0f*2.0f*Pi/360.0f));
		m_Textures[4]->Draw (loc,size,m_AnimLockColor);

		m_Textures[5]->Rotate (rotcounterclock+(180.0f*2.0f*Pi/360.0f));
		m_Textures[5]->Draw (loc,size,m_AnimLockColor);

		m_Textures[6]->Rotate (rotclockwise+(240.0f*2.0f*Pi/360.0f));
		m_Textures[6]->Draw (loc,size,m_AnimLockColor);

		m_Textures[7]->Rotate (rotcounterclock+(300.0f*2.0f*Pi/360.0f));
		m_Textures[7]->Draw (loc,size,m_AnimLockColor);
	}
//	g_HUDHeatLevel = oldheatlevel;
}


