package space.animefu.starfallhaven;

import org.junit.Test;
import static org.junit.Assert.*;
import static space.animefu.starfallhaven.SocietySystems.*;

public final class V3SocietyIntegrationTest {
    @Test public void tradeAidAndFamilyStateRoundTripDeterministically()throws Exception{
        GameWorld world=new GameWorld(3303);world.confirmLandingSite();world.crystal=200;world.food=30;
        FactionSimulation.Faction regional=world.factionSimulation.factions.get(0);Faction social=world.society.factions.get(0);
        assertTrue(world.offerAid(regional.id));assertEquals(social.reputation,regional.standing);
        Caravan caravan=world.society.caravans.get(0);TradeOffer offer=caravan.offers.get(0);int before=world.crystal;
        TradeReceipt receipt=world.buyTrade(caravan.id,0,1);assertTrue(receipt.accepted);assertEquals(before-offer.unitPrice,world.crystal);
        FamilyResult expected=world.planChild(world.colonists.get(0).id,world.colonists.get(1).id);assertTrue(expected.accepted);assertEquals(1,world.society.families.pregnancies.size());
        byte[] encoded=GameWorldSave.encode(world);GameWorld restored=GameWorldSave.decode(encoded);
        assertEquals(world.society.factions.get(0).reputation,restored.society.factions.get(0).reputation);
        assertEquals(world.society.caravans.get(0).offers,restored.society.caravans.get(0).offers);
        assertEquals(1,restored.society.families.pregnancies.size());assertArrayEquals(encoded,GameWorldSave.encode(restored));
    }

    @Test public void dawnBirthCreatesPersistentProtectedDependent()throws Exception{
        GameWorld world=new GameWorld(4404);world.confirmLandingSite();assertTrue(world.planChild(world.colonists.get(0).id,world.colonists.get(1).id).accepted);
        Pregnancy pregnancy=world.society.families.pregnancies.get(0);world.society.families.currentDay=pregnancy.dueDay-1;world.day=pregnancy.dueDay-1;world.timeOfDay=.9999f;
        world.update(.1f);assertTrue(world.colonists.size()>3);GameWorld.Colonist child=world.colonists.get(world.colonists.size()-1);
        assertEquals(AgeStage.INFANT,child.ageStage);assertTrue(child.noncombatant());assertFalse(world.setDrafted(child.id,true));
        GameWorld restored=GameWorldSave.decode(GameWorldSave.encode(world));GameWorld.Colonist saved=restored.colonist(child.id);
        assertNotNull(saved);assertEquals(AgeStage.INFANT,saved.ageStage);assertEquals(0,saved.ageDays);assertFalse(restored.setDrafted(saved.id,true));
    }
}
