package net.senmori.launchyou.launch.types;

import static sun.audio.AudioPlayer.player;
import net.senmori.launchyou.LaunchYou;
import net.senmori.launchyou.launch.Transport;
import net.senmori.launchyou.task.LaunchTask;
import net.senmori.launchyou.task.TeleportTask;
import net.senmori.launchyou.util.ActionBar;
import net.senmori.launchyou.util.LocaleMessage;
import net.senmori.launchyou.util.LogHandler;
import net.senmori.launchyou.util.Reference;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;

/**
 * Represents a transport that teleports players to their destination
 */
public class Teleport extends Transport {
    private LaunchYou plugin = LaunchYou.getInstance();
    
    
    @Override
    public void activate(Player player, Sign sign) {
        if(player.hasPermission(Reference.Permissions.CANNON_USE)) {
            if(LocaleMessage.canShowMessage("teleport.use") && LocaleMessage.doShowTeleportMessages()) {
                ActionBar.sendMessage(player, ChatColor.GREEN + LocaleMessage.get("teleport.use"));
            }
        } else {
            player.sendMessage(ChatColor.RED + LocaleMessage.get("cannon.nouse"));
            return;
        }
        
        String[] lines = sign.getLines();
        String targetLoc = lines[1];
        String world = lines[3];
        boolean useWhitelist = plugin.getConfig().getBoolean("settings.use-as-whitelist", false);
        int signDepth = plugin.getConfig().getInt("settings.depth", 1);
    
        String matName = plugin.getConfig().getString("transports.teleport.deactivate", "air");
    
        Material mat = Bukkit.getUnsafe().getMaterialFromInternalName(matName);
        if(canActivate(player, useWhitelist, signDepth, mat)) {
            Location end = null;
            if(targetLoc.contains(",")) { // it's a list of numbers
                // 0 = x, last = z, y = 'transports.cannon.height'
                String[] split = targetLoc.split(",");
                int x = Integer.valueOf(ChatColor.stripColor(split[0]));
                int y = Integer.valueOf(ChatColor.stripColor(split[1]));
                int z = Integer.valueOf(ChatColor.stripColor(split[split.length]));
                
                end = new Location(Bukkit.getWorld(world), x, y, z);
            } else {
                end = plugin.getTargetManager().getTargets().get(ChatColor.stripColor(targetLoc)).getLocation().getLocation();
            }
        
            if(end == null) {
                // this shouldn't happen...hopefully
                LogHandler.warn("Error assigning a target location '" + targetLoc + "'");
                player.sendMessage(ChatColor.RED + "Error assigning a target location '" + ChatColor.WHITE + targetLoc + ChatColor.RED + "'");
                return;
            }
        
            // start launch
            new TeleportTask(player, player.getLocation(), end);
        }
    }
    
    @Override
    public boolean onCreate(Player player) {
        if(!plugin.getConfig().getBoolean("transports.teleport.enable")) {
            Bukkit.broadcastMessage("TELEPORT: " + plugin.getConfig().getBoolean("transports.teleport.enable"));
            player.sendMessage(ChatColor.RED + " [Teleport] signs are disabled. Sorry!");
            return false;
        }
        
        if(!player.hasPermission(Reference.Permissions.TELEPORT_CREATE)) {
            player.sendMessage(ChatColor.RED + LocaleMessage.get("teleport.nopermission"));
            return false;
        }
        return true;
    }
    
    @Override
    public boolean onDestroy(Player player) {
        if(!player.hasPermission(Reference.Permissions.TELEPORT_DESTROY)) {
            player.sendMessage(ChatColor.RED + LocaleMessage.get("teleport.nopermission").replace("create", "destroy"));
            return false;
        }
        player.sendMessage(ChatColor.GREEN + LocaleMessage.get("teleport.success").replace("creation", "deletion"));
        return true;
    }
    
    private boolean canActivate(Player player, boolean whitelist, int signDepth, Material material) {
        if(whitelist) {
            return player.getLocation().getBlock().getRelative(0, - 1- signDepth, 0).getType() == material;
        }
        return player.getLocation().getBlock().getRelative(0, -1 -signDepth, 0).getType() != material;
    }
}
