/*
 * This file is part of UltimateCore, licensed under the MIT License (MIT).
 *
 * Copyright (c) Bammerbom
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package bammerbom.ultimatecore.sponge.modules.warp.api;

import bammerbom.ultimatecore.sponge.api.config.defaultconfigs.datafiles.WorldDataFile;
import bammerbom.ultimatecore.sponge.api.data.Key;
import bammerbom.ultimatecore.sponge.api.data.providers.KeyProvider;
import bammerbom.ultimatecore.sponge.api.error.utils.ErrorLogger;
import bammerbom.ultimatecore.sponge.api.language.utils.Messages;
import com.google.common.reflect.TypeToken;
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import org.spongepowered.api.Game;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.world.World;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

public class WarpKeys {
    public static Key.Global<List<Warp>> WARPS = new Key.Global<>("warps", new KeyProvider.Global<List<Warp>>() {
        @Override
        public List<Warp> load(Game arg) {
            List<Warp> warps = new ArrayList<>();
            for (World world : Sponge.getServer().getWorlds()) {
                WorldDataFile loader = new WorldDataFile(world.getUniqueId());
                CommentedConfigurationNode node = loader.get();
                for (CommentedConfigurationNode wnode : node.getNode("warps").getChildrenMap().values()) {
                    try {
                        warps.add(wnode.getValue(TypeToken.of(Warp.class)));
                    } catch (ObjectMappingException e) {
                        Messages.log(Messages.getFormatted("warp.command.warp.invalidwarp", "%warp%", wnode.getNode("name").getString()));
                    }
                }
            }
            return warps;
        }

        @Override
        public void save(Game arg, List<Warp> data) {
            //Sort warps by world
            HashMap<UUID, List<Warp>> worldWarps = new HashMap<>();
            for (Warp warp : data) {
                List<Warp> warps = worldWarps.containsKey(warp.getLocation().getExtent().getUniqueId()) ? worldWarps.get(warp.getLocation().getExtent().getUniqueId()) : new ArrayList<>();
                warps.add(warp);
                worldWarps.put(warp.getLocation().getExtent().getUniqueId(), warps);
            }

            for (UUID uuid : worldWarps.keySet()) {
                WorldDataFile loader = new WorldDataFile(uuid);
                CommentedConfigurationNode node = loader.get();
                node.getNode("warps").setValue(null);
                List<Warp> warps = worldWarps.get(uuid);
                for (Warp warp : warps) {
                    try {
                        node.getNode("warps", warp.getName()).setValue(TypeToken.of(Warp.class), warp);
                    } catch (ObjectMappingException e) {
                        ErrorLogger.log(e, "Failed to load warp. (Invalid world?)");
                    }
                }
                loader.save(node);
            }

        }
    });
}

@@ -3457,33 +3457,32 @@
 ;%0A            %7D%0A
-%0A
             for 
@@ -3486,37 +3486,50 @@
 or (
-UUID uuid : worldWarps.keySet
+World world : Sponge.getServer().getWorlds
 ()) 
@@ -3587,20 +3587,35 @@
 ataFile(
-uuid
+world.getUniqueId()
 );%0A     
@@ -3777,16 +3777,97 @@
 rps.
-get(uuid
+containsKey(world.getUniqueId()) ? worldWarps.get(world.getUniqueId()) : new ArrayList%3C%3E(
 );%0A 
@@ -4260,33 +4260,32 @@
 ;%0A            %7D%0A
-%0A
         %7D%0A    %7D)

Fix warp saving