/**
 * This file is part of Prism, licensed under the MIT License (MIT).
 *
 * Copyright (c) 2015 Helion3 http://helion3.com/
 *
 * 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 com.helion3.prism.util;

import java.util.Optional;

import org.spongepowered.api.data.DataView;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.Text.Builder;
import org.spongepowered.api.text.format.TextColors;

import com.helion3.prism.api.results.ResultRecord;
import com.helion3.prism.api.results.ResultRecordAggregate;
import com.helion3.prism.api.results.ResultRecordComplete;

public class Messages {
    private Messages() {}

    /**
     * Generates Text output from a ResultRecord.
     *
     * @param result ResultRecord
     * @return Text
     */
    public static Text from(ResultRecord result) {
        Builder builder = Text.builder().append(Text.of(
            TextColors.DARK_AQUA, result.getSourceName(), " ",
            TextColors.WHITE, result.getEventVerb(), " "
        ));

        String target = result.getTargetName();
        if (!target.isEmpty()) {
            builder.append(Text.of(TextColors.DARK_AQUA, target, " "));
        }

        if (result instanceof ResultRecordAggregate) {
            int count = result.data.getInt(DataQueries.Count).get();
            builder.append(Text.of(TextColors.GREEN, "x" + count, " "));
        } else {
            ResultRecordComplete recordComplete = (ResultRecordComplete) result;
            Optional<Object> optionalLocation = result.data.get(DataQueries.Location);

            if (optionalLocation.isPresent()) {
                DataView loc = (DataView) optionalLocation.get();
                builder.append(Text.of(TextColors.GRAY, "@ ", loc.getInt(DataQueries.X).get(), " ",
                        loc.getInt(DataQueries.Y).get(), " ",
                        loc.getInt(DataQueries.Z).get(), " "));
            }

            builder.append(Text.of(TextColors.WHITE, recordComplete.getRelativeTime()));
        }

        return builder.build();
    }
}

@@ -1262,194 +1262,581 @@
 al;%0A
-%0Aimport org.spongepowered.api.data.DataView;%0Aimport org.spongepowered.api.text.Text;%0Aimport org.spongepowered.api.text.Text.Builder;%0Aimport org.spongepowered.api.text.format.TextColors;%0A
+import java.util.UUID;%0Aimport java.util.function.Consumer;%0A%0Aimport org.spongepowered.api.command.CommandSource;%0Aimport org.spongepowered.api.data.DataView;%0Aimport org.spongepowered.api.entity.living.player.Player;%0Aimport org.spongepowered.api.text.Text;%0Aimport org.spongepowered.api.text.Text.Builder;%0Aimport org.spongepowered.api.text.action.TextActions;%0Aimport org.spongepowered.api.text.format.TextColors;%0Aimport org.spongepowered.api.world.Location;%0Aimport org.spongepowered.api.world.World;%0A%0Aimport com.flowpowered.math.vector.Vector3i;%0Aimport com.helion3.prism.Prism;
 %0Aimp
@@ -3087,79 +3087,426 @@
 ();%0A
-                builder.append(Text.of(TextColors.GRAY, %22@ %22, loc.getIn
+%0A                int x = loc.getInt(DataQueries.X).get();%0A                int y = loc.getInt(DataQueries.Y).get();%0A                int z = loc.getInt(DataQueries.Z).get();%0A%0A                builder.append(Text.of(TextColors.GRAY, %22@ %22, x, %22 %22, y, %22 %22, z, %22 %22));%0A%0A                // Allow teleportation on click%0A                Optional%3CWorld%3E world = Prism.getGame().getServer().getWorld(UUID.fromString((String) loc.ge
 t(Da
@@ -3519,22 +3519,27 @@
 ies.
-X
+WorldUuid
 ).get()
-, %22 %22,
+));
 %0A   
@@ -3555,117 +3555,548 @@
     
-        loc.getInt(DataQueries.Y).get(), %22 %22,%0A                        loc.getInt(DataQueries.Z).get(), %22 %22));
+if (world.isPresent()) %7B%0A                    Location%3CWorld%3E teleportLoc = world.get().getLocation(new Vector3i(x, y, z));%0A%0A                    builder.onClick(TextActions.executeCallback(new Consumer%3CCommandSource%3E() %7B%0A                        @Override%0A                        public void accept(CommandSource t) %7B%0A                            if (t instanceof Player) %7B%0A                                ((Player) t).setLocation(teleportLoc);%0A                            %7D%0A                        %7D%0A                    %7D));%0A                %7D
 %0A   

Allow teleport on click of complete records