package joshie.harvest.core.handlers;

import joshie.harvest.animals.AnimalTrackerServer;
import joshie.harvest.calendar.CalendarServer;
import joshie.harvest.core.helpers.generic.MCServerHelper;
import joshie.harvest.core.util.HFEvents;
import joshie.harvest.player.PlayerTrackerServer;
import joshie.harvest.town.TownTrackerServer;
import net.minecraft.client.gui.GuiMultiplayer;
import net.minecraft.client.gui.GuiWorldSelection;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.world.World;
import net.minecraftforge.client.event.GuiOpenEvent;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.PlayerEvent.PlayerChangedDimensionEvent;
import net.minecraftforge.fml.common.gameevent.PlayerEvent.PlayerLoggedInEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent.Phase;
import net.minecraftforge.fml.common.gameevent.TickEvent.ServerTickEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import static joshie.harvest.calendar.HFCalendar.TICKS_PER_DAY;

@HFEvents
public class EventsHandler {
    @HFEvents
    public static class ClientReset {
        @SideOnly(Side.CLIENT)
        @SubscribeEvent
        public void onOpenGui(GuiOpenEvent event) {
            if (event.getGui() instanceof GuiWorldSelection || event.getGui() instanceof GuiMultiplayer) {
                HFTrackers.resetClient();
            }
        }
    }

    //Server tick for new day
    @SubscribeEvent
    public void onTick(ServerTickEvent event) {
        if (event.phase != Phase.END) return;
        for (World world: FMLCommonHandler.instance().getMinecraftServerInstance().worldServers) {
            if (world != null) {
                if (world.getWorldTime() % TICKS_PER_DAY == 1) {
                    newDay(world); //Perform everything
                    if (world.provider.getDimension() == 0) { //If it's the overworld, tick the player trackers and the calendar
                        HFTrackers.<CalendarServer>getCalendar(world).newDay(world);
                        for (PlayerTrackerServer player : HFTrackers.getPlayerTrackers()) {
                            player.newDay();
                        }
                    }
                }
            }
        }
    }

    //New day
    public static void newDay(final World world) {
        HFTrackers.getTickables(world).newDay();
        HFTrackers.<AnimalTrackerServer>getAnimalTracker(world).newDay();
        HFTrackers.<TownTrackerServer>getTownTracker(world).newDay();
        HFTrackers.markDirty(world);
    }

    //Sync data on login
    @SubscribeEvent
    public void onPlayerLogin(PlayerLoggedInEvent event) {
        if (event.player instanceof EntityPlayerMP) {
            EntityPlayerMP player = (EntityPlayerMP) event.player;
            HFTrackers.<PlayerTrackerServer>getPlayerTrackerFromPlayer(player).getStats().setBirthday(FMLCommonHandler.instance().getMinecraftServerInstance().worldServers[0]); //Set birthday to overworld date
            HFTrackers.<CalendarServer>getCalendar(player.worldObj).syncToPlayer(player);
            HFTrackers.<TownTrackerServer>getTownTracker(event.player.worldObj).syncToPlayer(player);
            HFTrackers.<PlayerTrackerServer>getPlayerTrackerFromPlayer(player).syncPlayerStats(player);
        }
    }

    @SubscribeEvent
    public void onChangeDimension(PlayerChangedDimensionEvent event) {
        if (event.player instanceof EntityPlayerMP) {
            HFTrackers.<TownTrackerServer>getTownTracker(MCServerHelper.getWorld(event.toDim)).syncToPlayer(event.player); //Resync the town data
        }
    }
}
@@ -1689,25 +1689,25 @@
     
-for (
 World 
+over
 world
-:
+ =
  FML
@@ -1772,24 +1772,21 @@
 dServers
-) %7B%0A    
+%5B0%5D;%0A
         
@@ -1793,45 +1793,12 @@
 if (
-world != null) %7B%0A                if (
+over
 worl
@@ -1842,205 +1842,8 @@
 ) %7B%0A
-                    newDay(world); //Perform everything%0A                    if (world.provider.getDimension() == 0) %7B //If it's the overworld, tick the player trackers and the calendar%0A            
     
@@ -1889,16 +1889,20 @@
 alendar(
+over
 world).n
@@ -1907,24 +1907,28 @@
 .newDay(
+over
 world);%0A
         
@@ -1919,28 +1919,16 @@
 world);%0A
-            
         
@@ -2015,28 +2015,16 @@
         
-            
 player.n
@@ -2040,37 +2040,26 @@
             
-        
-    
 %7D
+%0A
 %0A           
@@ -2063,35 +2063,144 @@
     
-        %7D%0A                %7D
+for (World world : FMLCommonHandler.instance().getMinecraftServerInstance().worldServers) %7B%0A                EventsHandler.newDay(world);
 %0A   

Rewrite the newday tick