/**
 * @moduleName: Settings Service (MVP Simplified)
 * @version: 1.0.0
 * @since: 2025-07-24
 * @lastUpdated: 2025-07-24
 * @projectSummary: Local browser storage for user preferences - simplified for MVP
 * @techStack: TypeScript, LocalStorage API, Browser APIs
 * @dependency: Browser localStorage
 * @interModuleDependency: None
 * @requirementsTraceability: MVP-LOCAL (Local storage only)
 * @briefDescription: Basic settings management for MVP deployment
 * @methods: getSettings, updateSettings, resetToDefaults
 * @contributors: GitHub Copilot
 * @examples: const service = new SettingsService(); service.getSettings();
 * @vulnerabilitiesAssessment: Local storage only, no sensitive data
 */

export interface AppSettings {
  ui: {
    hideDisabledNavButtons: boolean;
    hideAllNavButtons: boolean;
    autoAdvanceQuestions: boolean;
    clickToAdvance: boolean;
    showContinueButton: boolean;
    announceSelections: boolean;
    selectionFeedbackDuration: number;
    showQuestionNumbers: boolean;
    enableAnimations: boolean;
    defaultViewMode: 'single' | 'list';
    sidebarExpanded: boolean;
    theme: 'light' | 'dark' | 'auto';
    accessibilityMode: boolean;
    // ISSUE_020: Click-to-Select Answer Cards
    clickToSelectCards: boolean;
  };
  quiz: {
    showHints: boolean;
    confirmBeforeSubmit: boolean;
    highlightAnsweredQuestions: boolean;
    timerEnabled: boolean;
    maxAttempts: number;
    // Educational Feedback Enhancement (ISSUE_009_FEEDBACK)
    showImmediateFeedback: boolean;
    showExplanationsAfterAnswer: boolean;
    enableCoachingMode: boolean;
    coachingIntensity: 'basic' | 'detailed' | 'comprehensive';
    // Instant Click-to-Submit System
    instantSubmission: boolean;
    instantFeedbackDelay: number;
    allowAnswerChange: boolean;
    showInstantExplanation: boolean;
  };
}

export class SettingsService {
  private static readonly STORAGE_KEY = 'quiz-app-settings';
  private static instance: SettingsService;
  private settings: AppSettings;
  private listeners: Set<(settings: AppSettings) => void> = new Set();

  constructor() {
    this.settings = this.loadSettings();
  }

  /**
   * @description Gets singleton instance (for compatibility)
   * @returns {SettingsService} Singleton instance
   */
  static getInstance(): SettingsService {
    if (!SettingsService.instance) {
      SettingsService.instance = new SettingsService();
    }
    return SettingsService.instance;
  }

  private getDefaultSettings(): AppSettings {
    return {
      ui: {
        hideDisabledNavButtons: false,
        hideAllNavButtons: false,
        autoAdvanceQuestions: false,
        clickToAdvance: false,
        showContinueButton: true,
        announceSelections: true,
        selectionFeedbackDuration: 1000,
        showQuestionNumbers: true,
        enableAnimations: true,
        defaultViewMode: 'list',
        sidebarExpanded: true,
        theme: 'auto',
        accessibilityMode: false,
        // ISSUE_020: Click-to-Select Answer Cards
        clickToSelectCards: true, // Enable clickable answer cards in single mode
      },
      quiz: {
        showHints: false,
        confirmBeforeSubmit: true,
        highlightAnsweredQuestions: true,
        timerEnabled: false,
        maxAttempts: 3,
        // Educational Feedback Enhancement (ISSUE_009_FEEDBACK)
        showImmediateFeedback: false,
        showExplanationsAfterAnswer: false,
        enableCoachingMode: false,
        coachingIntensity: 'basic',
        // Instant Click-to-Submit System
        instantSubmission: false,
        instantFeedbackDelay: 1500, // milliseconds before auto-advance
        allowAnswerChange: true, // allow clicking different answer before auto-advance
        showInstantExplanation: true,
      },
    };
  }

  getSettings(): AppSettings {
    return { ...this.settings };
  }

  updateSettings(updates: Partial<AppSettings>): void {
    this.settings = { ...this.settings, ...updates };
    this.saveSettings();
    this.notifyListeners();
  }

  updateUISettings(uiSettings: Partial<AppSettings['ui']>): void {
    this.settings.ui = { ...this.settings.ui, ...uiSettings };
    this.saveSettings();
    this.notifyListeners();
  }

  updateQuizSettings(quizSettings: Partial<AppSettings['quiz']>): void {
    this.settings.quiz = { ...this.settings.quiz, ...quizSettings };
    this.saveSettings();
    this.notifyListeners();
  }

  resetToDefaults(): void {
    this.settings = this.getDefaultSettings();
    this.saveSettings();
    this.notifyListeners();
  }

  subscribe(callback: (settings: AppSettings) => void): () => void {
    this.listeners.add(callback);
    return () => this.listeners.delete(callback);
  }

  private loadSettings(): AppSettings {
    try {
      const stored = localStorage.getItem(SettingsService.STORAGE_KEY);
      if (stored) {
        const parsed = JSON.parse(stored);
        return { ...this.getDefaultSettings(), ...parsed };
      }
    } catch (error) {
      console.warn('Failed to load settings:', error);
    }
    return this.getDefaultSettings();
  }

  private saveSettings(): void {
    try {
      localStorage.setItem(SettingsService.STORAGE_KEY, JSON.stringify(this.settings));
    } catch (error) {
      console.error('Failed to save settings:', error);
    }
  }

  private notifyListeners(): void {
    this.listeners.forEach(callback => {
      try {
        callback(this.settings);
      } catch (error) {
        console.error('Error in settings listener:', error);
      }
    });
  }
}

// Export singleton instance for compatibility
const settingsService = new SettingsService();
export default settingsService;
