import {
  ICollectItemsRequirement,
  ICraftRequirement,
  IKillMobsRequirement,
  IMapVisitRequirement,
  ITaskIdentifier,
  TaskStatus,
  TaskType
} from '@rpg-engine/shared';
import { Meta, StoryObj } from '@storybook/react';
import React from 'react';
import { DailyTasks, IDailyTasksProps } from '../../../components/DailyTasks/DailyTasks';
import { RPGUIRoot } from '../../../components/RPGUI/RPGUIRoot';
import iconAtlasJSON from '../../../mocks/atlas/icons/icons.json';
import iconAtlasIMG from '../../../mocks/atlas/icons/icons.png';
import itemsAtlasJSON from '../../../mocks/atlas/items/items.json';
import itemsAtlasIMG from '../../../mocks/atlas/items/items.png';
import { mockTasks } from '../../../mocks/dailyTasks.mocks';

const meta = {
  title: 'Features/Daily Tasks/Daily Tasks',
  component: DailyTasks,
  parameters: {
    layout: 'centered',
  },
} satisfies Meta<typeof DailyTasks>;

export default meta;
type Story = StoryObj<IDailyTasksProps>;

const Template = (args: IDailyTasksProps) => {
  return (
    <RPGUIRoot>
      <DailyTasks 
        {...args} 
      />
    </RPGUIRoot>
  );
};

export const Default: Story = {
  render: Template,
  args: {
    tasks: mockTasks,
    itemsAtlasJSON,
    itemsAtlasIMG,
    iconAtlasJSON,
    iconAtlasIMG,
    onClaimReward: ({ taskKey, type }: ITaskIdentifier) => {
      console.log('Reward collected:', taskKey, type);
    },
    onClose: () => {
      console.log('Closing daily tasks window');
    },
  },
};

export const EmptyTasks: Story = {
  render: Template,
  args: {
    tasks: [],
    onClaimReward: ({ taskKey, type }: ITaskIdentifier) => {
      console.log('Reward collected:', taskKey, type);
    },
    onClose: () => {
      console.log('Closing daily tasks window');
    },
  },
};

export const AllCompletedClaimed: Story = {
  render: Template,
  args: {
    itemsAtlasJSON,
    itemsAtlasIMG,
    iconAtlasJSON,
    iconAtlasIMG,
    tasks: mockTasks.map(task => {
      const baseTask = {
        ...task,
        claimed: true,
        status: TaskStatus.Completed,
      };

      switch (task.type) {  
        case TaskType.KillMobs:
          const killMobsRequirement = task.requirements as IKillMobsRequirement;

          return {
            ...baseTask,
            progress: {
              kills: killMobsRequirement.targets.reduce((acc, target) => {
                acc[target.key] = target.quantity;
                return acc;
              }, {} as Record<string, number>)
            }
          };
        case TaskType.CollectItems:
          const collectItemsRequirement = task.requirements as ICollectItemsRequirement;

          return {
            ...baseTask,
            progress: {
              collected: collectItemsRequirement.targets.reduce((acc, target) => {
                acc[target.key] = target.quantity;
                return acc;
              }, {} as Record<string, number>)
            }
          };
        case TaskType.CraftItems:
          const craftItemsRequirement = task.requirements as ICraftRequirement;

          return {
            ...baseTask,
            progress: {
              crafted: craftItemsRequirement.targets.reduce((acc, target) => {
                acc[target.key] = target.quantity;
                return acc;
              }, {} as Record<string, number>)
            }
          };
        case TaskType.MapVisit:
          const mapVisitRequirement = task.requirements as IMapVisitRequirement;
          return {
            ...baseTask,
            progress: {
              visitedMaps: mapVisitRequirement.mapKeys.reduce((acc, key) => {
                acc[key] = true;
                return acc;
              }, {} as Record<string, boolean>)
            }
          };
        default:
          return baseTask;
      }
    }),
    onClaimReward: ({ taskKey, type }: ITaskIdentifier) => {
      console.log('Reward collected:', taskKey, type);
    },
    onClose: () => {
      console.log('Closing daily tasks window');
    },
  },
};

