/**
 * Tutorial Manager
 * Handles tutorial progression and step triggers
 */

import { tutorialSteps } from '../data/tutorialSteps';

export class TutorialManager {
  store: any;
  
  constructor(settingsStore: any) {
    this.store = settingsStore;
  }

  /**
   * Get current tutorial step
   * @returns {Object|null} - Current step or null
   */
  getCurrentStep() {
    const { tutorial } = this.store.getState();
    
    if (tutorial.completed || tutorial.skipped) {
      return null;
    }

    return tutorialSteps[tutorial.currentStep] || null;
  }

  /**
   * Advance to next step
   */
  nextStep() {
    const { tutorial, setTutorialStep } = this.store.getState();
    const nextIndex = tutorial.currentStep + 1;

    if (nextIndex >= tutorialSteps.length) {
      this.complete();
      return null;
    }

    setTutorialStep(nextIndex);
    return tutorialSteps[nextIndex];
  }

  /**
   * Go to previous step
   */
  previousStep() {
    const { tutorial, setTutorialStep } = this.store.getState();
    const prevIndex = tutorial.currentStep - 1;

    if (prevIndex < 0) {
      return null; // Already at first step
    }

    setTutorialStep(prevIndex);
    return tutorialSteps[prevIndex];
  }

  /**
   * Skip tutorial
   */
  skip() {
    this.store.getState().skipTutorial();
  }

  /**
   * Complete tutorial
   */
  complete() {
    this.store.getState().completeTutorial();
  }

  /**
   * Reset tutorial (for replay)
   */
  reset() {
    this.store.getState().resetTutorial();
  }

  /**
   * Check if a trigger should advance tutorial
   * @param {string} trigger - Trigger type
   */
  checkTrigger(trigger) {
    const currentStep = this.getCurrentStep();
    
    if (!currentStep) return false;
    if (currentStep.nextTrigger !== trigger) return false;

    this.nextStep();
    return true;
  }

  /**
   * Get progress percentage
   * @returns {number} - 0-100
   */
  getProgress() {
    const { tutorial } = this.store.getState();
    
    if (tutorial.completed || tutorial.skipped) {
      return 100;
    }

    return Math.round((tutorial.currentStep / tutorialSteps.length) * 100);
  }

  /**
   * Check if tutorial is active
   * @returns {boolean}
   */
  isActive() {
    const { tutorial, game } = this.store.getState();
    return game.showTutorials && !tutorial.completed && !tutorial.skipped;
  }
}

