#include "log.h"
#include "toolkit.h"
#include "player.h"
#include "rng.h"
#include "music.h"
#include "land.h"

// Global/main window.
#define LAND_WIDTH    700
#define LAND_HEIGHT   600
#define BUTTON_WIDTH  200
#define BUTTON_HEIGHT 40

// Commodity window.
#define COMMODITY_WIDTH   400
#define COMMODITY_HEIGHT  400

// Outfits.
#define OUTFITS_WIDTH			600
#define OUTFITS_HEIGHT		500

// Shipyard.
#define SHIPYARD_WIDTH		700
#define SHIPYARD_HEIGHT		600
#define SHIPYARD_XPOS			(gl_screen.w-SHIPYARD_WIDTH)/2+100
#define SHIPYARD_YPOS			(gl_screen.h-SHIPYARD_HEIGHT)/2-25

// News window.
#define NEWS_WIDTH    400
#define NEWS_HEIGHT   400

// Bar window.
#define BAR_WIDTH     600
#define BAR_HEIGHT    400

#define MUSIC_TAKEOFF		"liftoff"
#define MUSIC_LAND			"agriculture"

int landed = 0;

static int land_wid = 0; // Primary land window.
static int secondary_wid = 0; // For the second opened land window (We can only have 2 max).
static Planet* planet = NULL;

// Commodity excahnge.
static void commodity_exchange(void);
static void commodity_exchange_close(char* str);
// Outfits.
static void outfits(void);
static void outfits_close(char* str);
// Shipyard.
static void shipyard(void);
static void shipyard_close(char* str);
static void shipyard_update(char* str);
// Spaceport bar.
static void spaceport_bar(void);
static void spaceport_bar_close(char* str);
// News.
static void news(void);
static void news_close(char* str);

// The local market.
static void commodity_exchange(void) {
	char** goods;
	int ngoods;

	goods = malloc(sizeof(char*)*3);
	goods[0] = strdup("Heya!");
	goods[1] = strdup("Just");
	goods[2] = strdup("testing.");
	ngoods = 3;

  secondary_wid = window_create("Commodity Exchange", -1, -1, COMMODITY_WIDTH, COMMODITY_HEIGHT);

  window_addButton(secondary_wid, -20, 20,
        BUTTON_WIDTH, BUTTON_HEIGHT, "btnCommodityClose",
        "Close", commodity_exchange_close);
	
	window_addList(secondary_wid, 20, -40, COMMODITY_WIDTH-30, COMMODITY_HEIGHT-80-BUTTON_HEIGHT,
				"lstGoods", goods, ngoods, 0, NULL);
}

static void commodity_exchange_close(char* str) {
  if(strcmp(str, "btnCommodityClose")==0)
    window_destroy(secondary_wid);
}

static void outfits(void) {
	secondary_wid = window_create("Outfits", -1, -1,
				OUTFITS_WIDTH, OUTFITS_HEIGHT);
	
	window_addButton(secondary_wid, -20, 20,
				BUTTON_WIDTH, BUTTON_HEIGHT, "btnCloseOutfits",
				"Close", outfits_close);
}

static void outfits_close(char* str) {
	if(strcmp(str, "btnCloseOutfits")==0)
		window_destroy(secondary_wid);
}

static void shipyard(void) {
	char** ships;
	int nships;

	secondary_wid = window_create("Shipyard",
				SHIPYARD_XPOS, SHIPYARD_YPOS,
				SHIPYARD_WIDTH, SHIPYARD_HEIGHT);
	
	window_addButton(secondary_wid, -20, 20,
				BUTTON_WIDTH, BUTTON_HEIGHT, "btnCloseShipyard",
				"Close", shipyard_close);
	
	window_addButton(secondary_wid, -40-BUTTON_WIDTH, 20,
				BUTTON_WIDTH, BUTTON_HEIGHT, "btnBuyShip",
				"Buy", NULL);

	window_addText(secondary_wid, 20+200+20, -160,
				SHIPYARD_WIDTH-260, 200, 0, "txtDescription",
				&gl_defFont, &cConsole, NULL);

	// Setup the ships to buy/sell.
	ships = ship_getAll(&nships);
	window_addList(secondary_wid, 20, 40,
				200, SHIPYARD_HEIGHT-120, "lstShipyard",
				ships, nships, 0, shipyard_update);
}

static void shipyard_close(char* str) {
	if(strcmp(str, "btnCloseShipyard")==0)
		window_destroy(secondary_wid);
}

static void shipyard_update(char* str) {
	(void)str;
	char* tship;

	tship = toolkit_getList(secondary_wid, "lstShipyard");
}

