#include "shop.h"

BuyShop::BuyShop(int sx, int sy, int sw, int sh, __shop sshop, bool sstarting)
{
}
BuyShop::~BuyShop()
{
}
void BuyShop::ResetValues()
{
}
void BuyShop::Display()
{
}
int BuyShop::PollEvent(SDL_Event event)
{
	return 0;
}

ShipyardShop::ShipyardShop(int sx, int sy, int sw, int sh, FPKShip **fpkships, bool sstarting)
{
	Log::WriteVerbose("New ShipyardShop");
	int i;
	x=sx;
	y=sy;
	w=sw;
	h=sh;
	click=-1;
	slottopline=0;
	slotlines=0;
	sGUI=new GUI;
	slotGUI=new GUI;
	slotshow=new GUI;
	starting=sstarting;
	showslots=false;
	font=TTF_OpenFont("fonts/arial.ttf", 12);
	font14=TTF_OpenFont("fonts/arial.ttf", 14);
	cr[0]=0;   cg[0]=0;   cb[0]=0; //none
	cr[1]=0;   cg[1]=255; cb[1]=255; //cyan
	cr[2]=0;   cg[2]=255; cb[2]=0;   //green
	cr[3]=103; cg[3]=8;   cb[3]=123; //violet
	cr[4]=255; cg[4]=255; cb[4]=0;   //yellow
	cr[5]=255; cg[5]=255; cb[5]=255; //white
	numcolors=6;
	sGUI->Init();
	prev=sGUI->MakeButton(x+35, y+h-75, 40, "< Prev");
	back=-1;
	if (starting) back=sGUI->MakeButton(x+(w/2)-70, y+h-40, 140, "Back to Scenario Selection");
	select=sGUI->MakeButton(x+(w/2)-95, y+h-75, 90, "Select this Ship");
	viewslots=sGUI->MakeButton(x+(w/2)+5, y+h-75, 90, "View Slots");
	next=sGUI->MakeButton(x+w-75, y+h-75, 40, "Next >");
	prevcolor=sGUI->MakeButton(x+w-115, y+(h/2)-40, 80, "< Prev Color");
	nextcolor=sGUI->MakeButton(x+w-115, y+(h/2)+16, 80, "Next Color >");
	slotGUI->Init();
	slotpageup=slotGUI->MakeDirButton(x+w-40, y+50, 0);
	back2=slotGUI->MakeButton(x+(w/2)-55, y+h-75, 110, "Back to Ship Select");
	slotpagedown=slotGUI->MakeDirButton(x+w-40, y+h-85, 1);
	this->ReloadSlotShowGUI(fpkships[0]);
	shipmax=0;
	for(i=0; i<100; i++)
	{
		if(!fpkships[i])
		{
			shipmax=i;
			break;
		}
	}
	Log::WriteVerbose("Max FPKShips=%d",shipmax);
	cur=0;
	curcolor=0;
	ships=fpkships;
	ships[cur]->Colorize(cr[curcolor], cg[curcolor], cb[curcolor]);
	engine=new FPKEngine("base.fpk", "Warp 5 Engine");
}
ShipyardShop::~ShipyardShop()
{
	int i;
	for(i=0; i<shipmax; i++)
	{
		delete ships[i];
	}
	delete engine;
	TTF_CloseFont(font);
	TTF_CloseFont(font14);
	delete sGUI;
	delete slotGUI;
	delete slotshow;
}
void ShipyardShop::ResetValues()
{
	cur=0;
	curcolor=0;
}
void ShipyardShop::Display()
{
	if(!showslots)
	{
		this->DisplayShipInfo(ships[cur], curcolor);
		sGUI->Draw();
	}
	else
	{
		this->DisplayShipSlots(ships[cur]);
		slotGUI->Draw();
	}
}
int ShipyardShop::PollEvent(SDL_Event event)
{
	int getx, gety;
	int done=0;

	// an event was found
	switch (event.type) {
	// close button clicked
	case SDL_QUIT:
		done=-1;
		break;

	// handle the keyboard
	case SDL_KEYDOWN:
		switch (event.key.keysym.sym) {
			case SDLK_ESCAPE:
			done = 1;
			break;
			default:
				break;
		}
		break;
	case SDL_MOUSEMOTION:
		SDL_GetMouseState(&getx, &gety);
		if (!showslots) sGUI->SendMouseMotion(getx,gety);
		if (showslots) slotGUI->SendMouseMotion(getx,gety);
		break;
	case SDL_MOUSEBUTTONDOWN:
		SDL_GetMouseState(&getx, &gety);
		if (!showslots) click = sGUI->SendMouseClick(getx, gety);
		if (showslots) click = slotGUI->SendMouseClick(getx, gety);
		break;
	case SDL_MOUSEBUTTONUP:
		SDL_GetMouseState(&getx, &gety);
		if (click!=-1 && click==sGUI->SendMouseClick(getx, gety) && !showslots)
		{
			if (click==prev)
			{
				if (cur-1<0) cur=shipmax-1;
				else cur--;
				ships[cur]->Colorize(cr[curcolor], cg[curcolor], cb[curcolor]);
				delete slotshow;
				slotshow=new GUI;
				this->ReloadSlotShowGUI(ships[cur]);
			}
			if (click==select)
			{
				if ((int)ship->GetCredits()>=(ships[cur]->GetPrice()-(ship->GetPriceBought()*0.75)))
				{
					Log::WriteVerbose("Giving New Ship");
					Log::WriteVerbose("Colorizing ship");
					ship->Colorize(cr[curcolor], cg[curcolor], cb[curcolor]);
					Log::WriteVerbose("Getting New ship images");
					ship->FPK_NewImages(ships[cur]->GetFPK(), ships[cur]->GetImageFN());
					Log::WriteVerbose("Setting max speed");
					ship->SetMaxSpeed(engine->GetMaxSpeed());
					Log::WriteVerbose("Setting max warp speed");
					ship->SetMaxWarp(engine->GetMaxWarpSpeed());
					Log::WriteVerbose("Setting max safe warp speed");
					ship->SetMaxSafeWarp(engine->GetMaxSafeWarpSpeed());
					Log::WriteVerbose("Setting turn delay");
					ship->SetTurnDelay(engine->GetTurnDelay());
					Log::WriteVerbose("Setting warp turn delay");
					ship->SetWarpTurnDelay(engine->GetWarpTurnDelay());
					Log::WriteVerbose("Filling slots");
					ship->FillSlots(ships[cur]->GetSlots(),ships[cur]->GetSlotCount());
					Log::WriteVerbose("Setting shoot point");
					ship->SetShootPoint(ships[cur]->GetShootX(),ships[cur]->GetShootY());
					Log::WriteVerbose("Setting overheat");
					ship->SetOverHeatMax(engine->GetOverHeatMax());
					Log::WriteVerbose("Adding weapons");
					ship->AddWeaponToSlot(101,"base.fpk","gfx/object/bullet.png");
					//ship->AddWeaponToSlot(102,"base.fpk","gfx/object/bullet2.png");
					if (ships[cur]->HasShield()) Log::WriteVerbose("Adding shield");
					if (ships[cur]->HasShield()) ship->AddShield(ships[cur]->GetShieldFN());
					Log::WriteVerbose("Setting battle hits");
					ship->SetHullHitMax(ships[cur]->GetHullHitMax());
					ship->SetShieldHitMax(10);
					ship->ResetHits();
					Log::WriteVerbose("Ship loaded");
					ship->SetCredits((int)(ship->GetCredits()-(ships[cur]->GetPrice()-(ship->GetPriceBought()*0.75))));
					ship->SetPriceBought(ships[cur]->GetPrice());
					done=2;
				}
			}
			if (click==viewslots)
			{
				showslots=true;
			}
			if (click==back)
			{
				done=1;
			}
			if (click==next)
			{
				if (cur+1>=shipmax) cur=0;
				else cur++;
				ships[cur]->Colorize(cr[curcolor], cg[curcolor], cb[curcolor]);
				delete slotshow;
				slotshow=new GUI;
				this->ReloadSlotShowGUI(ships[cur]);
			}
			if (click==prevcolor)
			{
				if (curcolor-1>=0) curcolor--;
				else curcolor=numcolors-1;
				ships[cur]->Colorize(cr[curcolor], cg[curcolor], cb[curcolor]);
			}
			if (click==nextcolor)
			{
				if (curcolor+1<numcolors) curcolor++;
				else curcolor=0;
				ships[cur]->Colorize(cr[curcolor], cg[curcolor], cb[curcolor]);
			}
		}
		if (click!=-1 && click==slotGUI->SendMouseClick(getx, gety) && showslots)
		{
			if (click==back2)
			{
				showslots=false;
			}
			if (click==slotpageup && slotlines>1)
			{
				if (slottopline>0) slottopline--;
				else slottopline=0;
				delete slotshow;
				slotshow=new GUI;
				this->ReloadSlotShowGUI(ships[cur]);
			}
			if (click==slotpagedown && slotlines>1)
			{
				if (slottopline<slotlines-max_y) slottopline++;
				else slottopline=slotlines-max_y;
				delete slotshow;
				slotshow=new GUI;
				this->ReloadSlotShowGUI(ships[cur]);
			}
		}
		slotGUI->UpdateButtons();
		sGUI->UpdateButtons();
		click=-1;
		break;
	default:
		break;
	}
	return done;
}
void ShipyardShop::DisplayShipInfo(FPKShip *curship, int curcolor)
{
	SDL_Surface *screen=SDL_GetVideoSurface();
	SDL_Color white = { 0xFF, 0xFF, 0xFF, 0 };
	if (starting) centerString(font14, x+(w/2), y+20, "Ship Selection", white);
	int price=(int)(ships[cur]->GetPrice()-(ship->GetPriceBought()*0.75));
	char c[300];
	sprintf(c, "Price: %d", price);
	std::string shipprice=c;
	centerString(font, x+(w/2), y+40, curship->GetName(), white);
	centerString(font, x+(w/2), y+54, shipprice, white);
	if (starting)
	{
		sprintf(c, "Credits: %d", ship->GetCredits());
		std::string scredits=c;
		drawString(font, x+20, (y+h)-110, scredits, white);
	}
	drawString(font, x+w-150, y+60, "Width: ", white);
	drawString(font, x+w-40, y+60, curship->GetSW(), white);
	drawString(font, x+w-150, y+76, "Length: ", white);
	drawString(font, x+w-40, y+76, curship->GetSL(), white);
	drawString(font, x+w-150, y+92, "Depth: ", white);
	drawString(font, x+w-40, y+92, curship->GetSD(), white);
	DrawIMG(curship->GetImage(),x+(w/2)-curship->GetImage()->w/2,y+(h/2)-curship->GetImage()->h/2);
	if(curship->HasShield()) DrawIMG(curship->GetShield(),x+(w/2)-curship->GetShield()->w/2,y+(h/2)-curship->GetShield()->h/2);
	rectangleRGBA(screen, x+w-120, y+(h/2)-9, x+w-30, y+(h/2)+9, 255, 255, 255, 255);
	if (cr[curcolor] || cg[curcolor] || cb[curcolor]) boxRGBA(screen, x+w-119, y+(h/2)-8, x+w-31, y+(h/2)+8, cr[curcolor], cg[curcolor], cb[curcolor], 255);
}
void ShipyardShop::DisplayShipSlots(FPKShip *curship)
{
	//SDL_Surface *screen=SDL_GetVideoSurface();
	SDL_Color white = { 0xFF, 0xFF, 0xFF, 0 };
	if (starting) centerString(font14, x+(w/2), y+20, "Ship Slots", white);
	char c[500];
	int price=(int)(ships[cur]->GetPrice()-(ship->GetPriceBought()*0.75));
	sprintf(c, "Price: %d", price);
	std::string shipprice=c;
	centerString(font, x+(w/2), y+40, curship->GetName(), white);
	centerString(font, x+(w/2), y+54, shipprice, white);
	if (starting)
	{
		sprintf(c, "Credits: %d", ship->GetCredits());
		std::string scredits=c;
		drawString(font, x+20, (y+h)-110, scredits, white);
	}
	slotshow->Draw();
}
void ShipyardShop::ReloadSlotShowGUI(FPKShip *curship)
{
	char c[500];
	//determine how many slots can fit on one line
	int max_x=(w-160)/100;  //160 is our pixel cushion
	max_y=(h-180)/86;  //180 is our pixel cushion
	__ship_slot *slots=curship->GetSlots();
	slotshow->Init();
	int i;
	int column=0;
	int curx=0;
	int cury=y+70;
	int displaynum=0;
	int start=slottopline*max_x;
	if (start>curship->GetSlotCount()) start=0;
	if (curship->GetSlotCount()>(max_x*max_y)) displaynum=max_x*max_y;
	else displaynum=curship->GetSlotCount();
	int end=displaynum+start;
	if (end>curship->GetSlotCount()) end=curship->GetSlotCount();
	for(i=start; i<end; i++)
	{
		if (column>=max_x)
		{
			column=0;
			cury+=80;
		}
		Log::WriteVerbose("column=%d, cury=%d",column,cury);
		curx=x+(column*100)+80;
		slotshow->MakeWindow(curx, cury, 80, 66);
		Log::WriteVerbose("Window %d, %d, %d, %d",curx, cury, 80, 66);
		sprintf(c, "ID: %d", slots[i].id);
		std::string slotid=c;
		std::string slottype="Type: ";
		slottype+=slots[i].type;
		sprintf(c, "Width: %d", slots[i].w);
		std::string width=c;
		sprintf(c, "Length: %d", slots[i].l);
		std::string length=c;
		sprintf(c, "Depth: %d", slots[i].d);
		std::string depth=c;
		slotshow->MakeStaticLeft(curx+4, cury+2, 76, 64, slotid, 8);
		slotshow->MakeStaticLeft(curx+4, cury+14, 76, 52, slottype, 8);
		slotshow->MakeStaticLeft(curx+4, cury+26, 76, 40, width, 8);
		slotshow->MakeStaticLeft(curx+4, cury+38, 76, 28, length, 8);
		slotshow->MakeStaticLeft(curx+4, cury+50, 76, 16, depth, 8);
		column++;
	}
	slotlines=curship->GetSlotCount()/max_x;
	if (curship->GetSlotCount()%max_x!=0) slotlines+=1;
	if (start) slotshow->MakeStaticLeft(x+(max_x*100)+84, y+90, 76, 64, "More /\\", 8);
	if (end<curship->GetSlotCount()) slotshow->MakeStaticLeft(x+(max_x*100)+84, y+90+((max_y-1)*80), 76, 64, "More \\/", 8);
}

int NewShipOptions(int w, int h, FPKShip **fpkships)
{
	Log::WriteVerbose("Inside Ship options");
	SDL_Event event;
	ShipyardShop *shipyard=new ShipyardShop(0,0,w,h,fpkships,true);
	SDL_Surface *screen=SDL_GetVideoSurface();
	int done=0;
	while(!done)
	{
		Slock();
		SDL_FillRect(screen,0,0);
		while (SDL_PollEvent(&event))
		{
			int donetemp=shipyard->PollEvent(event);
			if (donetemp) done=donetemp;
		}
		shipyard->Display();
		Sulock();
		SDL_Flip(screen);
	}
	delete shipyard;
	Log::WriteVerbose("End Ship options");
	return done;
}

