package com.zivilon.cinder_loe.command;

import com.zivilon.cinder_loe.CinderLoE_Config;
import com.zivilon.cinder_loe.world.event.Warband;
import com.zivilon.cinder_loe.world.event.WarbandFaction;

import net.minecraft.command.CommandBase;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.ChatComponentText;

import lotr.common.world.map.LOTRWaypoint;

import com.zivilon.cinder_loe.character.CharacterRoleAPI;
import com.zivilon.cinder_loe.util.Utilities;

import java.util.UUID;

public class CommandWarband extends CommandBase {
    @Override
    public String getCommandName() {
        return "warband";
    }

    @Override
    public String getCommandUsage(ICommandSender sender) {
        return "/warband <summon/reset/list/on/off> [faction_name] [waypoint] [x] [z]";
    }

    @Override
    public int getRequiredPermissionLevel() {
        return 4;
    }

    @Override
    public void processCommand(ICommandSender sender, String[] args) {
        if(!validate_args(args)) {
            sender.addChatMessage(new ChatComponentText("Incorrect arguments. Usage: " + getCommandUsage(sender)));
            return;
        }

        String action = args[0];
        System.out.println("Checking arg " + action);
        switch (action) {
            case "reset":
                reset_warband();
                sender.addChatMessage(new ChatComponentText("Warband timer has been reset. A new warband may now spawn."));
                return;
            case "list":
                list_warbands(sender);
                return;
            case "summon":
                summon_warband(sender, args);
                return;
            case "on":
                CinderLoE_Config.warbands_enabled = true;
                sender.addChatMessage(new ChatComponentText("Warbands have been enabled!"));
                return;
            case "off":
                CinderLoE_Config.warbands_enabled = true;
                sender.addChatMessage(new ChatComponentText("Warbands have been disabled!"));
                return;
        }
    }

    public void summon_warband(ICommandSender sender, String[] args) {
        WarbandFaction faction = WarbandFaction.get_warband_by_name(args[1]);
        LOTRWaypoint waypoint = null;
        String waypoint_name = null;
        int x = 0;
        int z = 0;

        if (args.length == 2) {
            if (!(sender instanceof EntityPlayer)) {
                System.out.println("Console must specify location to summon warband. Options:");
                System.out.println("/warband <summon> <faction_name> <waypoint>");
                System.out.println("/warband <summon> <faction_name> <x> <z>");
                return;
            }
            EntityPlayer player = (EntityPlayer)sender;
            x = (int)player.posX;
            z = (int)player.posZ;
            Warband.initialize_warband(faction, x, z);
        }

        if (args.length == 3) {
            waypoint_name = args[2];
            waypoint = LOTRWaypoint.waypointForName(waypoint_name);
            if (waypoint == null) {
                sender.addChatMessage(new ChatComponentText("Invalid waypoint \"" + waypoint_name + "\""));
                return;
            }
            Warband.initialize_warband(faction, waypoint);
            return;
        }

        if (args.length == 4) {
            try {
                x = Integer.parseInt(args[2]);
                z = Integer.parseInt(args[3]);
            } catch (Exception e) {
                sender.addChatMessage(new ChatComponentText("Invalid coordinates provided"));
                return;
            }
            Warband.initialize_warband(faction, x, z);
            return;
        }

        if (args.length > 4) {
            try {
                x = Integer.parseInt(args[3]);
                z = Integer.parseInt(args[4]);
            } catch (Exception e) {
                sender.addChatMessage(new ChatComponentText("Invalid coordinates provided"));
                return;
            }
            waypoint_name = args[2];
            waypoint = LOTRWaypoint.waypointForName(waypoint_name);
            if (waypoint == null) {
                sender.addChatMessage(new ChatComponentText("Invalid waypoint \"" + waypoint_name + "\""));
                return;
            }
            Warband.initialize_warband(faction, waypoint, x, z);
            return;
        }
    }

    public static boolean validate_args(String[] args) {
        if (args.length < 1) return false;
        String action = "";
        if (args[0].equals("summon") || args[0].equals("reset") || args[0].equals("list") || args[0].equals("on") || args[0].equals("off")) {
            action = args[0];
        } else {
            return false;
        }
        if (action.equals("summon")) {
            if (args.length < 2) {
                return false;
            }
            WarbandFaction faction = WarbandFaction.get_warband_by_name(args[1]);
            if (faction == null) return false;
        }
        return true;
    }

    public static void reset_warband() {
        // Set last warband to have happened 10 hours ago, thus allowing new warband
        Warband.last_warband_timestamp = System.currentTimeMillis() / 1000L - (60*60*10);
    }
    public static void list_warbands(ICommandSender sender) {
        sender.addChatMessage(new ChatComponentText("List of valid warbands:"));
        for (WarbandFaction faction : WarbandFaction.values()) {
            sender.addChatMessage(new ChatComponentText("- " + faction.name()));
        }
    }
}
