package electrosphere.game.server.structure.virtual;

import electrosphere.entity.types.structure.StructureUtils;
import electrosphere.game.data.structure.type.model.StructureType;
import electrosphere.main.Globals;
import electrosphere.game.server.character.Character;
import java.util.LinkedList;
import java.util.List;
import org.joml.Quaternionf;
import org.joml.Vector2f;
import org.joml.Vector3f;

/**
 *
 * @author amaterasu
 */
public class VirtualStructureUtils {
    
    
    public static Structure placeStructureAtPoint(float posX, float posY, String type){
        int worldX = Globals.serverWorldData.convertRealToChunkSpace(posX);
        int worldY = Globals.serverWorldData.convertRealToChunkSpace(posY);
        Structure rVal = new Structure(worldX,worldY,posX,posY,type);
        Globals.macroData.addStructure(rVal);
        
        double centerHeight = Globals.serverTerrainManager.getHeightAtPosition(posX, posY);
        StructureType currentTypeObject = Globals.gameConfigCurrent.getStructureTypeMap().getType(type);
        float radius = currentTypeObject.getRadius();
        for(int x = -(int)radius; x < radius; x++){
            for(int y = -(int)radius; y < radius; y++){
                int newWorldX = Globals.serverWorldData.convertRealToChunkSpace(posX + x);
                int newWorldY = Globals.serverWorldData.convertRealToChunkSpace(posY + y);
                double newLocationX = Globals.serverWorldData.getRelativeLocation(posX + x, newWorldX);
                double newLocationY = Globals.serverWorldData.getRelativeLocation(posY + y, newWorldY);
//                System.out.println("Set height: " + centerHeight);
//                System.out.println("Deform in chunk: " + newWorldX + "," + newWorldY);
                Globals.serverTerrainManager.deformTerrainAtLocationToValue(newWorldX, newWorldY, (int)(newLocationX), (int)(newLocationY), (float)centerHeight);
            }
        }
        StructureUtils.spawnBasicStructure(type, new Vector3f(posX,(float)centerHeight + 2.4f,posY), new Quaternionf());
        return rVal;
    }
    
    public static boolean validStructurePlacementPosition(float posX, float posY, String type){
        StructureType toPlaceType = Globals.gameConfigCurrent.getStructureTypeMap().getType(type);
        Vector2f toPlacePos = new Vector2f(posX, posY);
        for(Structure virtualStruct : Globals.macroData.getStructures()){
            StructureType existantType = Globals.gameConfigCurrent.getStructureTypeMap().getType(virtualStruct.getType());
            Vector2f existantPos = new Vector2f(virtualStruct.getLocationX(),virtualStruct.getLocationY());
            if(existantPos.distance(toPlacePos) < toPlaceType.getRadius() + existantType.getRadius()){
                return false;
            }
        }
        return true;
    }
    
    
    public static void addResident(Structure structure, Character character){
        List<Character> residents = null;
        if(structure.getDataKeys().contains(StructureDataStrings.RESIDENTS)){
            residents = (List)structure.getData(StructureDataStrings.RESIDENTS);
        } else {
            residents = new LinkedList();
            structure.putData(StructureDataStrings.RESIDENTS, residents);
        }
        residents.add(character);
    }
    
    public static List<Character> getResidents(Structure structure){
        return (List)structure.getData(StructureDataStrings.RESIDENTS);
    }
}
