package me.semx11.autotip.event;

import java.lang.reflect.Field;
import me.semx11.autotip.Autotip;
import me.semx11.autotip.api.reply.LogoutReply;
import me.semx11.autotip.api.request.LogoutRequest;
import me.semx11.autotip.misc.StartLogin;
import me.semx11.autotip.util.MessageUtil;
import me.semx11.autotip.util.ReflectionUtil;
import me.semx11.autotip.util.UniversalUtil;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiPlayerTabOverlay;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.network.FMLNetworkEvent.ClientConnectedToServerEvent;
import net.minecraftforge.fml.common.network.FMLNetworkEvent.ClientDisconnectionFromServerEvent;

public class EventClientConnection {

    // TODO: Don't hard-code header.
    private static final String HYPIXEL_HEADER = "\u00a7r\u00a7bYou are playing on \u00a7r\u00a7e\u00a7lMC.HYPIXEL.NET\u00a7r";
    private static final Field HEADER_FIELD = ReflectionUtil
            .findField(GuiPlayerTabOverlay.class, "field_175256_i", "header");

//    private static final Pattern IP_PATTERN = Pattern
//            .compile("(^([\\w-]+[.\\u2024])?hypixel[.\\u2024]net|209\\.222\\.115\\.\\d{1,3})");

    public static String lastIp;

    @SubscribeEvent
    public void playerLoggedIn(ClientConnectedToServerEvent event) {
        MessageUtil.clearQueues();

        // TODO: Remove this probably.
        lastIp = UniversalUtil.getRemoteAddress(event).toString().toLowerCase();

        // Thanks, Forge, for not having a proper 'packet received' event.
        Autotip.THREAD_POOL.submit(() -> {
            Object header;
            int attempts = 0;
            while ((header = getHeader()) == null) {
                if (attempts > 15) {
                    return;
                }
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                attempts++;
            }

            if (UniversalUtil.getFormattedText(header).equals(HYPIXEL_HEADER)) {
                Autotip.onHypixel = true;
                EventClientTick.waveCounter = 910;
                Autotip.THREAD_POOL.submit(new StartLogin());
            } else {
                Autotip.onHypixel = false;
            }

        });
    }

    @SubscribeEvent
    public void playerLoggedOut(ClientDisconnectionFromServerEvent event) {
        Autotip.onHypixel = false;

        Autotip.THREAD_POOL.submit(() -> {
            LogoutReply reply = LogoutRequest.doRequest(Autotip.getSessionKey());
            if (!reply.isSuccess()) {
                return;
            }
            Autotip.EXECUTOR.shutdown();
            Autotip.setSessionKey(null);
        });
    }

    public static Object getHeader() {
        try {
            return HEADER_FIELD.get(Minecraft.getMinecraft().ingameGUI.getTabList());
        } catch (IllegalAccessException | NullPointerException e) {
            e.printStackTrace();
            return null;
        }
    }

}
@@ -783,22 +783,20 @@
 rd-code 
-header
+this
 .%0A    pr
@@ -843,22 +843,8 @@
  = %22
-%5Cu00a7r%5Cu00a7b
 You 
@@ -862,29 +862,8 @@
  on 
-%5Cu00a7r%5Cu00a7e%5Cu00a7l
 MC.H
@@ -876,15 +876,8 @@
 .NET
-%5Cu00a7r
 %22;%0A 
@@ -1020,163 +1020,8 @@
 );%0A%0A
-//    private static final Pattern IP_PATTERN = Pattern%0A//            .compile(%22(%5E(%5B%5C%5Cw-%5D+%5B.%5C%5Cu2024%5D)?hypixel%5B.%5C%5Cu2024%5Dnet%7C209%5C%5C.222%5C%5C.115%5C%5C.%5C%5Cd%7B1,3%7D)%22);%0A%0A
     
@@ -1854,17 +1854,19 @@
 Util.get
-F
+Unf
 ormatted

fix hypixel detection issues in 1.12.1