package electrosphere.game.server.town;

import electrosphere.game.server.structure.virtual.Structure;
import electrosphere.engine.Globals;
import electrosphere.game.server.character.Character;

import java.util.LinkedList;
import java.util.List;
import org.joml.Vector2i;

/**
 * Server representation of a town
 */
public class Town {
    
    int id;
    static int idIncrementer = 0;
    
    List<Vector2i> positions = new LinkedList<Vector2i> ();
    
    List<Structure> structures = new LinkedList<Structure>();
    List<Character> residents = new LinkedList<Character>();
    
    final static int avgDiffThreshold = 10;
    
    public static Vector2i findValidTownLocation(){
        // for(int x = 0; x < Globals.serverTerrainManager.getWorldDiscreteSize(); x++){
        //     for(int y = 0; y < Globals.serverTerrainManager.getWorldDiscreteSize(); y++){
        //         // ServerTerrainChunk chunk = Globals.serverTerrainManager.getChunk(x, y);
        //         // float[][] macroValues = chunk.getMacroValues();
        //         // float sum = 0;
        //         // int count = 0;
        //         // for(int i = 0; i < 5; i++){
        //         //     for(int j = 0; j < 5; j++){
        //         //         sum = sum + macroValues[i][j];
        //         //         count++;
        //         //     }
        //         // }
        //         // float average = sum / (float)count;
        //         // if(average > 1000){
        //         //     float diffSum = 0;
        //         //     for(int i = 0; i < 5; i++){
        //         //         for(int j = 0; j < 5; j++){
        //         //             diffSum = diffSum + (float)Math.abs(average - macroValues[i][j]);
        //         //         }
        //         //     }
        //         //     float averageDiff = diffSum / (float)count;
        //         //     if(averageDiff < avgDiffThreshold){
        //         //         return new Vector2i(x,y);
        //         //     }
        //         // }
        //     }
        // }
        return null;
    }
    
    Town(){
        this.id = idIncrementer;
        idIncrementer++;
    }
    
    public static Town createTown(int x, int y){
        Town rVal = new Town();
        rVal.positions.add(new Vector2i(x,y));
        Globals.macroData.addTown(rVal);
//        Random rand = new Random();
//        int structCount = (rand.nextInt(3) + 2);
//        int chunkSize = Globals.serverTerrainManager.getChunkWidth();
//        for(int i = 0; i < structCount; i++){
//            StructurePlacer.placeStructureAtPoint(x * chunkSize + rand.nextFloat() * 100, y * chunkSize + rand.nextFloat() * 100, "building1");
//        }
        return rVal;
    }
    
    public static Town getTownAtPosition(int x, int y){
        Town rVal = null;
//        DatabaseResult locationLookupResult = Globals.dbController.executeQuery("SELECT townID FROM townWorldPositions WHERE posX = " + x + " AND posY = " + y + ";");
//        if(locationLookupResult.hasResultSet()){
//            ResultSet rs = locationLookupResult.getResultSet();
//            int townID = -1;
//            try {
//                if(rs.next()){
//                    townID = rs.getInt("townID");
//                }
//            } catch (SQLException ex) {
//                LoggerInterface.loggerEngine.ERROR("Error reading result set from getTownAtPosition", ex);
//            }
//            if(townID != -1){
//                
//            }
//        }
        for(Town town : Globals.macroData.getTowns()){
            for(Vector2i position : town.positions){
                if(position.x == x && position.y == y){
                    return town;
                }
            }
        }
        return rVal;
    }
    
    public List<Vector2i> getPositions(){
        return positions;
    }
    
    public void addStructure(Structure structure){
        structures.add(structure);
    }
    
    public List<Structure> getStructures(){
        return structures;
    }
    
    public void addResident(Character resident){
        residents.add(resident);
    }
    
    public List<Character> getResidents(){
        return residents;
    }
    
}
