package com.newhive.scammerradar.manager;

import com.newhive.scammerradar.ScammerRadarAddon;
import com.newhive.scammerradar.classes.CheckResult;
import com.newhive.scammerradar.classes.Player;
import com.newhive.scammerradar.enums.ListEnum;
import com.newhive.scammerradar.exceptions.ProfileUpdateException;
import com.newhive.scammerradar.manager.submanager.FileManager;
import com.newhive.scammerradar.manager.submanager.ServiceManager;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import javax.naming.ServiceUnavailableException;
import net.labymod.api.labynet.LabyNetController;
import net.labymod.api.util.I18n;
import org.jetbrains.annotations.Nullable;

public class ListManager {
   private final ScammerRadarAddon addon;
   private final HashMap<ListEnum, HashMap<String, Player>> nameLists = new HashMap();
   private HashMap<ListEnum, List<String>> uuidLists = new HashMap();
   private final FileManager fileManager;
   private final ServiceManager serviceManager;
   private final LabyNetController controller;

   public ListManager(LabyNetController controller, ScammerRadarAddon addon) {
      this.addon = addon;
      this.controller = controller;
      this.fileManager = new FileManager();
      this.serviceManager = new ServiceManager();

      try {
         this.nameLists.put(ListEnum.PRIVATE, this.fileManager.readFile());
      } catch (Exception var5) {
         this.addon.logger().error("error while reading/creating private list", new Object[0]);
         this.nameLists.put(ListEnum.PRIVATE, new HashMap());
      }

      try {
         HashMap<ListEnum, HashMap<String, Player>> publicLists = this.serviceManager.getPublicLists();
         this.nameLists.put(ListEnum.SCAMMERRADAR, (HashMap)publicLists.get(ListEnum.SCAMMERRADAR));
         this.nameLists.put(ListEnum.MM, (HashMap)publicLists.get(ListEnum.MM));
      } catch (Exception var4) {
         this.addon.logger().error("error while reading/creating scammerradar/trustedmm list", new Object[0]);
         this.nameLists.put(ListEnum.SCAMMERRADAR, new HashMap());
         this.nameLists.put(ListEnum.MM, new HashMap());
      }

      this.initializeUUIDList();
   }

   public void initializeUUIDList() {
      HashMap<ListEnum, List<String>> result = new HashMap();
      this.uuidLists = result;
      if (!((HashMap)this.nameLists.get(ListEnum.SCAMMERRADAR)).isEmpty()) {
         List<String> list = new ArrayList();
         HashMap<String, Player> map = (HashMap)this.nameLists.get(ListEnum.SCAMMERRADAR);

         for(Player p : map.values()) {
            list.add(p.getUUID());
         }

         this.addon.logger().info("ScammerRadar List:" + list.size(), new Object[0]);
         result.put(ListEnum.SCAMMERRADAR, list);
      } else {
         this.addon.logger().info("SR List empty", new Object[0]);
         result.put(ListEnum.SCAMMERRADAR, new ArrayList());
      }

      if (!((HashMap)this.nameLists.get(ListEnum.PRIVATE)).isEmpty()) {
         List<String> list = new ArrayList();
         HashMap<String, Player> map = (HashMap)this.nameLists.get(ListEnum.PRIVATE);

         for(Player p : map.values()) {
            list.add(p.getUUID());
         }

         this.addon.logger().info("Private List:" + list.size(), new Object[0]);
         result.put(ListEnum.PRIVATE, list);
      } else {
         this.addon.logger().info("Private List empty", new Object[0]);
         result.put(ListEnum.PRIVATE, new ArrayList());
      }

      if (!((HashMap)this.nameLists.get(ListEnum.MM)).isEmpty()) {
         List<String> list = new ArrayList();
         HashMap<String, Player> map = (HashMap)this.nameLists.get(ListEnum.MM);

         for(Player p : map.values()) {
            list.add(p.getUUID());
         }

         this.addon.logger().info("MM List:" + list.size(), new Object[0]);
         result.put(ListEnum.MM, list);
      } else {
         this.addon.logger().info("MM List empty", new Object[0]);
         result.put(ListEnum.MM, new ArrayList());
      }

      this.uuidLists = result;
   }

   public HashMap<String, Player> getScammer() {
      return (HashMap)this.nameLists.get(ListEnum.SCAMMERRADAR);
   }

   public HashMap<String, Player> getLocalScammer() {
      return (HashMap)this.nameLists.get(ListEnum.PRIVATE);
   }

