import java.io.*;
import java.net.*;
import java.util.List;
import java.util.stream.Collectors;

import org.bukkit.Bukkit;
import org.bukkit.Statistic;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

public class MCbot extends JavaPlugin {

    @Override
    public void onEnable() {
        getLogger().info("MCbot enabled");
        // Start your socket server here
        startSocketServer();
    }

    @Override
    public void onDisable() {
        getLogger().info("MCbot disabled");
        // Stop your socket server here if needed
    }

     public void startSocketServer() {
        try {
            ServerSocket serverSocket = new ServerSocket(8888); // Port for socket communication

            while (true) {
                Socket clientSocket = serverSocket.accept();
                BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);

                String command = in.readLine();

                // Process the command
                String response = processCommand(command);

                // Send the response back to the client
                out.println(response);

                clientSocket.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    private String processCommand(String command) {
        // Implement logic to process commands here
        // Example: If command is "!players", return player list from Minecraft server
        if (command.equals("!players")) {
            // Use Spigot API to get player list
            List<Player> onlinePlayers = Bukkit.getServer().getOnlinePlayers().stream().collect(Collectors.toList()));
            
            if (onlinePlayers.isEmpty()) {
                return "No players are currently online.";
            } else {
                StringBuilder playerList = new StringBuilder("Players online: ");
                for (Player player : onlinePlayers) {
                    playerList.append(player.getName()).append(", ");
                }
                // Remove the trailing comma and space
                playerList.setLength(playerList.length() - 2);
                return playerList.toString();
            }
        }
        if (command.startsWith("!exp ")) {
        // Extract player name from command
        String playerName = command.substring(5); // Assuming "!exp " is 5 characters long
        
            // Get player object by name
            Player player = Bukkit.getPlayer(playerName);
            if (player != null) {
                // Get player's experience level
                int expLevel = player.getLevel();
                return playerName + "'s experience level: " + expLevel;
            } else {
                return "Player not found: " + playerName;
            }
        }
        if (command.startsWith("!deaths ")) {
            //Extract player name from command
            String playerName = command.substring(8); // Assuming "!deaths " is at least 8 chars long
            
            // Get Player object by name
            Player player = Bukkit.getPlayer(playerName);
            if(player != null) {
                // Get Player's Death Count
                int deaths = player.getStatistic(Statistic.DEATHS);
                return playerName + "has died" + deaths + "times";
            }else {
                return "Player not found: " + playerName;
            }
        }
           
        // Add more command processing logic as needed
        return "Unknown command: " + command;
    }
}