// Spaceport bar.
static void spaceport_bar(void) {
  secondary_wid = window_create("SpacePort Bar", -1, -1, BAR_WIDTH, BAR_HEIGHT);

  window_addText(secondary_wid, 20, 20 + BUTTON_HEIGHT + 20,
        BAR_WIDTH-140, BAR_HEIGHT - 40 - BUTTON_HEIGHT - 60,
        0, "txtDescription", &gl_smallFont, &cBlack, planet->bar_description);

  window_addButton(secondary_wid, -20, 20, BUTTON_WIDTH, BUTTON_HEIGHT,
        "btnCloseBar", "Close", spaceport_bar_close);
 
}
static void spaceport_bar_close(char* str) {
  if(strcmp(str, "btnCloseBar")==0)
    window_destroy(secondary_wid);
}

// Planet news reports.
static void news(void) {
  secondary_wid = window_create("New Reports", -1, -1, NEWS_WIDTH, NEWS_HEIGHT);

  window_addText(secondary_wid, 20, 20 + BUTTON_HEIGHT + 20,
        NEWS_WIDTH-40, NEWS_HEIGHT - 20 - BUTTON_HEIGHT - 20 - 20 -20,
        0, "txtNews", &gl_smallFont, &cBlack,
        "News reporters report that they are on strike right now! D:");

  window_addButton(secondary_wid, -20, 20, BUTTON_WIDTH, BUTTON_HEIGHT,
        "btnCloseNews", "Close", news_close);
}

static void news_close(char* str) {
  if(strcmp(str, "btnCloseNews")==0)
    window_destroy(secondary_wid);
}

// Land the player.
void land(Planet* p) {
  if(landed) return;

	// Change music.
	music_load(MUSIC_LAND);
	music_play();

  planet = p;
  land_wid = window_create(p->name, -1, -1, LAND_WIDTH, LAND_HEIGHT);

  // Pretty display.
  window_addImage(land_wid, 20, -440, "imgPlanet", p->gfx_exterior);
  window_addText(land_wid, 440, 80, 200, 460, 0,
        "txtPlanetDesc", &gl_smallFont, &cBlack, p->description);
  // Buttons.
  window_addButton(land_wid, -20, 20, BUTTON_WIDTH, BUTTON_HEIGHT,
        "btnTakeoff", "Takeoff", (void(*)(char*))takeoff);
  
  if(planet_hasService(planet, PLANET_SERVICE_COMMODITY))
    window_addButton(land_wid, -20, 20 + BUTTON_HEIGHT + 20,
          BUTTON_WIDTH, BUTTON_HEIGHT, "btnCommodity",
          "Commodity Exchange", (void(*)(char*))commodity_exchange);
  
  if(planet_hasService(planet, PLANET_SERVICE_SHIPYARD))
    window_addButton(land_wid, -20 - BUTTON_WIDTH - 20, 20,
          BUTTON_WIDTH, BUTTON_HEIGHT, "btnShipyard",
          "Shipyard", (void(*)(char*))shipyard);
  
  if(planet_hasService(planet, PLANET_SERVICE_OUTFITS))
    window_addButton(land_wid, -20 - BUTTON_WIDTH - 20, 20 + BUTTON_HEIGHT + 20,
          BUTTON_WIDTH, BUTTON_HEIGHT, "btnOutfits",
          "Outfits", (void(*)(char*))outfits);
  
  if(planet_hasService(planet, PLANET_SERVICE_BASIC))
    window_addButton(land_wid, 20, 20,
          BUTTON_WIDTH, BUTTON_HEIGHT, "btnNews",
          "News", (void(*)(char*))news);
    window_addButton(land_wid, 20, 20 + BUTTON_HEIGHT + 20,
          BUTTON_WIDTH, BUTTON_HEIGHT, "btnBar",
          "SpaceBar", (void(*)(char*))spaceport_bar);


  landed = 1;
}

// Takeoff from the planet.
void takeoff(void) {
  if(!landed) return;

	music_load(MUSIC_TAKEOFF);
	music_play();

  int sw, sh;
  sw = planet->gfx_space->w;
  sh = planet->gfx_space->h;

  // Set player to another position with random facing direction and no velocity.
  player_warp(planet->pos.x + RNG(-sw/2, sw/2), planet->pos.y + RNG(-sh/2, sh/2));
  vect_pset(&player->solid->vel, 0., 0.);
  player->solid->dir = RNG(0, 359) * M_PI/180.;

  // Heal the player.
  player->armour = player->armour_max;
  player->shield = player->shield_max;

  space_init(NULL);

  planet = NULL;
  window_destroy(land_wid);
  landed = 0;
}