package com.lucasdnd.ags.ui;
import java.util.ArrayList;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.util.FontUtils.Alignment;
import com.lucasdnd.ags.gameplay.Business;
import com.lucasdnd.ags.gameplay.Game;
import com.lucasdnd.ags.gameplay.market.Market;
import com.lucasdnd.ags.gameplay.market.MarketGame;
import com.lucasdnd.ags.system.MainState;
import com.lucasdnd.ags.system.ResourceLoader;
public class ViewGamesList extends List {
private BuyGamesList buyGamesList; // This list must know about the BuyGamesList because it has to change its columns
/**
* Basic Constructor
* @param listHeaders
* @param listRows
*/
public ViewGamesList(float xPos, float yPos, float width, float height,
Business business, BuyGamesList buyGamesList, Market market) throws SlickException {
// Super Class Constructor (for the arrow buttons)
super(xPos, yPos, width, height);
// Get the Objects that will define this List
this.xPos = xPos;
this.yPos = yPos;
this.width = width;
this.height = height;
this.buyGamesList = buyGamesList;
// Now let's create the Headers...
listHeaders = new ListHeader[8];
listHeaders[0] = new ListHeader(0, "Console", Alignment.LEFT, this.xPos + leftMargin, this.yPos + topMargin + lineSize, 280f, lineSize);
listHeaders[1] = new ListHeader(1, "Game", Alignment.LEFT, listHeaders[0].xPos + listHeaders[0].width, this.yPos + topMargin + lineSize, 400f, lineSize);
listHeaders[2] = new ListHeader(2, "Amount", Alignment.CENTER, listHeaders[1].xPos + listHeaders[1].width, this.yPos + topMargin + lineSize, 100f, lineSize);
listHeaders[3] = new ListHeader(3, "Demand", Alignment.CENTER, listHeaders[2].xPos + listHeaders[2].width, this.yPos + topMargin + lineSize, 120f, lineSize);
listHeaders[4] = new ListHeader(4, "Rent Price", Alignment.CENTER, listHeaders[3].xPos + listHeaders[3].width, this.yPos + topMargin + lineSize, 144f, lineSize);
listHeaders[5] = new ListHeader(5, "Sell Price", Alignment.CENTER, listHeaders[4].xPos + listHeaders[4].width, this.yPos + topMargin + lineSize, 144f, lineSize);
listHeaders[6] = new ListHeader(6, "Profit", Alignment.CENTER, listHeaders[5].xPos + listHeaders[5].width, this.yPos + topMargin + lineSize, 98f, lineSize);
listHeaders[7] = new ListHeader(7, "", Alignment.CENTER, listHeaders[6].xPos + listHeaders[6].width, this.yPos + topMargin + lineSize, 30f, lineSize);
// Creates the actual list. This is also called in the BuyGamesList,
// because there we add a new Item
recreateList(business, market);
}
/**
* Specific update method for the View Games List
*/
public void update(GameContainer container, StateBasedGame game, int delta, MainState mainState, Business business,
boolean leftMouseClicked, boolean leftMouseDown, int mouseX, int mouseY, boolean isShiftDown) throws SlickException {
// Super Class update
super.update(container, game, delta, business, leftMouseClicked, leftMouseDown, mouseX, mouseY);
// Header clicks (sorting)
checkHeaderClicks(leftMouseClicked, mouseX, mouseY);
// Updates the List Rows
ViewGamesListRow rowToUpdate = null;
Game gameToRemove = null;
int i = 0;
// Check for updates
for(ListRow lr : listRows) {
// With this if statement, we make sure we update only what's in the current page
if((i >= page * pageSize) && (i <= (page + 1) * (pageSize - 1))) {
// Updates the List Row
((ViewGamesListRow)lr).update(container, game, delta, mainState, business, leftMouseClicked, leftMouseDown, mouseX, mouseY, this, isShiftDown);
// Sell Game
if(((ViewGamesListRow)lr).isDeleteFlag()) {
// Go through each instance of that game and only sell it
// if it's not rented or being played
boolean anyGameFree = false;
for(Game g : business.getStore().getGames()) {
if(g.getReferredMarketGame().getId() == ((ViewGamesListRow)lr).getGame().getReferredMarketGame().getId()) {
if(!g.isBeingPlayed() && !g.isRented()) {
anyGameFree = true;
rowToUpdate = (ViewGamesListRow)lr;
gameToRemove = g;
break;
}
}
}
// If none of the games were available, send a message to the Player
if(!anyGameFree) {
ResourceLoader.getInstance().popSound.play();
// TODO: play a deny sound and tell the Player that game is in use
}
}
}
// Update index
i++;
}
// User clicked dispose game. Update rows
if(rowToUpdate != null) {
// Dispose the game
business.removeGame(gameToRemove);
// Updates this List
updateListInfo(business, gameToRemove);
}
}
/**
* Updates the quantity texts, and removes rows if necessary
* @param business
* @param gameToRemove
* @param rowToRemove
* @throws SlickException
*/
public void updateListInfo(Business business, Game gameToRemove) throws SlickException {
// Find the related List Row
ViewGamesListRow rowToUpdate = findListRow(business, gameToRemove);
if(rowToUpdate != null) {
// Update the Quantity Column in this List
if(business.getStore().getNumberOfGames(gameToRemove.getReferredMarketGame()) > 0) {
rowToUpdate.getListColumns()[2].setText("" + business.getStore().getNumberOfGames(gameToRemove.getReferredMarketGame()));
} else {
// If it was the last one, we remove it from the Store
listRows.remove(rowToUpdate);
// And update their positions
super.updateRowsPositions();
for(ListRow lr : listRows) {
lr.createRectangle();
}
}
// Update the Quantity Column in the BuyGamesList
buyGamesList.updateQuantities(business, gameToRemove.getReferredMarketGame());
}
}
/**
* Given a Game, find the List Row
* @param game
*/
private ViewGamesListRow findListRow(Business business, Game game) {
for(ListRow lr : listRows) {
if(game.getReferredMarketGame().getId() == ((ViewGamesListRow)lr).getGame().getReferredMarketGame().getId()) {
return (ViewGamesListRow)lr;
}
}
return null;
}
/**
* Recreates the list. This is called when the list structure changes (adds or removes an item)
* @param business
* @throws SlickException
*/
public void recreateList(Business business, Market market) throws SlickException {
// We create the List Row
listRows = new ArrayList<ListRow>();
ArrayList<MarketGame> gamesAdded = new ArrayList<MarketGame>();
for(Game g : business.getStore().getGames()) {
// Add it to the List Row...
if (gamesAdded.contains(g.getReferredMarketGame()) == false) {
listRows.add(new ViewGamesListRow(g, business, market));
gamesAdded.add(g.getReferredMarketGame());
}
}
// And update their positions
super.updateRowsPositions();
for(ListRow lr : listRows) {
lr.createRectangle();
}
// Default sort = Game Name
currentSort = 1;
super.sortList(currentSort, false);
}
}