package electrosphere.game.server.structure.virtual;

import com.google.gson.Gson;
import electrosphere.game.server.db.DatabaseResult;
import electrosphere.logger.LoggerInterface;
import electrosphere.main.Globals;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author amaterasu
 */
public class StructureManager {
    
    static Gson serializer;
    
    static {
        serializer = new Gson();
    }
    
    static int structIDIterator = 0;
    
    
    public static Structure createStructure(int worldX, int worldY, float localX, float localY, String type){
        int id = structIDIterator;
        structIDIterator++;
        Structure rVal = new Structure(worldX, worldY, localX, localY, type);
        Globals.dbController.executeStatement("INSERT INTO structWorldPositions (structID,posX,posY) VALUES (" + structIDIterator + "," + worldX + "," + worldY + ");");
        Globals.dbController.executeStatement("INSERT INTO structData(structID,propName,propValue) VALUES(" + structIDIterator + ",'localX','" + localX + "');");
        Globals.dbController.executeStatement("INSERT INTO structData(structID,propName,propValue) VALUES(" + structIDIterator + ",'localY','" + localY + "');");
        return rVal;
    }
    
    public static List<Structure> getStructuresInChunk(int worldX, int worldY){
        List<Structure> rVal = new LinkedList();
        DatabaseResult result = Globals.dbController.executeStatement("SELECT * FROM structWorldPositions WHERE posX = " + worldX + " AND posY = " + worldY + ";");
        List<Integer> returnedIDs = new LinkedList();
        if(result.hasResultSet()){
            ResultSet positionSet = result.getResultSet();
            try {
                while(positionSet.next()){
                    returnedIDs.add(positionSet.getInt("structID"));
                }
            } catch (SQLException ex) {
                LoggerInterface.loggerEngine.ERROR("Error looping through positions returned in StructureManager.getStructuresInChunk", ex);
            }
        }
        if(returnedIDs.size() > 0){
            String ids = "";
            for(int ID : returnedIDs){
                ids = ids + ID + ",";
            }
            ids = ids.substring(0, ids.length() - 1);
            result = Globals.dbController.executeStatement("SELECT * FROM structData WHERE structID IN (" + ids + ");");
            if(result.success() && result.hasResultSet()){
                ResultSet rs = result.getResultSet();
                try {
                    while(rs.next()){
                        String rawData = rs.getString("dataVal");
                        Structure newStruct = serializer.fromJson(rawData, Structure.class);
                        rVal.add(newStruct);
                    }
                } catch (SQLException ex) {
                    LoggerInterface.loggerEngine.ERROR("Error looping through structures returned in StructureManager.getStructuresInChunk", ex);
                }
            }
        }
        return rVal;
    }
}
