package org.kne.cloud.network.minecraft;

import java.net.UnknownHostException;
import java.util.function.BiConsumer;

import org.kne.cloud.network.MultiProtocolSocketAddress;
import org.kne.cloud.network.scanner.InetAddressRange;
import org.kne.cloud.network.scanner.PortRange;
import org.kne.cloud.network.scanner.ScanRange;
import org.kne.cloud.network.scanner.TCPNetworkScanner;

public class MinecraftScanner extends TCPNetworkScanner{
	private BiConsumer<MultiProtocolSocketAddress, MinecraftServerPinger>minecraftConsumer;
	
	public BiConsumer<MultiProtocolSocketAddress, MinecraftServerPinger> getMinecraftConsumer() {
		return minecraftConsumer;
	}

	public void setMinecraftConsumer(BiConsumer<MultiProtocolSocketAddress, MinecraftServerPinger> minecraftConsumer) {
		this.minecraftConsumer = minecraftConsumer;
	}
	{
		super.setConsumer((mpsa,s)->{
			if(s!=null) {
			try {
				MinecraftServerPinger msp=new MinecraftServerPinger(mpsa,s);
				if(minecraftConsumer!=null) {
					minecraftConsumer.accept(mpsa, msp);
				}
			} catch (UnknownHostException e) {
				if(minecraftConsumer!=null) {
					minecraftConsumer.accept(mpsa, null);
				}
			} catch (Exception e) {
				if(minecraftConsumer!=null) {
					minecraftConsumer.accept(mpsa, null);
				}
			}
			}else {
				if(minecraftConsumer!=null) {
					minecraftConsumer.accept(mpsa, null);
				}
			}
		});
	}
	public MinecraftScanner() {
		
	}
	
	public MinecraftScanner(int connectTimeout, int soTimeout) {
		super(connectTimeout, soTimeout);
		
	}

	public static void main(String[] args) throws UnknownHostException {
		MinecraftScanner tcn = new MinecraftScanner();
		tcn.getRanges().add(new ScanRange(new InetAddressRange("127.0.0.1","127.0.0.2"),new PortRange(25565,37000)));
		tcn.setMinecraftConsumer((mpsa,mc)->{
			if(mc!=null)
			System.out.println(mc);
		});
		tcn.setProcessConsumer((f)->{//System.out.println(f);
			});
		
		tcn.run();
	}

}
