package dev.crucifixbeta.mod.commands.impl;

import asm.accessors.IMinecraftClient;
import dev.crucifixbeta.mod.commands.Command;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.session.Session;
import net.minecraft.client.session.Session.AccountType;

@Environment(EnvType.CLIENT)
public class TokenLoginCommand extends Command {
   private static final Session ORIGINAL_SESSION;

   public TokenLoginCommand() {
      super("tokenlogin", "[login/status/restore/name/skin] [token/name/url]");
   }

   @Override
   public void runCommand(String[] parameters) {
      if (parameters.length == 0) {
         this.sendUsage();
      } else {
         String var2 = parameters[0].toLowerCase();
         switch (var2) {
            case "login":
               this.login(parameters);
               break;
            case "status":
               this.status(parameters);
               break;
            case "restore":
               this.restore();
               break;
            case "name":
               this.changeName(parameters);
               break;
            case "skin":
               this.changeSkin(parameters);
               break;
            default:
               this.sendUsage();
         }
      }
   }

   private void login(String[] parameters) {
      if (parameters.length >= 2 && !parameters[1].isBlank()) {
         String var2 = parameters[1].trim();
         this.sendChatMessage("\u00a7e[TokenLogin] \u6b63\u5728\u9a8c\u8bc1 token...");
         CompletableFuture.runAsync(() -> {
            try {
               TokenLoginApi.Profile var2x = TokenLoginApi.profile(var2);
               Session var3 = new Session(var2x.name(), parseUuid(var2x.id()), var2, Optional.empty(), Optional.empty(), AccountType.MSA);
               mc.execute(() -> {
                  ((IMinecraftClient)mc).setSession(var3);
                  this.sendChatMessage("§a[TokenLogin] 已登录为 §f" + var2x.name());
               });
            } catch (Exception var4) {
               this.sendChatMessage("\u00a7c[TokenLogin] token \u65e0\u6548\u6216\u7f51\u7edc\u8bf7\u6c42\u5931\u8d25");
            }
         });
      } else {
         this.sendUsage();
      }
   }

   private void status(String[] parameters) {
      String var2 = parameters.length >= 2 && !parameters[1].isBlank() ? parameters[1].trim() : mc.getSession().getAccessToken();
      this.sendChatMessage("\u00a7e[TokenLogin] \u6b63\u5728\u68c0\u67e5\u5f53\u524d\u4f1a\u8bdd...");
      CompletableFuture.runAsync(() -> {
         try {
            TokenLoginApi.Profile var2x = TokenLoginApi.profile(var2);
            this.sendChatMessage("§a[TokenLogin] 有效: §f" + var2x.name());
         } catch (Exception var3) {
            this.sendChatMessage("\u00a7c[TokenLogin] \u65e0\u6548");
         }
      });
   }

   private void restore() {
      mc.execute(() -> {
         ((IMinecraftClient)mc).setSession(ORIGINAL_SESSION);
         this.sendChatMessage("§a[TokenLogin] 已恢复启动时的会话: §f" + ORIGINAL_SESSION.getUsername());
      });
   }

