package com.newhive.scammerradar.helper.subhelper;

import com.newhive.scammerradar.ScammerRadarAddon;
import com.newhive.scammerradar.classes.CheckResult;
import com.newhive.scammerradar.config.ScammerRadarConfig;
import com.newhive.scammerradar.enums.NotificationEnum;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import net.labymod.api.client.component.Component;
import net.labymod.api.client.component.TextComponent;
import net.labymod.api.client.component.event.ClickEvent;
import net.labymod.api.client.component.event.HoverEvent;
import net.labymod.api.client.component.format.TextColor;
import net.labymod.api.client.component.format.TextDecoration;
import net.labymod.api.notification.Notification;
import net.labymod.api.notification.NotificationController;
import net.labymod.api.notification.Notification.Type;
import net.labymod.api.util.I18n;

public class NotificationSubHelper {
   private final ScammerRadarAddon addon;
   private final NotificationController notificationController;
   private static final TextComponent srComponent = ((TextComponent.Builder)((TextComponent.Builder)TextComponent.builder().text("§f§l§m---------§7 [§2Scammer§aRadar§7] §f§l§m---------").hoverEvent(HoverEvent.showText(Component.text("§b" + I18n.translate("scammerradar.commands.help.help", new Object[0]))))).clickEvent(ClickEvent.suggestCommand("/scammer"))).build();

   public NotificationSubHelper(ScammerRadarAddon addon, NotificationController notificationController) {
      this.addon = addon;
      this.notificationController = notificationController;
   }

   public void displayNotification(Object o) {
      if (o instanceof Exception) {
         this.displayNotification((Exception)o);
      }

      if (o instanceof String) {
         this.displayNotification((String)o);
      }

      if (o instanceof TextComponent) {
         this.displayNotification((TextComponent)o);
      }

   }

   public void displayNotification(Exception e) {
      String text = e.getMessage();
      if (((NotificationEnum)((ScammerRadarConfig)this.addon.configuration()).getErrorNotificationType().get()).equals(NotificationEnum.CHAT)) {
         TextComponent chatComponent = ((TextComponent.Builder)((TextComponent.Builder)((TextComponent.Builder)TextComponent.builder().append(srComponent)).append(System.lineSeparator())).append(text)).build();
         this.addon.displayMessage(chatComponent);
         this.addon.displayMessage(srComponent);
      } else {
         this.notificationController.push(Notification.builder().icon(IconSubHelper.Common.ADDON).title(((TextComponent)((TextComponent)Component.text(I18n.translate("scammerradar.notifications.title", new Object[0])).color(TextColor.color(64, 128, 64))).append(Component.text(" | ").color(TextColor.color(255, 255, 255)))).append(Component.text(I18n.translate("scammerradar.notifications.error", new Object[0])).color(TextColor.color(255, 0, 0)))).text(Component.text(text)).type(Type.SYSTEM).build());
      }
   }

   public void displayNotification(TextComponent text) {
      TextComponent chatComponent = ((TextComponent.Builder)((TextComponent.Builder)((TextComponent.Builder)TextComponent.builder().append(srComponent)).append(System.lineSeparator())).append(text)).build();
      this.addon.displayMessage(chatComponent);
      this.addon.displayMessage(srComponent);
   }

   public static boolean isValidURL(String urlString) {
      try {
         URL url = new URL(urlString);
         url.toURI();
         return true;
      } catch (Exception var2) {
         return false;
      }
   }

   public void displayNotification(String text) {
      if (((NotificationEnum)((ScammerRadarConfig)this.addon.configuration()).getErrorNotificationType().get()).equals(NotificationEnum.CHAT)) {
         TextComponent chatComponent = ((TextComponent.Builder)((TextComponent.Builder)((TextComponent.Builder)TextComponent.builder().append(srComponent)).append(System.lineSeparator())).append("§f" + text)).build();
         this.addon.displayMessage(chatComponent);
         this.addon.displayMessage(srComponent);
      } else {
         this.notificationController.push(Notification.builder().icon(IconSubHelper.Common.ADDON).title(Component.text(I18n.translate("scammerradar.notifications.title", new Object[0])).color(TextColor.color(64, 128, 64))).text(Component.text(text)).type(Type.SYSTEM).build());
      }
   }