   public List<String> getLocalScammerUUID() {
      return (List)this.uuidLists.get(ListEnum.PRIVATE);
   }

   public void setLocalScammerUUID(List<String> list) {
      this.uuidLists.put(ListEnum.PRIVATE, list);
   }

   public @Nullable CheckResult getPlayer(UUID uuid) {
      String uuidString = uuid.toString();
      CheckResult checkResult = new CheckResult(false, false, false);
      if (this.uuidLists.get(ListEnum.SCAMMERRADAR) != null && ((List)this.uuidLists.get(ListEnum.SCAMMERRADAR)).contains(uuidString)) {
         checkResult.scammer = true;
      }

      if (this.uuidLists.get(ListEnum.MM) != null && ((List)this.uuidLists.get(ListEnum.MM)).contains(uuidString)) {
         checkResult.mm = true;
      }

      if (this.uuidLists.get(ListEnum.PRIVATE) != null && ((List)this.uuidLists.get(ListEnum.PRIVATE)).contains(uuidString)) {
         checkResult.localScammer = true;
      }

      return checkResult.localScammer && checkResult.mm && checkResult.scammer ? checkResult : null;
   }

   public @Nullable CheckResult getPlayer(String playerName) {
      CheckResult checkResult = new CheckResult(false, false, false);
      if (((HashMap)this.nameLists.get(ListEnum.SCAMMERRADAR)).containsKey(playerName)) {
         checkResult.scammer = true;
         checkResult.scammerPlayer = (Player)((HashMap)this.nameLists.get(ListEnum.SCAMMERRADAR)).get(playerName);
      }

      if (((HashMap)this.nameLists.get(ListEnum.PRIVATE)).containsKey(playerName)) {
         checkResult.localScammer = true;
         checkResult.localScammerPlayer = (Player)((HashMap)this.nameLists.get(ListEnum.PRIVATE)).get(playerName);
      }

      if (((HashMap)this.nameLists.get(ListEnum.MM)).containsKey(playerName)) {
         checkResult.mm = true;
         checkResult.mmPlayer = (Player)((HashMap)this.nameLists.get(ListEnum.MM)).get(playerName);
      }

      return !checkResult.localScammer && !checkResult.mm && !checkResult.scammer ? null : checkResult;
   }

   public void setScammer(HashMap<String, Player> scammer) {
      this.nameLists.put(ListEnum.SCAMMERRADAR, scammer);
   }

   public void setMM(HashMap<String, Player> mm) {
      this.nameLists.put(ListEnum.MM, mm);
   }

   public void setLocalScammer(HashMap<String, Player> local_scammer) throws IOException {
      this.nameLists.put(ListEnum.PRIVATE, local_scammer);
      this.fileManager.writeFile(local_scammer);
   }

   public void updateLocalList() throws ProfileUpdateException, IOException {
      HashMap<String, Player> player_map = this.getLocalScammer();
      List<String> player_error = new ArrayList();

      for(Map.Entry<String, Player> entry : player_map.entrySet()) {
         String uuid = ((Player)entry.getValue()).getUUID();
         if (!uuid.equals("")) {
            String result;
            try {
               result = this.addon.getHelper().getUpdateHelper().getName(uuid);
            } catch (Exception var8) {
               player_error.add(((Player)entry.getValue()).getName());
               continue;
            }

            if (!((Player)entry.getValue()).getName().equals(result)) {
               player_map.remove(((Player)entry.getValue()).getName());
               ((Player)entry.getValue()).name = result;
               player_map.put(result, (Player)entry.getValue());
            }
         }
      }

      this.setLocalScammer(player_map);
      if (!player_error.isEmpty()) {
         throw new ProfileUpdateException(I18n.translate("scammerradar.exceptions.commands.update.local-update-error-exception", new Object[0]).concat((String)player_error.stream().collect(Collectors.joining(", "))));
      }
   }

   public void updateOnlineLists() throws ServiceUnavailableException {
      try {
         HashMap<ListEnum, HashMap<String, Player>> map = this.serviceManager.getPublicLists();
         this.setScammer((HashMap)map.get(ListEnum.SCAMMERRADAR));
         this.setMM((HashMap)map.get(ListEnum.MM));
      } catch (ServiceUnavailableException var2) {
         this.setScammer(new HashMap());
         this.setMM(new HashMap());
      }

      this.initializeUUIDList();
   }
}
