package space.animefu.starfallhaven;

import org.junit.Test;
import java.util.EnumSet;
import java.util.List;
import static org.junit.Assert.*;

public final class RegionalWorldTest {
    @Test public void regionalGenerationIsDeterministicVariedAndSeedSensitive(){
        RegionalMap a=new RegionalMap(73021),b=new RegionalMap(73021),other=new RegionalMap(73022);
        EnumSet<RegionalMap.Biome> biomes=EnumSet.noneOf(RegionalMap.Biome.class);
        EnumSet<RegionalMap.Resource> resources=EnumSet.noneOf(RegionalMap.Resource.class);
        boolean differs=false;
        for(int x=0;x<a.width;x++)for(int y=0;y<a.height;y++){
            RegionalMap.Cell ac=a.cell(x,y),bc=b.cell(x,y),oc=other.cell(x,y);
            assertEquals(ac.biome,bc.biome);assertEquals(ac.resources,bc.resources);assertEquals(ac.suitability,bc.suitability);
            biomes.add(ac.biome);resources.addAll(ac.resources);
            differs|=ac.biome!=oc.biome||!ac.resources.equals(oc.resources)||ac.suitability!=oc.suitability;
        }
        assertTrue(differs);assertTrue(biomes.size()>=4);assertTrue(resources.size()>=4);assertTrue(a.validLandingSites().size()>=3);
    }

    @Test public void landingSelectionRejectsInvalidSitesAndLocksOnConfirmation(){
        GameWorld world=new GameWorld(4431);List<RegionalMap.Cell> valid=world.regionalMap.validLandingSites();
        RegionalMap.Cell choice=valid.get(valid.size()-1);assertTrue(world.selectLandingSite(choice.x,choice.y));
        assertSame(choice,world.selectedLandingSite());
        RegionalMap.Cell invalid=null;for(int x=0;x<world.regionalMap.width;x++)for(int y=0;y<world.regionalMap.height;y++)if(!world.regionalMap.cell(x,y).canLand())invalid=world.regionalMap.cell(x,y);
        assertNotNull(invalid);assertFalse(world.selectLandingSite(invalid.x,invalid.y));
        assertTrue(world.confirmLandingSite());assertFalse(world.selectLandingSite(valid.get(0).x,valid.get(0).y));assertFalse(world.confirmLandingSite());
    }

    @Test public void factionsCoverRelationsAndStrategicEvolutionIsDeterministic(){
        GameWorld a=new GameWorld(9988),b=new GameWorld(9988);
        EnumSet<FactionSimulation.Relation> relations=EnumSet.noneOf(FactionSimulation.Relation.class);
        for(FactionSimulation.Faction faction:a.factionSimulation.factions)relations.add(faction.relation());
        assertEquals(EnumSet.allOf(FactionSimulation.Relation.class),relations);assertTrue(a.factionSimulation.settlements().size()>=4);
        for(int i=0;i<8;i++){a.factionSimulation.advanceTurn();b.factionSimulation.advanceTurn();}
        assertEquals(a.factionSimulation.strategicTurn,b.factionSimulation.strategicTurn);
        for(int i=0;i<a.factionSimulation.factions.size();i++){
            FactionSimulation.Faction af=a.factionSimulation.factions.get(i),bf=b.factionSimulation.factions.get(i);assertEquals(af.standing,bf.standing);
            for(int j=0;j<af.settlements.size();j++){FactionSimulation.Settlement as=af.settlements.get(j),bs=bf.settlements.get(j);assertEquals(as.population,bs.population);assertEquals(as.prosperity,bs.prosperity);assertEquals(as.security,bs.security);assertEquals(as.lastStrategy,bs.lastStrategy);}
        }
        assertTrue(a.factionSimulation.factions.get(0).settlements.get(0).prosperity>28);
    }
}
