import type { CustomWorldProfile } from '../../types';

import CampSceneEditor from './CustomWorldCampEditorSection';
import WorldCoverEditor from './CustomWorldCoverEditorSection';
import WorldFoundationEditor from './CustomWorldFoundationEditorSection';
import LandmarkEditor from './CustomWorldLandmarkEditorSection';
import PlayableNpcEditor, {
  StoryNpcEditor,
} from './CustomWorldRoleEditorSection';
import WorldEditor from './CustomWorldWorldEditorSection';
import {
  resolveEditableLandmark,
  resolveEditablePlayableNpc,
  resolveEditableStoryNpc,
} from './rpgCreationResultFormMapper';

export type RpgCreationEditorTarget =
  | { kind: 'world' }
  | { kind: 'foundation' }
  | { kind: 'cover' }
  | { kind: 'camp' }
  | { kind: 'playable'; mode: 'create' }
  | { kind: 'playable'; mode: 'edit'; id: string }
  | { kind: 'story'; mode: 'create' }
  | { kind: 'story'; mode: 'edit'; id: string }
  | { kind: 'landmark'; mode: 'create' }
  | { kind: 'landmark'; mode: 'edit'; id: string };

export interface RpgCreationEntityEditorModalProps {
  profile: CustomWorldProfile;
  target: RpgCreationEditorTarget | null;
  onClose: () => void;
  onProfileChange: (profile: CustomWorldProfile) => void;
}

/**
 * 工作包 C 收口后的编辑器主入口只负责目标分发。
 * 具体 section 实现已经下沉到 shared/section 文件，后续可以继续物理拆分而不再膨胀主壳层。
 */
export function RpgCreationEntityEditorModal({
  profile,
  target,
  onClose,
  onProfileChange,
}: RpgCreationEntityEditorModalProps) {
  if (!target) {
    return null;
  }

  if (target.kind === 'world') {
    return (
      <WorldEditor
        profile={profile}
        onSave={onProfileChange}
        onClose={onClose}
      />
    );
  }

  if (target.kind === 'foundation') {
    return (
      <WorldFoundationEditor
        profile={profile}
        onSave={onProfileChange}
        onClose={onClose}
      />
    );
  }

  if (target.kind === 'cover') {
    return (
      <WorldCoverEditor
        profile={profile}
        onSaveProfile={onProfileChange}
        onClose={onClose}
      />
    );
  }

  if (target.kind === 'camp') {
    return (
      <CampSceneEditor
        profile={profile}
        onSaveProfile={onProfileChange}
        onClose={onClose}
      />
    );
  }

  if (target.kind === 'playable') {
    const npc = resolveEditablePlayableNpc(profile, target);
    return npc ? (
      <PlayableNpcEditor
        profile={profile}
        npc={npc}
        mode={target.mode}
        onSave={(nextNpc) =>
          onProfileChange({
            ...profile,
            playableNpcs:
              target.mode === 'create'
                ? [...profile.playableNpcs, nextNpc]
                : profile.playableNpcs.map((item) =>
                    item.id === nextNpc.id ? nextNpc : item,
                  ),
          })
        }
        onClose={onClose}
      />
    ) : null;
  }

  if (target.kind === 'story') {
    const npc = resolveEditableStoryNpc(profile, target);
    return npc ? (
      <StoryNpcEditor
        profile={profile}
        npc={npc}
        mode={target.mode}
        onSave={(nextNpc) =>
          onProfileChange({
            ...profile,
            storyNpcs:
              target.mode === 'create'
                ? [...profile.storyNpcs, nextNpc]
                : profile.storyNpcs.map((item) =>
                    item.id === nextNpc.id ? nextNpc : item,
                  ),
          })
        }
        onClose={onClose}
      />
    ) : null;
  }

  const landmark = resolveEditableLandmark(profile, target);
  return landmark ? (
    <LandmarkEditor
      profile={profile}
      landmark={landmark}
      mode={target.mode}
      onSaveProfile={onProfileChange}
      onClose={onClose}
    />
  ) : null;
}

export default RpgCreationEntityEditorModal;
