package cz.creeper.customitemlibrary.managers;

import com.google.common.collect.Maps;
import cz.creeper.customitemlibrary.CustomItemLibrary;
import cz.creeper.customitemlibrary.event.MiningProgressEvent;
import lombok.AllArgsConstructor;
import lombok.ToString;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.block.BlockSnapshot;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.Order;
import org.spongepowered.api.event.block.InteractBlockEvent;
import org.spongepowered.api.event.cause.Cause;
import org.spongepowered.api.event.filter.cause.First;

import java.util.Map;
import java.util.UUID;

public class MiningManager {
    private final Map<UUID, Mining> playerToMining = Maps.newHashMap();

    public MiningManager start() {
        Sponge.getEventManager().registerListeners(CustomItemLibrary.getInstance(), this);

        return this;
    }

    public void stop() {
        Sponge.getEventManager().unregisterListeners(this);
    }

    @Listener(order = Order.BEFORE_POST)
    public void onInteractBlock(InteractBlockEvent.Primary.MainHand event, @First Player player) {
        UUID playerId = player.getUniqueId();
        int currentTick = Sponge.getServer().getRunningTimeTicks();
        BlockSnapshot snapshot = event.getTargetBlock();
        Mining mining = playerToMining.get(playerId);

        if(mining != null && BlockSnapshot.NONE.equals(snapshot)) {
            int durationTicks = currentTick - mining.tickStarted;
            MiningProgressEvent miningEvent = new MiningProgressEvent(player, mining.snapshot, durationTicks,
                    Cause.source(CustomItemLibrary.getInstance().getPluginContainer()).build(), false);

            Sponge.getEventManager().post(miningEvent);

            if (miningEvent.isCancelled()) {
                playerToMining.remove(playerId);
            }
        } else {
            playerToMining.put(playerId, new Mining(currentTick, snapshot));
        }
    }

    @AllArgsConstructor
    @ToString
    private static class Mining {
        private final int tickStarted;
        private BlockSnapshot snapshot;
    }
}

@@ -1412,16 +1412,43 @@
 ayerId);
+%0A        int durationTicks;
 %0A%0A      
@@ -1513,36 +1513,32 @@
 ) %7B%0A            
-int 
 durationTicks = 
@@ -1571,24 +1571,186 @@
 tarted;%0A    
+    %7D else %7B%0A            mining = new Mining(currentTick, snapshot);%0A            durationTicks = 0;%0A%0A            playerToMining.put(playerId, mining);%0A        %7D%0A%0A
         Mini
@@ -1859,20 +1859,16 @@
         
-    
 Cause.so
@@ -1948,28 +1948,24 @@
 );%0A%0A        
-   
- 
 Sponge.getEv
@@ -2005,20 +2005,16 @@
         
-    
 if (mini
@@ -2034,28 +2034,24 @@
 celled()) %7B%0A
-    
             
@@ -2087,116 +2087,8 @@
 d);%0A
-            %7D%0A        %7D else %7B%0A            playerToMining.put(playerId, new Mining(currentTick, snapshot));%0A
     

call the mining event the first time a block is clicked