package com.yutian.minerwar;

import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.math.BlockVector3;
import com.yutian.minerwar.data.RepairGiftGui;
import com.yutian.minerwar.game.Game;
import com.yutian.minerwar.game.Point;
import com.yutian.minerwar.game.Region;
import com.yutian.minerwar.listener.*;
import com.yutian.minerwar.liveevent.GiftEventHandler;
import com.yutian.minerwar.liveevent.GiftListener;
import com.yutian.minerwar.liveevent.KeyInputListener;
import com.yutian.minerwar.liveevent.LikeListener;
import com.yutian.minerwar.manager.GameManager;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import tools.GameUtil;
import tools.RandomUtil;

import java.util.ArrayList;
import java.util.List;

public class MinerWar extends JavaPlugin {

    private static MinerWar instance;
    private static Game game;
    private static GameManager gameManager;

    @Override
    public void onEnable() {
        instance = this;
        saveDefaultConfig();
        gameManager = new GameManager();
        game = new Game(gameManager);
        Bukkit.getPluginManager().registerEvents(new GiftListener(), this);
        Bukkit.getPluginManager().registerEvents(new LikeListener(), this);
        Bukkit.getPluginManager().registerEvents(new JoinListener(), this);
        Bukkit.getPluginManager().registerEvents(new GamePotect(), this);
        Bukkit.getPluginManager().registerEvents(new PlayerListener(), this);
        Bukkit.getPluginManager().registerEvents(new GameListener(), this);
        Bukkit.getPluginManager().registerEvents(new GameEffect(), this);
        Bukkit.getPluginManager().registerEvents(new KeepEntitiesOnDeath(), this);
        Bukkit.getPluginManager().registerEvents(new FoodAndMedicineListener(), this);
        Bukkit.getPluginManager().registerEvents(new KeyInputListener(), this);
        Bukkit.getPluginManager().registerEvents(new RepairGiftGui(), this);
    }

    @Override
    public void onDisable() {
        gameManager.saveGameSettings();
        game.stop();
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String Command, String[] args) {
        if (Command.equalsIgnoreCase("livegift")) {
            // RepairGiftGui.OpenGui((Player) sender);
            return true;
        }
        if (Command.equalsIgnoreCase("gameedit")) {
            // GameEditGui.OpenGui((Player) sender);
            return true;
        }
        if (Command.equalsIgnoreCase("game")) {
            if (args.length == 0) {
                sender.sendMessage("/game start - 开始游戏");
                sender.sendMessage("/game keyd - 按键调试");
                return true;
            }
            if(args.length == 1 && args[0].equalsIgnoreCase("keyd")){
                if(gameManager.isKeyInputd()){
                    gameManager.setKeyInputd(false);
                    sender.sendMessage("按键调试: 已关闭");
                }else {
                    gameManager.setKeyInputd(true);
                    sender.sendMessage("按键调试: 已开启");
                }
                return true;
            }
            if (args[0].equalsIgnoreCase("region")) {
                String regionKey = args[1];
                Player player = (Player) sender;
                Actor actor = BukkitAdapter.adapt(player);
                LocalSession localSession = actor.getSession();
                com.sk89q.worldedit.regions.Region region = localSession.getSelection(BukkitAdapter.adapt(player.getWorld()));
                if (region == null) {
                    player.sendMessage("请先选中一个区域");
                    return true;
                }
                BlockVector3 min = region.getMinimumPoint();
                BlockVector3 max = region.getMaximumPoint();
                Point minPoint = new Point(min.getBlockX(), min.getBlockY(), min.getBlockZ());
                Point maxPoint = new Point(max.getBlockX(), max.getBlockY(), max.getBlockZ());
                Region region1 = new Region(player.getWorld(), minPoint, maxPoint);
                game.createRegion(regionKey, region1);
                player.sendMessage("区域创建成功.");
            }
            if (args.length == 2 && args[0].equalsIgnoreCase("effect")) {
                List<Player> players = new ArrayList<>(Bukkit.getOnlinePlayers());
                Player player = players.get(0);
                if (sender instanceof Player) {
                    player = (Player) sender;
                }
                String eventName = args[1];
                String userName = "观众" + RandomUtil.getRandomInt(0, 10);
                String title = "§c" + eventName;
                String subtitle = "§9" + GameUtil.hideName(userName);
                player.sendTitle(title, subtitle, 10, 30, 10);
                GiftEventHandler.SendHandLer(player, userName, eventName);
                return true;
            }
        }
        return false;
    }

    public static Plugin inst() {return instance;}

    public static Game getGame() {return game;}

    public static GameManager getGameManager() {return gameManager;}
}
