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

// Player Settings System similar to Anime Jutsu
export class PlayerSettingsManager {
  constructor(namespace = "aft_soulhunters") {
    this.namespace = namespace;
    this.settings = new Map();
    this.eventListeners = new Map();
    
    // Default player settings
    this.defaultSettings = {
      // Visual Settings
      weapon_glow_effects: { type: "boolean", default: true },
      soul_altar_particles: { type: "boolean", default: true },
      companion_outline: { type: "boolean", default: true },
      hide_action_bar: { type: "boolean", default: false },
      
      // Camera Settings
      camera_shake_intensity: { type: "enum", default: "normal", values: ["off", "low", "normal", "high"] },
      
      // Audio Settings
      weapon_sound_effects: { type: "boolean", default: true },
      ambient_soul_sounds: { type: "boolean", default: true },
      
      // Gameplay Settings
      auto_soul_collection: { type: "boolean", default: false },
      weapon_ability_hints: { type: "boolean", default: true },
      companion_auto_follow: { type: "boolean", default: true }
    };
  }

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

  // Set player setting
  setPlayerSetting(player, settingName, value) {
    try {
      const key = `${this.namespace}:setting_${settingName}`;
      player.setDynamicProperty(key, value);
      
      // Trigger event listeners
      this.triggerSettingChange(player, 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(player, settingName, value) {
    const listeners = this.eventListeners.get(settingName);
    if (listeners) {
      listeners.forEach(callback => {
        try {
          callback({ player, settingName, value });
        } catch (e) {
          // removed debug log
        }
      });
    }
  }

  // Initialize player settings on spawn
  initializePlayer(player) {
    Object.keys(this.defaultSettings).forEach(settingName => {
      const currentValue = this.getPlayerSetting(player, settingName);
      if (currentValue !== undefined) {
        this.triggerSettingChange(player, settingName, currentValue);
      }
    });
  }
}

// Global instance
export const playerSettings = new PlayerSettingsManager();

// Initialize on player spawn
world.afterEvents.playerSpawn.subscribe(({ player, initialSpawn }) => {
  if (initialSpawn) {
    system.runTimeout(() => {
      playerSettings.initializePlayer(player);
    }, 20); // Wait 1 second after spawn
  }
});

// Player Settings System initialized