   public TextComponent visualizeCheckResult(CheckResult result) {
      TextComponent.Builder component = TextComponent.builder();
      component.append(TextComponent.builder().text(System.lineSeparator()).build());
      if (result.localScammer) {
         ((TextComponent.Builder)((TextComponent.Builder)component.append(TextComponent.builder().text("§7[").build())).append(((TextComponent.Builder)TextComponent.builder().text("S").color(TextColor.color((Integer)((ScammerRadarConfig)this.addon.configuration()).getChat().getScammerColorLocal().get()))).build())).append(((TextComponent.Builder)TextComponent.builder().append("§7]")).build());
      }

      if (result.scammer) {
         ((TextComponent.Builder)((TextComponent.Builder)component.append(TextComponent.builder().text("§7[").build())).append(((TextComponent.Builder)TextComponent.builder().text("S").color(TextColor.color((Integer)((ScammerRadarConfig)this.addon.configuration()).getChat().getScammerColor().get()))).build())).append(TextComponent.builder().text("§7]").build());
      }

      if (result.mm) {
         ((TextComponent.Builder)((TextComponent.Builder)component.append(TextComponent.builder().text("§7[").build())).append(((TextComponent.Builder)TextComponent.builder().text("MM").color(TextColor.color((Integer)((ScammerRadarConfig)this.addon.configuration()).getChat().getMiddlemanColor().get()))).build())).append(TextComponent.builder().text("§7]").build());
      }

      return component.build();
   }

   public TextComponent getListCheckNotification(HashMap<String, CheckResult> list) {
      if (list.isEmpty()) {
         return ((TextComponent.Builder)TextComponent.builder().append(I18n.translate("scammerradar.commands.check.no-result-server", new Object[0]))).build();
      } else {
         TextComponent scammer_component = TextComponent.builder().build();
         TextComponent local_scammer_component = TextComponent.builder().build();
         TextComponent mm_component = TextComponent.builder().build();
         list.forEach((key, value) -> {
            TextComponent entry = ((TextComponent.Builder)((TextComponent.Builder)TextComponent.builder().append(this.visualizeCheckResult(value))).append(((TextComponent.Builder)((TextComponent.Builder)TextComponent.builder().text(key + " ").clickEvent(ClickEvent.suggestCommand("/scammer check " + key))).hoverEvent(HoverEvent.showText(Component.text(I18n.translate("scammerradar.commands.check.hover", new Object[0]))))).build())).build();
            if (value.scammer) {
               scammer_component.append(entry);
            }

            if (value.localScammer) {
               local_scammer_component.append(entry);
            }

            if (value.mm) {
               mm_component.append(entry);
            }

         });
         TextComponent.Builder result = TextComponent.builder();
         if (scammer_component.getChildren().isEmpty() && local_scammer_component.getChildren().isEmpty() && mm_component.getChildren().isEmpty()) {
            return ((TextComponent.Builder)result.append(I18n.translate("scammerradar.commands.check.no-result-server", new Object[0]))).build();
         } else {
            String var10001 = I18n.translate("scammerradar.commands.check.success-server", new Object[0]);
            result.append(var10001 + System.lineSeparator());
            if (!scammer_component.getChildren().isEmpty()) {
               TextComponent.Builder var6 = TextComponent.builder();
               String var10002 = System.lineSeparator();
               ((TextComponent.Builder)result.append(var6.text(var10002 + "§e" + I18n.translate("scammerradar.terms.scammer", new Object[0]) + ":").build())).append(scammer_component);
            }

            if (!local_scammer_component.getChildren().isEmpty()) {
               TextComponent.Builder var7 = TextComponent.builder();
               String var9 = System.lineSeparator();
               ((TextComponent.Builder)result.append(var7.text(var9 + "§e" + I18n.translate("scammerradar.terms.local-scammer", new Object[0]) + ":").build())).append(local_scammer_component);
            }

            if (!mm_component.getChildren().isEmpty()) {
               TextComponent.Builder var8 = TextComponent.builder();
               String var10 = System.lineSeparator();
               ((TextComponent.Builder)((TextComponent.Builder)result.append(var8.text(var10 + "§e" + I18n.translate("scammerradar.terms.mm", new Object[0]) + ":").build())).append(mm_component)).build();
            }

            return result.build();
         }
      }
   }