   private void changeName(String[] parameters) {
      if (parameters.length >= 2 && !parameters[1].isBlank()) {
         String var2 = parameters[1].trim();
         String var3 = mc.getSession().getAccessToken();
         if (var3.equals(ORIGINAL_SESSION.getAccessToken())) {
            this.sendChatMessage("\u00a7c[TokenLogin] \u5df2\u963b\u6b62\u4fee\u6539\u4e3b\u8d26\u53f7\u540d\u79f0");
         } else {
            this.sendChatMessage("\u00a7e[TokenLogin] \u6b63\u5728\u63d0\u4ea4\u6539\u540d\u8bf7\u6c42...");
            CompletableFuture.runAsync(() -> {
               try {
                  int var3x = TokenLoginApi.changeName(var3, var2);
                  if (var3x == 200) {
                     TokenLoginApi.Profile var4 = TokenLoginApi.profile(var3);
                     Session var5 = new Session(var4.name(), parseUuid(var4.id()), var3, Optional.empty(), Optional.empty(), AccountType.MSA);
                     mc.execute(() -> ((IMinecraftClient)mc).setSession(var5));
                     this.sendChatMessage("§a[TokenLogin] 改名成功: §f" + var4.name());
                  } else if (var3x == 400) {
                     this.sendChatMessage("\u00a7c[TokenLogin] \u540d\u5b57\u683c\u5f0f\u65e0\u6548");
                  } else if (var3x == 401 || var3x == 403) {
                     this.sendChatMessage("\u00a7c[TokenLogin] \u5f53\u524d\u4f1a\u8bdd\u65e0\u6548\u6216\u540d\u5b57\u4e0d\u53ef\u7528");
                  } else if (var3x == 429) {
                     this.sendChatMessage("\u00a7c[TokenLogin] \u8bf7\u6c42\u8fc7\u5feb\uff0c\u8bf7\u7a0d\u540e\u518d\u8bd5");
                  } else {
                     this.sendChatMessage("§c[TokenLogin] 改名失败，状态码: " + var3x);
                  }
               } catch (Exception var6) {
                  this.sendChatMessage("\u00a7c[TokenLogin] \u6539\u540d\u8bf7\u6c42\u5931\u8d25");
               }
            });
         }
      } else {
         this.sendUsage();
      }
   }

   private void changeSkin(String[] parameters) {
      if (parameters.length >= 2 && !parameters[1].isBlank()) {
         String var2 = parameters[1].trim();
         String var3 = mc.getSession().getAccessToken();
         this.sendChatMessage("\u00a7e[TokenLogin] \u6b63\u5728\u63d0\u4ea4\u76ae\u80a4\u8bf7\u6c42...");
         CompletableFuture.runAsync(() -> {
            try {
               int var3x = TokenLoginApi.changeSkin(var3, var2);
               if (var3x == 200 || var3x == 204) {
                  this.sendChatMessage("\u00a7a[TokenLogin] \u76ae\u80a4\u4fee\u6539\u6210\u529f");
               } else if (var3x == 400) {
                  this.sendChatMessage("\u00a7c[TokenLogin] \u76ae\u80a4\u94fe\u63a5\u65e0\u6548");
               } else if (var3x == 401 || var3x == 403) {
                  this.sendChatMessage("\u00a7c[TokenLogin] \u5f53\u524d\u4f1a\u8bdd\u65e0\u6548");
               } else if (var3x == 429) {
                  this.sendChatMessage("\u00a7c[TokenLogin] \u8bf7\u6c42\u8fc7\u5feb\uff0c\u8bf7\u7a0d\u540e\u518d\u8bd5");
               } else {
                  this.sendChatMessage("§c[TokenLogin] 皮肤修改失败，状态码: " + var3x);
               }
            } catch (Exception var4) {
               this.sendChatMessage("\u00a7c[TokenLogin] \u76ae\u80a4\u8bf7\u6c42\u5931\u8d25");
            }
         });
      } else {
         this.sendUsage();
      }
   }

   private static UUID parseUuid(String value) {
      String var1 = value.replace("-", "");
      return UUID.fromString(
         var1.substring(0, 8) + "-" + var1.substring(8, 12) + "-" + var1.substring(12, 16) + "-" + var1.substring(16, 20) + "-" + var1.substring(20)
      );
   }

   @Override
   public String[] getAutocorrect(int count, List<String> separated) {
      if (count != 1) {
         return null;
      } else {
         String var3 = ((String)separated.getLast()).toLowerCase();
         ArrayList<String> var4 = new ArrayList<>();

         for (String var6 : List.of(
            "login", "status", "restore", "name", "skin"
         )) {
            if (var6.startsWith(var3)) {
               var4.add(var6);
            }
         }

         return var4.toArray(new String[0]);
      }
   }

   static {

      ORIGINAL_SESSION = mc.getSession();
   }
}
