package tc.oc.pgm.tablist;

import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent;
import tc.oc.api.docs.Server;
import tc.oc.api.minecraft.MinecraftService;
import tc.oc.commons.core.scheduler.Task;
import tc.oc.commons.bukkit.tablist.DynamicTabEntry;
import tc.oc.commons.bukkit.tablist.TabView;
import tc.oc.commons.core.chat.Component;
import tc.oc.commons.core.formatting.PeriodFormats;
import tc.oc.pgm.match.Match;
import tc.oc.pgm.match.MatchScope;
import tc.oc.pgm.PGMTranslations;
import tc.oc.commons.core.util.DefaultProvider;

import javax.annotation.Nullable;
import javax.inject.Inject;

public class MatchFooterTabEntry extends DynamicTabEntry {

    public static class Factory implements DefaultProvider<Match, MatchFooterTabEntry> {
        @Override
        public MatchFooterTabEntry get(Match key) {
            return new MatchFooterTabEntry(key);
        }
    }

    @Inject private static MinecraftService minecraftService;

    private final Match match;
    private @Nullable Task tickTask;

    public MatchFooterTabEntry(Match match) {
        this.match = match;
    }

    @Override
    public void addToView(TabView view) {
        super.addToView(view);
        if(this.tickTask == null) {
            Runnable tick = new Runnable() {
                @Override public void run() {
                    MatchFooterTabEntry.this.invalidate();
                }
            };
            this.tickTask = match.getScheduler(MatchScope.LOADED).createRepeatingTask(5, 20, tick);
        }
    }

    @Override
    public void removeFromView(TabView view) {
        super.removeFromView(view);
        if(!this.hasViews() && this.tickTask != null) {
            this.tickTask.cancel();
            this.tickTask = null;
        }
    }

    @Override
    public BaseComponent getContent(TabView view) {
        Component content = new Component(ChatColor.DARK_GRAY);

        Server server = minecraftService.getLocalServer();
        String datacenter = server.datacenter();
        String name = server.name();

        if(datacenter != null) {
            content.extra(new Component(datacenter, ChatColor.WHITE, ChatColor.BOLD),
                          new Component(" - "));
        }

        content.extra(new Component(PGMTranslations.get().t("command.match.matchInfo.time", view.getViewer()) + ": ", ChatColor.GRAY),
                      new Component(PeriodFormats.formatColons(this.match.runningTime()), this.match.isRunning() ? ChatColor.GREEN : ChatColor.GOLD));

        if(name != null) {
            content.extra(new Component(" - "),
                          new Component(name, ChatColor.WHITE, ChatColor.BOLD));
        }

        return content;
    }
}

@@ -140,53 +140,8 @@
 er;%0A
-import tc.oc.api.minecraft.MinecraftService;%0A
 impo
@@ -178,16 +178,16 @@
 r.Task;%0A
+
 import t
@@ -370,16 +370,41 @@
 ormats;%0A
+import tc.oc.pgm.Config;%0A
 import t
@@ -580,36 +580,8 @@
 ble;
-%0Aimport javax.inject.Inject;
 %0A%0Apu
@@ -867,71 +867,8 @@
  %7D%0A%0A
-    @Inject private static MinecraftService minecraftService;%0A%0A
     
@@ -1811,67 +1811,8 @@
 );%0A%0A
-        Server server = minecraftService.getLocalServer();%0A
     
@@ -1835,22 +1835,33 @@
 enter = 
-server
+Config.PlayerList
 .datacen
@@ -1886,26 +1886,41 @@
 ing 
-name = 
+server = Config.PlayerList.
 server
-.name
 ();%0A
@@ -2397,20 +2397,22 @@
      if(
-name
+server
  != null
@@ -2507,12 +2507,14 @@
 ent(
-name
+server
 , Ch

Fix tab list not rendering from injection