import { world, system } from "@minecraft/server";

// World Settings System similar to Anime Jutsu
export class WorldSettingsManager {
  constructor(namespace = "aft_soulhunters") {
    this.namespace = namespace;
    this.eventListeners = new Map();
    
    // Default world settings
    this.defaultSettings = {
      // Combat Settings
      soul_hunters_pvp: { type: "boolean", default: true },
      friendly_fire_companions: { type: "boolean", default: false },
      weapon_griefing: { type: "boolean", default: false },
      
      // Spawn Settings  
      soul_altar_spawn_rate: { type: "enum", default: "normal", values: ["none", "rare", "normal", "frequent"] },
      companion_spawn_limit: { type: "number", default: 5, min: 1, max: 10 },
      
      // World Effects
      soul_realm_effects: { type: "boolean", default: true },
      ambient_soul_particles: { type: "boolean", default: true },
      altar_activation_effects: { type: "boolean", default: true },
      
      // Boss Settings
      boss_soul_multiplier: { type: "number", default: 1, min: 1, max: 5 },
      boss_companion_limit: { type: "number", default: 3, min: 1, max: 5 },
      
      // Performance Settings
      max_active_souls: { type: "number", default: 50, min: 10, max: 100 },
      particle_density: { type: "enum", default: "normal", values: ["low", "normal", "high"] }
    };
  }

  // Get world setting
  getWorldSetting(settingName) {
    try {
      const key = `${this.namespace}:world_${settingName}`;
      const value = world.getDynamicProperty(key);
      
      if (value === undefined) {
        return this.defaultSettings[settingName]?.default;
      }
      
      return value;
    } catch (e) {
      // removed debug log
      return this.defaultSettings[settingName]?.default;
    }
  }

  // Set world setting
  setWorldSetting(settingName, value) {
    try {
      const key = `${this.namespace}:world_${settingName}`;
      world.setDynamicProperty(key, value);
      
      // Trigger event listeners
      this.triggerSettingChange(settingName, value);
      
      return true;
    } catch (e) {
      // removed debug log
      return false;
    }
  }

  // Add event listener for setting changes
  addEventListener(settingName, callback) {
    if (!this.eventListeners.has(settingName)) {
      this.eventListeners.set(settingName, []);
    }
    this.eventListeners.get(settingName).push(callback);
  }

  // Trigger setting change events
  triggerSettingChange(settingName, value) {
    const listeners = this.eventListeners.get(settingName);
    if (listeners) {
      listeners.forEach(callback => {
        try {
          callback({ settingName, value });
        } catch (e) {
          // removed debug log
        }
      });
    }
  }

  // Initialize world settings
  initializeWorld() {
    Object.keys(this.defaultSettings).forEach(settingName => {
      const currentValue = this.getWorldSetting(settingName);
      if (currentValue !== undefined) {
        this.triggerSettingChange(settingName, currentValue);
      }
    });
  }

  // Validate setting value
  validateSetting(settingName, value) {
    const setting = this.defaultSettings[settingName];
    if (!setting) return false;

    switch (setting.type) {
      case "boolean":
        return typeof value === "boolean";
      case "number":
        return typeof value === "number" && 
               value >= (setting.min || -Infinity) && 
               value <= (setting.max || Infinity);
      case "enum":
        return setting.values && setting.values.includes(value);
      default:
        return true;
    }
  }
}

// Global instance
export const worldSettings = new WorldSettingsManager();

// Initialize world settings
system.runTimeout(() => {
  worldSettings.initializeWorld();
}, 40); // Wait 2 seconds after world load

// World Settings System initialized
