import { useRef } from "react";
import type { LegacyLifeInstance } from "../legacy/interop.js";
import type { LegacySessionActionState } from "./legacySessionActionTypes";
import type { LegacyLifeSession } from "./legacySessionTypes";
import { useLegacyFeedbackCenter } from "./useLegacyFeedbackCenter";
import { useLegacySessionActions } from "./useLegacySessionActions";
import { useLegacySessionLifecycle } from "./useLegacySessionLifecycle";
import { useLegacySessionMeta } from "./useLegacySessionMeta";
import { useLegacySessionState } from "./useLegacySessionState";

export type {
  AllocationState,
  DraftStatKey,
  LegacyActivityEntry,
  LegacyActivitySource,
  LegacyBanner,
  LegacyBannerTone,
  LegacyLifeSession,
  LegacyStage,
  LegacyStatKey,
  LegacyStatRecord,
} from "./legacySessionTypes";

export function useLegacyLifeSession(): LegacyLifeSession {
  const lifeRef = useRef<LegacyLifeInstance | null>(null);
  const { derived, setters, state } = useLegacySessionState();
  const actionState: LegacySessionActionState = {
    ...state,
    remainingPoints: derived.remainingPoints,
    selectedTalents: derived.selectedTalents,
  };
  const { achievementCatalog, refreshMeta, totalCards, totals } =
    useLegacySessionMeta(lifeRef);
  const {
    activities,
    banners,
    errorMessage,
    clearError,
    dismissBanner,
    addActivity,
    showError,
    showValidation,
  } = useLegacyFeedbackCenter();
  const {
    beginLife,
    openSummary,
    randomizeAllocation,
    refreshDraft,
    remakeLife,
    reshuffleSpecialThanks,
    selectExtendTalent,
    setAllocationValue,
    stepLife,
    toggleTalent,
  } = useLegacySessionActions({
    feedback: {
      addActivity,
      clearError,
      showError,
      showValidation,
    },
    lifeRef,
    refreshMeta,
    setters,
    state: actionState,
  });
  useLegacySessionLifecycle({
    addActivity,
    autoPlaySpeed: state.autoPlaySpeed,
    clearError,
    lifeRef,
    refreshDraft,
    refreshMeta,
    selectedTalentIds: state.selectedTalentIds,
    setAllocation: setters.setAllocation,
    setAutoPlaySpeed: setters.setAutoPlaySpeed,
    setIsBusy: setters.setIsBusy,
    setIsReady: setters.setIsReady,
    setSpecialThanks: setters.setSpecialThanks,
    setStage: setters.setStage,
    showError,
    stage: state.stage,
    stepLife,
    trajectoryIsEnd: derived.trajectoryIsEnd,
  });

  return {
    stage: state.stage,
    isReady: state.isReady,
    isBusy: state.isBusy,
    errorMessage,
    availableTalents: state.availableTalents,
    selectedTalentIds: state.selectedTalentIds,
    extendTalentId: state.extendTalentId,
    allocation: state.allocation,
    trajectory: state.trajectory,
    currentRecord: state.currentRecord,
    summary: state.summary,
    totals,
    totalCards,
    achievementCatalog,
    specialThanks: state.specialThanks,
    activities,
    banners,
    autoPlaySpeed: state.autoPlaySpeed,
    remainingPoints: derived.remainingPoints,
    canStartLife: derived.canStartLife,
    selectedTalents: derived.selectedTalents,
    sortedExtendOptions: derived.sortedExtendOptions,
    toggleTalent,
    dismissBanner,
    refreshDraft,
    reshuffleSpecialThanks,
    setAllocationValue,
    randomizeAllocation,
    beginLife,
    stepLife,
    openSummary,
    selectExtendTalent,
    remakeLife,
    setAutoPlaySpeed: setters.setAutoPlaySpeed,
  };
}
