/* 
    This file is a part of the SteamWar software.
    
    Copyright (C) 2020  SteamWar.de-Serverteam

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

package de.steamwar.fightsystem.listener;

import de.steamwar.core.TrickyTrialsWrapper;
import de.steamwar.fightsystem.ArenaMode;
import de.steamwar.fightsystem.Config;
import de.steamwar.fightsystem.fight.Fight;
import de.steamwar.fightsystem.fight.FightTeam;
import de.steamwar.fightsystem.states.FightState;
import de.steamwar.fightsystem.states.StateDependentListener;
import de.steamwar.fightsystem.utils.FlatteningWrapper;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.EntityType;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.entity.EntitySpawnEvent;

import java.util.HashMap;
import java.util.Map;

public class WaterRemover implements Listener {

    private static final int MIN_Y = Config.BluePasteRegion.getMinY() + Config.WaterDepth;

    private final Map<Integer, FightTeam> tnt = new HashMap<>();

    public WaterRemover() {
        new StateDependentListener(ArenaMode.AntiReplay, FightState.Running, this){
            @Override
            public void enable() {
                tnt.clear();
                super.enable();
            }
        };
    }

    @EventHandler
    public void handleEntitySpawn(EntitySpawnEvent event) {
        if(event.getEntityType() != TrickyTrialsWrapper.impl.getTntEntityType())
            return;

        Location location = event.getLocation();
        Fight.teams().forEach(team -> {
            if(team.getExtendRegion().inRegion(location))
                tnt.put(event.getEntity().getEntityId(), team);
        });
    }

    @EventHandler
    public void handleEntityExplode(EntityExplodeEvent event) {
        event.setYield(0); //No drops (additionally to world config)

        FightTeam spawn = tnt.remove(event.getEntity().getEntityId());
        if(spawn != null && !spawn.getExtendRegion().inRegion(event.getLocation())) {
            Block b = event.getLocation().getBlock();
            for(int y = -1; y <= 1; y++) {
                for(int z = -1; z <= 1; z++) {
                    for(int x = -1; x <= 1; x++) {
                        checkBlock(b.getRelative(x, y, z));
                    }
                }
            }
        }

        event.blockList().forEach(this::checkNeighbours);
    }

    private void checkNeighbours(Block b) {
        checkBlock(b.getRelative(BlockFace.UP));
        checkBlock(b.getRelative(BlockFace.EAST));
        checkBlock(b.getRelative(BlockFace.WEST));
        checkBlock(b.getRelative(BlockFace.NORTH));
        checkBlock(b.getRelative(BlockFace.SOUTH));
        checkBlock(b.getRelative(BlockFace.DOWN));
    }

    private void checkBlock(Block b) {
        //do not remove outside teamareas
        if(!Config.BlueExtendRegion.inRegion(b) && !Config.RedExtendRegion.inRegion(b))
            return;

        //checks for water and removes it, if present
        if(!FlatteningWrapper.impl.removeWater(b))
            return;

        if(b.getY() < MIN_Y)
            return;

        checkNeighbours(b);
    }
}
