/*
 * 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.bausystem.region;

import com.comphenix.tinyprotocol.TinyProtocol;
import de.steamwar.Reflection;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.region.dynamic.DynamicRegion;
import de.steamwar.bausystem.region.dynamic.MovementListener;
import de.steamwar.bausystem.region.dynamic.RegionDataRepository;
import de.steamwar.bausystem.region.dynamic.global.GlobalRegion;
import de.steamwar.bausystem.region.dynamic.path.PathRegion;
import de.steamwar.bausystem.region.dynamic.spawn.SpawnPathRegion;
import de.steamwar.bausystem.region.dynamic.spawn.SpawnRegion;
import de.steamwar.bausystem.region.dynamic.spawn.SpawnResetter;
import de.steamwar.core.Core;
import de.steamwar.providers.BauServerInfo;
import lombok.NonNull;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import sun.misc.Unsafe;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.RecordComponent;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class DynamicRegionSystem implements RegionSystem {

    public static DynamicRegionSystem INSTANCE;
    private static final World WORLD = Bukkit.getWorlds().get(0);

    private static Map<UUID, Region> regionMap = new HashMap<>();

    private Class<?> loginPacket = Reflection.getClass("net.minecraft.network.protocol.game.ClientboundLoginPacket");
    private Class<?> commonPlayerSpawnInfo = Reflection.getClass("net.minecraft.network.protocol.game.CommonPlayerSpawnInfo");
    private Reflection.Constructor loginPacketConstructor = Reflection.getConstructor(loginPacket, int.class, boolean.class, Set.class, int.class, int.class, int.class, boolean.class, boolean.class, boolean.class, commonPlayerSpawnInfo, boolean.class);
    private Class<?> holderClass = Reflection.getClass("net.minecraft.core.Holder");
    private Class<?> resourceKeyClass = Reflection.getClass("net.minecraft.resources.ResourceKey");
    private Class<?> gameTypeClass = Reflection.getClass("net.minecraft.world.level.GameType");
    private Reflection.Constructor commonPlayerSpawnInfoConstructor = Reflection.getConstructor(commonPlayerSpawnInfo, holderClass, resourceKeyClass, long.class, gameTypeClass, gameTypeClass, boolean.class, boolean.class, Optional.class, int.class, int.class);
    private Class<?> minecraftKeyClass = Reflection.getClass("net.minecraft.resources.MinecraftKey");
    private Reflection.Constructor resourceKeyConstructor = Reflection.getConstructor(resourceKeyClass, minecraftKeyClass, minecraftKeyClass);
    private Reflection.Constructor minecraftKeyConstructor = Reflection.getConstructor(minecraftKeyClass, String.class, String.class);

    @Override
    public void load() {
        INSTANCE = this;
        // TODO: Extract into own PR
        String identifier = BauServerInfo.getOwnerUser().getUUID().toString().replace("-", "");
        Object resourceKey = resourceKeyConstructor.invoke(minecraftKeyConstructor.invoke("minecraft", "dimension"), minecraftKeyConstructor.invoke("steamwar", "bau/" + Core.getVersion() + "/" + identifier));
        TinyProtocol.instance.addFilter(loginPacket, (player, o) -> {
            RecordComponent[] components = loginPacket.getRecordComponents();
            Object[] parameters_loginPacket = new Object[components.length];
            for (int i = 0; i < components.length; i++) {
                try {
                    parameters_loginPacket[i] = components[i].getAccessor().invoke(o);
                } catch (IllegalAccessException | InvocationTargetException e) {
                    throw new RuntimeException(e);
                }
            }
            parameters_loginPacket[2] = Set.of(resourceKey);

            components = commonPlayerSpawnInfo.getRecordComponents();
            Object[] parameters_commonPlayerSpawnInfo = new Object[components.length];
            for (int i = 0; i < components.length; i++) {
                try {
                    parameters_commonPlayerSpawnInfo[i] = components[i].getAccessor().invoke(parameters_loginPacket[9]);
                } catch (IllegalAccessException | InvocationTargetException e) {
                    throw new RuntimeException(e);
                }
            }
            parameters_commonPlayerSpawnInfo[1] = resourceKey;
            parameters_loginPacket[9] = commonPlayerSpawnInfoConstructor.invoke(parameters_commonPlayerSpawnInfo);

            return loginPacketConstructor.invoke(parameters_loginPacket);
        });
        new DynamicRegionCommand();
        RegionDataRepository.loadRegions();
        Bukkit.getPluginManager().registerEvents(new MovementListener(), BauSystem.getInstance());

        if (regionMap.isEmpty()) { // TODO: Implement this in default region!
            new SpawnRegion(-9, -9);
            new SpawnPathRegion(-9, -28);
            new SpawnPathRegion(-9, 10);
            new SpawnPathRegion(-28, -9);
            new SpawnPathRegion(10, -9);

            new PathRegion(-28, -28);
            new PathRegion(-28, 10);
            new PathRegion(10, -28);
            new PathRegion(10, 10);
        }
    }

    public void add(DynamicRegion region) {
        regionMap.put(region.getID(), region);
    }

    public void delete(DynamicRegion region) {
        regionMap.remove(region.getID());
    }

    @Override
    public void save() {
    }

    @Override
    public @NonNull Location getWorldSpawn() {
        if (SpawnResetter.isBigSpawn()) {
            return SpawnResetter.BIG_WORLD_SPAWN;
        } else {
            return SpawnResetter.SMALL_WORLD_SPAWN;
        }
    }

    @Override
    public @NonNull Region getGlobalRegion() {
        return GlobalRegion.INSTANCE;
    }

    private Region get(Location location, Collection<Region> regions) {
        return regions.stream()
                .filter(region -> region.getArea().inRegion(location, false))
                .findFirst()
                .orElse(GlobalRegion.INSTANCE);
    }

    @Override
    public @NonNull Region get(@NonNull Location location) {
        return get(location, regionMap.values());
    }

    @Override
    public Optional<Region> getRegion(@NonNull UUID id) {
        return Optional.ofNullable(regionMap.get(id));
    }

    @Override
    public @NonNull Stream<Region> getRegions() {
        return regionMap.values().stream();
    }

    private Stream<DynamicRegion> getNeighbours(Region region, boolean noCorners, Collection<Region> regions) {
        Point minPoint = region.getArea().getMinPoint(false).subtract(18, 0, 18);
        Point maxPoint = region.getArea().getMaxPoint(false).add(19, 0, 19);
        Set<Region> neighbours = new HashSet<>();
        for (int x = minPoint.getX() + (noCorners ? 18 : 0); x <= maxPoint.getX() - (noCorners ? 19 : 0); x += 19) {
            int minZ = minPoint.getZ();
            int maxZ = maxPoint.getZ();
            neighbours.add(get(new Location(WORLD, x, 0, minZ), regions));
            neighbours.add(get(new Location(WORLD, x, 0, maxZ), regions));
        }
        for (int z = minPoint.getZ() + 18; z <= maxPoint.getZ() - 19; z += 19) {
            int minX = minPoint.getX();
            int maxX = maxPoint.getX();
            neighbours.add(get(new Location(WORLD, minX, 0, z), regions));
            neighbours.add(get(new Location(WORLD, maxX, 0, z), regions));
        }
        neighbours.remove(GlobalRegion.INSTANCE);
        return ((Set<DynamicRegion>) (Set) neighbours).stream();
    }

    public Stream<DynamicRegion> getNeighbours(Region region) {
        return getNeighbours(region, false, regionMap.values());
    }

    public Stream<DynamicRegion> getConnectedRegions(DynamicRegion region) {
        Set<Region> regions = regionMap.values()
                .stream()
                .filter(r -> r.getType() == region.getType())
                .collect(Collectors.toSet());

        Set<DynamicRegion> connectedRegions = new HashSet<>();
        LinkedHashSet<DynamicRegion> current = new LinkedHashSet<>();
        current.add(region);
        while (!current.isEmpty()) {
            DynamicRegion r = current.removeFirst();
            if (!connectedRegions.add(r)) continue;
            getNeighbours(r, true, regions).forEach(current::add);
        }
        return connectedRegions.stream();
    }
}
