package com.bernard.greposimu;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import com.bernard.greposimu.engine.Registerar;
import com.bernard.greposimu.engine.game.Buildings;
import com.bernard.greposimu.engine.json.MapJsonDeserializer;
import com.bernard.greposimu.model.Dieu;
import com.bernard.greposimu.model.Heros;
import com.bernard.greposimu.model.OffDefStats;
import com.bernard.greposimu.model.game.GameData;
import com.bernard.greposimu.model.game.Power;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.StreamReadFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;

public class GrepoSimu {

	public static GameData readGameData() throws IOException {
		
	    ClassLoader classLoader = ClassLoader.getSystemClassLoader();
	    try (InputStream is = classLoader.getResourceAsStream("gamedata.json")) {
	        if (is == null) return null;
	        try (InputStreamReader isr = new InputStreamReader(is);
	            BufferedReader reader = new BufferedReader(isr)) {
	        	// Reading the file
	            String json = reader.lines().collect(Collectors.joining(System.lineSeparator()));
	            
	            // De-serialize to an object
	            JsonFactory jsonFactory = JsonFactory.builder()
	            		   .enable(StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION)
	            		   .build();
	            SimpleModule module = new SimpleModule();
	            module.addDeserializer(Map.class, new MapJsonDeserializer());
	            ObjectMapper mapper = new ObjectMapper(jsonFactory);
	            mapper.registerModule(module);
	            GameData data = mapper.readValue(json, GameData.class);
	            
	            // Adapting power map
	            Map<String,Power> pmap = new HashMap<>();
	            for(String id : data.powers.keySet()) {
	            	Set<String> types = data.powers.get(id).getTypes();
	            	if(types.isEmpty()) {
	            		pmap.put(id, new Power(data.powers.get(id)));
	            	} else {
	            		for(String type : types) {
		            		pmap.put(id+"."+type, new Power(data.powers.get(id), type));
	            		}
	            	}
	            	
	            }
	            data.powers = pmap;
	            System.out.println(data.powers.get("effort_of_the_huntress"));
	            return data;
	        }
	    }
	}
	
	public static void main(String[] args) {
		for(int i=0;i<26;i++)
			System.out.println(i+"->"+Buildings.wallBonus(i));
	}
	
	public static void mainZ(String[] args) {
		Registerar.regiter();
		
		System.out.println(Registerar.unites.size());
		OffDefStats scoring = new OffDefStats(0, 0, 0, 1.0, 0,0,0,0);
		for(Heros h : withNull(Registerar.heros)) {
			if(h!= null && h.getNom()!="Agamemnon")continue;
			for(int l = 1;l<=20&&h!=null;l+=10-(l%10)) {
				for(Dieu d : withNull(Arrays.asList(Dieu.values()))) {
					
					//System.out.println("Héros: "+((c.h==null)?"aucun":("%s (lvl %d)".formatted(h.getNom(),l)))+" - Dieu: "+((d==null)?"aucun":d.getNom()));
					//up.print(h, l, 1000);
				}
			}
		}
	}
	
	
	
	
	
	public static final <T> Iterable<T> withNull(Iterable<T> itrble) {
		return (Iterable<T>) new Iterable<T>() {
			@Override
			public Iterator<T> iterator() {
				Iterator<T> it = itrble.iterator();
				return new Iterator<T>() {
					boolean pastNull = false;
					@Override
					public boolean hasNext() {
						return !pastNull || it.hasNext();
					}

					@Override
					public T next() {
						if(pastNull)
							return it.next();
						else {
							pastNull = true;
							return null;
						}
					}
				};
			};
		};
	}
}
