import type { ComponentProps } from 'react';

import { UnifiedCreationPage } from './UnifiedCreationPage';
import type { UnifiedCreationSpec } from './unifiedCreationSpecs';
import { Match3DCreationWorkspace } from './workspaces/Match3DCreationWorkspace';
import { PuzzleCreationWorkspace } from './workspaces/PuzzleCreationWorkspace';
import { JumpHopCreationWorkspace } from './workspaces/JumpHopCreationWorkspace';
import { WoodenFishCreationWorkspace } from './workspaces/WoodenFishCreationWorkspace';

type PuzzleCreationWorkspaceProps = ComponentProps<
  typeof PuzzleCreationWorkspace
>;
type Match3DCreationWorkspaceProps = ComponentProps<
  typeof Match3DCreationWorkspace
>;
type JumpHopCreationWorkspaceProps = ComponentProps<
  typeof JumpHopCreationWorkspace
>;
type WoodenFishCreationWorkspaceProps = ComponentProps<
  typeof WoodenFishCreationWorkspace
>;

type UnifiedCreationWorkspaceBaseProps = {
  spec: UnifiedCreationSpec;
};

type PuzzleUnifiedCreationWorkspaceProps =
  UnifiedCreationWorkspaceBaseProps & {
    playId: 'puzzle';
  } & PuzzleCreationWorkspaceProps;

type Match3DUnifiedCreationWorkspaceProps =
  UnifiedCreationWorkspaceBaseProps & {
    playId: 'match3d';
  } & Match3DCreationWorkspaceProps;

type JumpHopUnifiedCreationWorkspaceProps =
  UnifiedCreationWorkspaceBaseProps & {
    playId: 'jump-hop';
  } & JumpHopCreationWorkspaceProps;

type WoodenFishUnifiedCreationWorkspaceProps =
  UnifiedCreationWorkspaceBaseProps & {
    playId: 'wooden-fish';
  } & WoodenFishCreationWorkspaceProps;

export type UnifiedCreationWorkspaceProps =
  | PuzzleUnifiedCreationWorkspaceProps
  | Match3DUnifiedCreationWorkspaceProps
  | JumpHopUnifiedCreationWorkspaceProps
  | WoodenFishUnifiedCreationWorkspaceProps;

export function UnifiedCreationWorkspace(props: UnifiedCreationWorkspaceProps) {
  switch (props.playId) {
    case 'puzzle':
      return (
        <UnifiedCreationPage spec={props.spec} onBack={props.onBack}>
          <PuzzleCreationWorkspace
            session={props.session}
            isBusy={props.isBusy}
            error={props.error}
            onBack={props.onBack}
            onSubmitMessage={props.onSubmitMessage}
            onExecuteAction={props.onExecuteAction}
            onCreateFromForm={props.onCreateFromForm}
            onAutoSaveForm={props.onAutoSaveForm}
            initialFormPayload={props.initialFormPayload}
            showBackButton={false}
            title={null}
            unifiedChrome
          />
        </UnifiedCreationPage>
      );
    case 'match3d':
      return (
        <UnifiedCreationPage spec={props.spec} onBack={props.onBack}>
          <Match3DCreationWorkspace
            session={props.session}
            isBusy={props.isBusy}
            error={props.error}
            onBack={props.onBack}
            onExecuteAction={props.onExecuteAction}
            onCreateFromForm={props.onCreateFromForm}
            onSubmitMessage={props.onSubmitMessage}
            initialFormPayload={props.initialFormPayload}
            showBackButton={false}
            title={null}
            unifiedChrome
          />
        </UnifiedCreationPage>
      );
    case 'jump-hop':
      return (
        <UnifiedCreationPage spec={props.spec} onBack={props.onBack}>
          <JumpHopCreationWorkspace
            isBusy={props.isBusy}
            error={props.error}
            onBack={props.onBack}
            onSubmitted={props.onSubmitted}
            showBackButton={false}
            unifiedChrome
          />
        </UnifiedCreationPage>
      );
    case 'wooden-fish':
      return (
        <UnifiedCreationPage spec={props.spec} onBack={props.onBack}>
          <WoodenFishCreationWorkspace
            isBusy={props.isBusy}
            error={props.error}
            onBack={props.onBack}
            onSubmitted={props.onSubmitted}
            showBackButton={false}
            unifiedChrome
          />
        </UnifiedCreationPage>
      );
    default: {
      const exhaustiveCheck: never = props;
      return exhaustiveCheck;
    }
  }
}

export default UnifiedCreationWorkspace;
