import { world, system } from "@minecraft/server";
import { playerSettings } from "./player_settings.js";
import { worldSettings } from "./world_settings.js";

// Settings Commands System similar to Anime Jutsu
export class SettingsCommandManager {
  constructor() {
    // Remove chat command system since it's experimental
    // Commands will be available through items or other means
    // Settings Commands System initialized (without chat commands)
  }

  // Keep all the methods for potential future use or alternative triggers
  
  // Player Settings Methods
  listPlayerSettings(player) {
    player.sendMessage("§6=== Soul Hunters Player Settings ===");
    Object.keys(playerSettings.defaultSettings).forEach(settingName => {
      const value = playerSettings.getPlayerSetting(player, settingName);
      const setting = playerSettings.defaultSettings[settingName];
      player.sendMessage(`§b${settingName}§f: §a${value} §7(${setting.type})`);
    });
  }

  getPlayerSetting(player, settingName) {
    if (!playerSettings.defaultSettings[settingName]) {
      player.sendMessage(`§cSetting '${settingName}' does not exist!`);
      return;
    }

    const value = playerSettings.getPlayerSetting(player, settingName);
    player.sendMessage(`§b${settingName}§f: §a${value}`);
  }

  setPlayerSetting(player, settingName, valueStr) {
    if (!playerSettings.defaultSettings[settingName]) {
      player.sendMessage(`§cSetting '${settingName}' does not exist!`);
      return;
    }

    const setting = playerSettings.defaultSettings[settingName];
    let value;

    // Parse value based on type
    switch (setting.type) {
      case "boolean":
        value = valueStr.toLowerCase() === "true" || valueStr.toLowerCase() === "on";
        break;
      case "number":
        value = parseFloat(valueStr);
        if (isNaN(value)) {
          player.sendMessage("§cInvalid number value!");
          return;
        }
        break;
      case "enum":
        if (!setting.values.includes(valueStr)) {
          player.sendMessage(`§cInvalid value! Valid options: ${setting.values.join(", ")}`);
          return;
        }
        value = valueStr;
        break;
      default:
        value = valueStr;
    }

    if (playerSettings.setPlayerSetting(player, settingName, value)) {
      player.sendMessage(`§aSet §b${settingName}§a to §f${value}`);
    } else {
      player.sendMessage("§cFailed to set setting!");
    }
  }

  togglePlayerSetting(player, settingName) {
    const currentValue = playerSettings.getPlayerSetting(player, settingName);
    const newValue = !currentValue;
    
    if (playerSettings.setPlayerSetting(player, settingName, newValue)) {
      player.sendMessage(`§aToggled §b${settingName}§a: §f${newValue}`);
    }
  }

  cyclePlayerSetting(player, settingName, values) {
    const currentValue = playerSettings.getPlayerSetting(player, settingName);
    const currentIndex = values.indexOf(currentValue);
    const nextIndex = (currentIndex + 1) % values.length;
    const newValue = values[nextIndex];
    
    if (playerSettings.setPlayerSetting(player, settingName, newValue)) {
      player.sendMessage(`§aSet §b${settingName}§a to §f${newValue}`);
    }
  }

  // World Settings Methods
  listWorldSettings(player) {
    player.sendMessage("§6=== Soul Hunters World Settings ===");
    Object.keys(worldSettings.defaultSettings).forEach(settingName => {
      const value = worldSettings.getWorldSetting(settingName);
      const setting = worldSettings.defaultSettings[settingName];
      player.sendMessage(`§b${settingName}§f: §a${value} §7(${setting.type})`);
    });
  }

  getWorldSetting(player, settingName) {
    if (!worldSettings.defaultSettings[settingName]) {
      player.sendMessage(`§cWorld setting '${settingName}' does not exist!`);
      return;
    }

    const value = worldSettings.getWorldSetting(settingName);
    player.sendMessage(`§b${settingName}§f: §a${value}`);
  }

  setWorldSetting(player, settingName, valueStr) {
    // Check if player is op
    if (!player.isOp()) {
      player.sendMessage("§cYou need operator permissions to use world settings!");
      return;
    }

    if (!worldSettings.defaultSettings[settingName]) {
      player.sendMessage(`§cWorld setting '${settingName}' does not exist!`);
      return;
    }

    const setting = worldSettings.defaultSettings[settingName];
    let value;

    // Parse value (same logic as player settings)
    switch (setting.type) {
      case "boolean":
        value = valueStr.toLowerCase() === "true" || valueStr.toLowerCase() === "on";
        break;
      case "number":
        value = parseFloat(valueStr);
        if (isNaN(value)) {
          player.sendMessage("§cInvalid number value!");
          return;
        }
        break;
      case "enum":
        if (!setting.values.includes(valueStr)) {
          player.sendMessage(`§cInvalid value! Valid options: ${setting.values.join(", ")}`);
          return;
        }
        value = valueStr;
        break;
      default:
        value = valueStr;
    }

    if (worldSettings.setWorldSetting(settingName, value)) {
      player.sendMessage(`§aSet world §b${settingName}§a to §f${value}`);
      // Notify all players of world setting change
      world.sendMessage(`§7[World] §b${settingName}§7 changed to §f${value}§7 by §a${player.name}`);
    } else {
      player.sendMessage("§cFailed to set world setting!");
    }
  }

  // Alternative method: Use items to trigger settings
  setupItemBasedCommands() {
    world.afterEvents.itemUse.subscribe((event) => {
      const { source: player, itemStack } = event;
      
      // No item-based settings triggers currently active
    });
  }
}

// Initialize settings commands
export const settingsCommands = new SettingsCommandManager();

// Settings Commands System initialized
