package com.bernard.greposimu;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import com.bernard.greposimu.model.game.GameConfig;
import com.bernard.greposimu.model.game.GrepoYaml;
import com.bernard.greposimu.model.game.units.Unit;
import com.bernard.greposimu.model.simulator.SimulatorData;
import com.bernard.greposimu.model.simulator.objective.TownObjective;
import com.bernard.greposimu.source.JSONSourcer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature;

@SpringBootTest
class GrepoSimuApplicationTests {

	@Test
	void loadGameData() throws IOException {
		//	System.out.println(GrepoSimu.makeGameData());
	}
	
	@Test
	void testJsonRead() throws IOException {
		System.out.println("Test de sérialisation :");
		GameConfig gc = GrepoSimu.makeGameData();
		SimulatorData sd = JSONSourcer.makeSimulationData(new File("/home/mysaa/Downloads/greposimu.json"),gc);
		//System.out.println(sd);
	}

	TownObjective testObjective(GameConfig gc) {
		return new TownObjective(gc, Map.of(
				gc.getBuilding("main"), 24,
				gc.getBuilding("wall"), 25,
				gc.getBuilding("farm"), 45,
				gc.getBuilding("academy"), 20,
				gc.getBuilding("docks"), 1,
				gc.getBuilding("barracks"), 30),
			Map.of(
				gc.getUnit("sword"), 8.0,
				gc.getUnit("archer"), 8.0,
				gc.getUnit("hoplite"), 16.0,
				gc.getUnit("big_transporter"),7.0),
			Set.of(
				gc.getResearch("hoplite"),
				gc.getResearch("archer"),
				gc.getResearch("conscription")),
			100_000,
			gc.getGod("poseidon"));
	}
	
	@Test
	void testDifferences() throws IOException {
		GameConfig gc = GrepoSimu.makeGameData();
		SimulatorData sd = JSONSourcer.makeSimulationData(new File("/home/mysaa/Downloads/greposimu.json"),gc);
		System.out.println(gc.getUnits().stream().map(Unit::getId).collect(Collectors.joining(",")));
		TownObjective defObjective = testObjective(gc);
		/*
		for(Ville v : sd.getVilles().values()) {
			System.out.println("==== Ville "+v.getNom()+" ====");
			System.out.println(defObjective.getDifferences(v).stream().map(Command::toString).sorted().collect(Collectors.joining("\n")));
		}*/
	}

	@Test
	void writeTestObjective() throws IOException {
		ObjectMapper om = new ObjectMapper(new YAMLFactory().disable(Feature.WRITE_DOC_START_MARKER));
		GameConfig gc = GrepoSimu.makeGameData();
		om.registerModule(new GrepoYaml(gc));
		
		om.writeValue(new File("/tmp/out.yml"), testObjective(gc));

		TownObjective newObjective = om.readValue(new File("/tmp/out.yml"), TownObjective.class);
		System.out.println("READ VALUE");
		System.out.println(newObjective);
	}

}
