package space.animefu.starfallhaven;

import org.junit.Test;
import java.util.EnumSet;
import static org.junit.Assert.*;
import static space.animefu.starfallhaven.ColonySystems.*;

public class ColonySimulationTest {
    @Test public void colonistsHaveStableIdentitySkillsNeedsAndPriorities() {
        GameWorld w=new GameWorld(91);
        GameWorld.Colonist c=w.colonists.get(0);
        assertNotNull(c.id); assertSame(c,w.colonist(c.id));
        assertFalse(c.traits.isEmpty()); assertTrue(c.skill(Skill.CRAFT)>0);
        c.setPriority(WorkType.CONSTRUCT,1);
        assertEquals(1,c.priority(WorkType.CONSTRUCT));
        float mood=c.mood; c.hunger=0; w.update(.1f);
        assertTrue(c.mood<mood); assertFalse(c.thoughts.isEmpty());
    }

    @Test public void draftedColonistObeysMoveAndAttackOrders() {
        GameWorld w=new GameWorld(92); GameWorld.Colonist c=w.colonists.get(0);
        c.x=10.5f;c.y=10.5f; assertTrue(w.setDrafted(c.id,true));
        assertTrue(w.orderMove(c.id,12,10));
        for(int i=0;i<20;i++)w.update(.1f);
        assertTrue(c.x>11.5f); assertEquals(OrderType.NONE,c.order);
        GameWorld.Enemy e=GameWorld.enemyForTest(c.x+.4f,c.y,30);w.enemies.add(e);
        assertTrue(w.orderAttack(c.id,e.id));float hp=e.health;
        w.update(.1f);assertTrue(e.health<hp);
    }

    @Test public void blueprintsBecomeBedsAndWorkbenchesThenProductionRuns() {
        GameWorld w=new GameWorld(93);w.wood=100;w.stone=100;w.crystal=100;
        Blueprint bed=w.placeBlueprint(StructureType.BED,18,12);
        Blueprint bench=w.placeBlueprint(StructureType.FABRICATOR,22,12);
        assertNotNull(bed);assertNotNull(bench);assertFalse(bed.complete);
        for(int i=0;i<400;i++)w.update(.1f);
        assertTrue(bed.complete);assertTrue(bench.complete);assertFalse(w.beds().isEmpty());
        int before=w.food;assertNotNull(w.queueProduction(bench.id,Recipe.NUTRIENT_PACK,2));
        for(int i=0;i<500;i++)w.update(.1f);
        assertTrue(w.food>before);
    }

    @Test public void stockpileReceivesHauledResourcePiles() {
        GameWorld w=new GameWorld(94);
        StockpileZone zone=w.addStockpile(17,12,3,3,EnumSet.allOf(ResourceType.class));
        ResourcePile pile=w.addResourcePile(ResourceType.WOOD,5,25,14);
        for(int i=0;i<500;i++)w.update(.1f);
        assertTrue(zone.contains(pile.x,pile.y));assertTrue(pile.stored);
    }

    @Test public void enclosedRoomsReportAreaAndBeds() {
        GameWorld w=new GameWorld(95);w.wood=999;w.stone=999;
        for(int x=10;x<=14;x++){w.walls[x][10]=true;w.walls[x][14]=true;}
        for(int y=11;y<14;y++){w.walls[10][y]=true;w.walls[14][y]=true;}
        Blueprint bed=w.placeBlueprint(StructureType.BED,12,12);bed.complete=true;
        RoomStats room=w.roomAt(12,12);
        assertTrue(room.enclosed);assertEquals(9,room.area);assertEquals(1,room.beds);
    }

    @Test public void injuriesReduceEfficiencyAndCanHealDuringRest() {
        GameWorld w=new GameWorld(96);GameWorld.Colonist c=w.colonists.get(0);
        float base=c.efficiency();c.addInjury("Ion burn",30,4);assertTrue(c.efficiency()<base);
        c.energy=5;c.hunger=100;float severity=c.injuries.get(0).severity;
        for(int i=0;i<1000;i++){w.enemies.clear();w.update(.1f);}
        assertTrue(c.injuries.isEmpty()||c.injuries.get(0).severity<severity);
    }
}