   public TextComponent getPlayerCheckNotification(String playerName, CheckResult checkResult) {
      TextComponent component = TextComponent.builder().build();
      if (checkResult != null) {
         TextComponent.Builder var10001 = TextComponent.builder();
         String var10002 = I18n.translate("scammerradar.commands.check.success-name", new Object[0]).replace("<user>", playerName);
         component.append(((TextComponent.Builder)var10001.append(var10002 + System.lineSeparator() + System.lineSeparator())).build());
         TextComponent onList = ((TextComponent.Builder)TextComponent.builder().append(" §7[§2✔§7]")).build();
         TextComponent notOnList = ((TextComponent.Builder)TextComponent.builder().append(" §7[§4❌§7]")).build();
         if (checkResult.localScammer) {
            ((TextComponent)((TextComponent)component.append(((TextComponent.Builder)TextComponent.builder().append(I18n.translate("scammerradar.terms.local-scammer", new Object[0]))).build())).append(onList)).append(TextComponent.builder().text(System.lineSeparator()).build());
         } else {
            ((TextComponent)((TextComponent)component.append(((TextComponent.Builder)TextComponent.builder().append(I18n.translate("scammerradar.terms.local-scammer", new Object[0]))).build())).append(notOnList)).append(TextComponent.builder().text(System.lineSeparator()).build());
         }

         if (checkResult.scammer) {
            ((TextComponent)((TextComponent)component.append(((TextComponent.Builder)TextComponent.builder().append(I18n.translate("scammerradar.terms.scammer", new Object[0]))).build())).append(onList)).append(TextComponent.builder().text(System.lineSeparator()).build());
         } else {
            ((TextComponent)((TextComponent)component.append(((TextComponent.Builder)TextComponent.builder().append(I18n.translate("scammerradar.terms.scammer", new Object[0]))).build())).append(notOnList)).append(TextComponent.builder().text(System.lineSeparator()).build());
         }

         if (checkResult.mm) {
            ((TextComponent)((TextComponent)component.append(((TextComponent.Builder)TextComponent.builder().append(I18n.translate("scammerradar.terms.mm", new Object[0]))).build())).append(onList)).append(TextComponent.builder().text(System.lineSeparator()).build());
         } else {
            ((TextComponent)((TextComponent)component.append(((TextComponent.Builder)TextComponent.builder().append(I18n.translate("scammerradar.terms.mm", new Object[0]))).build())).append(notOnList)).append(TextComponent.builder().text(System.lineSeparator()).build());
         }

         if (!checkResult.localScammerPlayer.getNotice().isEmpty() || !checkResult.scammerPlayer.getNotice().isEmpty()) {
            component.append(TextComponent.builder().text(System.lineSeparator()).build());
         }

         if (checkResult.localScammer && !checkResult.localScammerPlayer.getNotice().isEmpty()) {
            if (isValidURL(checkResult.localScammerPlayer.getNotice())) {
               try {
                  component.append(((TextComponent.Builder)((TextComponent.Builder)((TextComponent.Builder)TextComponent.builder().append(I18n.translate("scammerradar.commands.check.local-notice", new Object[0]))).append(((TextComponent.Builder)((TextComponent.Builder)((TextComponent.Builder)((TextComponent.Builder)TextComponent.builder().text(checkResult.localScammerPlayer.getNotice()).color(TextColor.color(0, 180, 210))).hoverEvent(HoverEvent.showText(Component.text(I18n.translate("scammerradar.commands.check.notice-link-description", new Object[0]))))).clickEvent(ClickEvent.openUrl(new URL(checkResult.localScammerPlayer.getNotice())))).decorate(TextDecoration.UNDERLINED)).build())).append(System.lineSeparator())).build());
               } catch (MalformedURLException var8) {
                  this.addon.logger().error("malformed URL", new Object[0]);
               }
            } else {
               var10001 = (TextComponent.Builder)TextComponent.builder().append(I18n.translate("scammerradar.commands.check.local-notice", new Object[0]));
               var10002 = checkResult.localScammerPlayer.getNotice();
               component.append(((TextComponent.Builder)var10001.append(var10002 + System.lineSeparator())).build());
            }
         }

         if (checkResult.scammer && !checkResult.scammerPlayer.getNotice().isEmpty()) {
            if (isValidURL(checkResult.scammerPlayer.getNotice())) {
               try {
                  component.append(((TextComponent.Builder)((TextComponent.Builder)((TextComponent.Builder)TextComponent.builder().append(I18n.translate("scammerradar.commands.check.scammerradar-notice", new Object[0]))).append(((TextComponent.Builder)((TextComponent.Builder)((TextComponent.Builder)((TextComponent.Builder)TextComponent.builder().text(checkResult.scammerPlayer.getNotice()).color(TextColor.color(0, 180, 210))).hoverEvent(HoverEvent.showText(Component.text(I18n.translate("scammerradar.commands.check.notice-link-description", new Object[0]))))).clickEvent(ClickEvent.openUrl(new URL(checkResult.scammerPlayer.getNotice())))).decorate(TextDecoration.UNDERLINED)).build())).append(System.lineSeparator())).build());
               } catch (MalformedURLException var7) {
                  this.addon.logger().error("malformed URL", new Object[0]);
               }
            } else {
               var10001 = (TextComponent.Builder)TextComponent.builder().append(I18n.translate("scammerradar.commands.check.scammerradar-notice", new Object[0]));
               var10002 = checkResult.scammerPlayer.getNotice();
               component.append(((TextComponent.Builder)var10001.append(var10002 + System.lineSeparator())).build());
            }
         }

         return component;
      } else {
         component.append(((TextComponent.Builder)TextComponent.builder().append(I18n.translate("scammerradar.commands.check.no-result-name", new Object[0]).replace("<user>", playerName))).build());
         return component;
      }
   }
}
