package org.kne.cloud.network.minecraft;

import java.net.Inet6Address;
import java.util.function.Function;
import java.util.function.Supplier;

import org.kne.cloud.network.klalb.CONST;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class KLALBJsonMotdInserter implements Function<MinecraftPacket, MinecraftPacket> {
	private Supplier<Inet6Address> vaddrsup;
	private Supplier<Integer>vportsup;
	@Override
	public MinecraftPacket apply(MinecraftPacket t) {
		if(t instanceof MinecraftMotdResponse) {
			String src=((MinecraftMotdResponse) t).getJson();
			JsonObject jo=(JsonObject) new JsonParser().parse(src);
			JsonObject jok=new JsonObject();
			jok.addProperty("version", CONST.klalbver);
			jok.addProperty("vaddr", vaddrsup.get().getHostAddress());
			jok.addProperty("vport", vportsup.get());
			jo.add("KLALB", jok);
			String des=jo.toString();
			//System.out.println(des);
			return new MinecraftMotdResponse(des);
		}
		return t;
	}
	public KLALBJsonMotdInserter(Supplier<Inet6Address> vaddrsup, Supplier<Integer> vportsup) {
		super();
		this.vaddrsup = vaddrsup;
		this.vportsup = vportsup;
	}

}
