package com.yaohun.wheatwar;

import com.yaohun.wheatwar.data.RepairGiftGui;
import com.yaohun.wheatwar.effect.*;
import com.yaohun.wheatwar.game.Game;
import com.yaohun.wheatwar.listener.GameEffectListener;
import com.yaohun.wheatwar.listener.GameListener;
import com.yaohun.wheatwar.listener.GamePotect;
import com.yaohun.wheatwar.listener.JoinListener;
import com.yaohun.wheatwar.liveevent.GiftEventHandler;
import com.yaohun.wheatwar.liveevent.GiftListener;
import com.yaohun.wheatwar.liveevent.LikeListener;
import com.yaohun.wheatwar.manage.GameManager;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.entity.Villager;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import tools.GameUtil;
import tools.RandomUtil;

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

public class WheatWar extends JavaPlugin {

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

    @Override
    public void onEnable() {
        instance = this;
        gameManager = new GameManager();
        game = new Game();
        getServer().getPluginManager().registerEvents(new GiftListener(),this);
        getServer().getPluginManager().registerEvents(new LikeListener(gameManager),this);

        getServer().getPluginManager().registerEvents(new RepairGiftGui(),this);

        getServer().getPluginManager().registerEvents(new GameEffectListener(),this);
        getServer().getPluginManager().registerEvents(new GameListener(),this);
        getServer().getPluginManager().registerEvents(new GamePotect(),this);
        getServer().getPluginManager().registerEvents(new JoinListener(),this);
    }

    @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 - 开始游戏");
                return true;
            }
            if(args.length == 1 && args[0].equalsIgnoreCase("test1")){
                SummonTheVillager.apply(getGame(), (Player) sender, "村民", 50);
                sender.sendMessage("执行成功.");
            }
            if(args.length == 1 && args[0].equalsIgnoreCase("test2")){
                SummonTheSheep.apply(getGame(), (Player) sender, "路人", 50);
                sender.sendMessage("执行成功.");
            }
            if(args.length == 1 && args[0].equalsIgnoreCase("test3")){
                SummonTheZombie.apply(getGame(), (Player) sender, "僵尸先生", 10);
                sender.sendMessage("执行成功.");
            }
            if(args.length == 1 && args[0].equalsIgnoreCase("test4")){
                SummonTheBombsTNT.apply(getGame(), (Player) sender, "僵尸先生", 10);
                sender.sendMessage("执行成功.");
            }
            if(args.length == 1 && args[0].equalsIgnoreCase("test5")){
                SummonTheTNTRain.apply(getGame(), (Player) sender, "僵尸先生", 50);
                sender.sendMessage("执行成功.");
            }
            if(args.length == 1 && args[0].equalsIgnoreCase("test6")){
                SummonTheSheepPro.apply(getGame(), (Player) sender, "暴躁小杨", 10);
                sender.sendMessage("执行成功.");
            }
            if(args.length == 1 && args[0].equalsIgnoreCase("test7")){
                Player player = (Player) sender;
                Location location = player.getLocation();
                Villager entity = (Villager) location.getWorld().spawnEntity(location, EntityType.VILLAGER);
                entity.setAI(false);
                entity.setProfession(Villager.Profession.FARMER);
                sender.sendMessage("执行成功.");
            }
            if(args.length == 1 && args[0].equalsIgnoreCase("play")){
                Player player = (Player) sender;
                Location center = new Location(Bukkit.getWorld("world"), 1, 66, -2);
                // 启动计时器
                new BukkitRunnable() {
                    private int ticks = 0; // 计时器的 ticks 变量，1秒20 ticks
                    private final double radius = 30.0; // 绕圈半径
                    private final int duration = 36000 * 10; // 1分钟（1200 ticks）
                    @Override
                    public void run() {
                        if(player == null || !player.isOnline()){
                            cancel();
                            return;
                        }
                        if (ticks >= duration) { // 计时结束
                            cancel();
                            return;
                        }
                        // 计算当前旋转角度
                        double angle = Math.toRadians((ticks * 0.3) % 360); // 每tick增加角度，控制旋转速度
                        double x = center.getX() + radius * Math.cos(angle);
                        double z = center.getZ() + radius * Math.sin(angle);
                        double y = center.getY() + 13;
                        // 更新玩家位置和朝向
                        Location playerLoc = player.getLocation();
                        playerLoc.setX(x);
                        playerLoc.setY(y);
                        playerLoc.setZ(z);

                        // 设置玩家面朝中心点
                        playerLoc.setDirection(center.toVector().subtract(playerLoc.toVector()));
                        player.teleport(playerLoc);
                        ticks++;
                    }
                }.runTaskTimer(instance, 0L, 1L); // 每tick执行一次
                sender.sendMessage("执行成功.");
                return true;
            }
            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 WheatWar inst(){return instance;}
    public static Game getGame() {return game;}
    public static GameManager getGameManager() {return gameManager;}
}
