package net.traviangui.model;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import net.traviangui.Util;
import net.traviangui.hostInterface.IOTroops;
import net.traviangui.hostInterface.IOVillage;
public class Village
{
int id = -1;
String name = "<none>";
URL url;
int coordX = Integer.MAX_VALUE;
int coordY = Integer.MAX_VALUE;
List<Terrain> terrains = new ArrayList<Terrain>();
private Date terrainsTimestamp = new Date (0);
private Map<ResourceType,Integer> currentStorage = new Hashtable<ResourceType,Integer>();
private Date currentStorageTimestamp = new Date (0);
private Map<ResourceType,Integer> maximumStorage = new Hashtable<ResourceType,Integer>();
private Date maximumStorageTimestamp = new Date (0);
private Map<ResourceType,Integer> productionRate = new Hashtable<ResourceType,Integer>();
private Date productionRateTimestamp = new Date (0);
private ArrayList<ArrayList<Object>> constructionQueue = new ArrayList<ArrayList<Object>>( );
private Date constructionQueueTimestamp = new Date (0);
private Troops troops;
private Date troopsTimestamp = new Date (0);
static private final SimpleDateFormat dateFormat = new SimpleDateFormat( "HH:mm:ss");
public Village() {
super();
for( ResourceType r : ResourceType.values())
{
currentStorage.put( r, 0);
maximumStorage.put( r, 0);
productionRate.put( r, 0);
}
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public URL getUrl() {
return url;
}
public void setUrl(URL url) {
this.url = url;
}
public int getCoordX() {
return coordX;
}
public void setCoordX(int coordX) {
this.coordX = coordX;
}
public int getCoordY() {
return coordY;
}
public void setCoordY(int coordY) {
this.coordY = coordY;
}
public List<Terrain> getTerrains() throws Exception {
if( Util.obsolete( terrainsTimestamp)) {
IOVillage.updateVillageOverview( this);
terrainsTimestamp = new Date( );
}
return terrains;
}
public void setTerrains(List<Terrain> terrains) {
terrainsTimestamp = new Date( );
this.terrains = terrains;
}
public Map<ResourceType, Integer> getCurrentStorage() throws Exception {
if( Util.obsolete( currentStorageTimestamp)) {
IOVillage.updateVillageOverview( this);
currentStorageTimestamp = new Date( );
}
return currentStorage;
}
public void setCurrentStorage(Map<ResourceType, Integer> currentStorage) {
currentStorageTimestamp = new Date( );
this.currentStorage = currentStorage;
}
public Map<ResourceType, Integer> getMaximumStorage() throws Exception {
if( Util.obsolete( maximumStorageTimestamp)) {
IOVillage.updateVillageOverview( this);
maximumStorageTimestamp = new Date( );
}
return maximumStorage;
}
public void setMaximumStorage(Map<ResourceType, Integer> maximumStorage) {
maximumStorageTimestamp = new Date( );
this.maximumStorage = maximumStorage;
}
public Map<ResourceType, Integer> getProductionRate() throws Exception {
if( Util.obsolete( productionRateTimestamp)) {
IOVillage.updateVillageOverview( this);
productionRateTimestamp = new Date( );
}
return productionRate;
}
public void setProductionRate(Map<ResourceType, Integer> productionRate) {
productionRateTimestamp = new Date( );
this.productionRate = productionRate;
}
public ArrayList<ArrayList<Object>> getConstructionQueue() throws Exception {
if( Util.obsolete( constructionQueueTimestamp)) {
IOVillage.updateVillageOverview( this);
constructionQueueTimestamp = new Date( );
}
return constructionQueue;
}
public void setConstructionQueue(ArrayList<ArrayList<Object>> constructionQueue) {
constructionQueueTimestamp = new Date( );
this.constructionQueue = constructionQueue;
}
public Troops getTroups() throws Exception {
if( Util.obsolete( troopsTimestamp)) {
troops = IOTroops.getTroops( this);
this.troopsTimestamp = new Date( );
}
return troops;
}
public void setTroops( Troops troops) {
this.troopsTimestamp = new Date( );
this.troops = troops;
}
// public MapSquareType getType( ) throws Exception
// {
// if( Util.obsolete( troopsTimestamp)) {
// type = IOMap.getSquareType( this.getUrl());
// this.typeTimestamp = new Date( );
// }
// return type;
// }
//
// public void setType( MapSquareType type) {
// this.typeTimestamp = new Date( );
// this.type = type;
// }
@Override
public String toString() {
ByteArrayOutputStream baoStream = new ByteArrayOutputStream( );
PrintStream oStream = new PrintStream( baoStream);
oStream.printf( "VILLAGE: %s (%d,%d)\n", name, coordX, coordY);
oStream.printf( "Id: %d\n", id);
oStream.printf( "Url: %s\n", url);
oStream.println( "");
if( currentStorageTimestamp.getTime() != 0) {
oStream.println( "Storage: " + dateFormat.format( currentStorageTimestamp));
for( ResourceType r : ResourceType.values())
{
oStream.printf( " - %s=%d/%d (%d units/hour)\r", r.toString(), currentStorage.get( r), maximumStorage.get( r), productionRate.get( r));
}
oStream.println( "");
}
if( terrainsTimestamp.getTime() != 0) {
oStream.println( "Village Map: " + dateFormat.format( terrainsTimestamp));
for( Terrain t : terrains)
{
oStream.println( " - " + t);
}
oStream.println( "");
}
if( constructionQueueTimestamp.getTime() != 0) {
oStream.println( "Construction Queue: " + dateFormat.format( constructionQueueTimestamp));
for( ArrayList<Object> c : constructionQueue)
{
oStream.printf( " - updating %s to level %s, ready at %s after %s hours\n",
c.get( 0), c.get( 1), c.get( 3), c.get( 2));
}
oStream.println( "");
}
return baoStream.toString();
}
public int getCodPosition( )
{
return MapWorld.getCodPosition( coordX, coordY);
}
}