import type { Meta, StoryObj } from '@storybook/nextjs-vite'

import { expect, fn } from 'storybook/test'

import { MaterialIcon } from '../Icons/MaterialIcon'
import { config as typographyConfig } from '../Typography/variants'
import { Button } from './Button'
import { config as buttonConfig } from './variants'

const meta: Meta<typeof Button> = {
  title: 'Components/Button',
  component: Button,
  argTypes: {
    onPress: {
      table: {
        disable: true,
      },
    },
    typography: {
      control: 'select',
      options: Object.keys(typographyConfig.variants.variant),
    },
    variant: {
      control: 'select',
      options: Object.keys(buttonConfig.variants.variant),
      default: 'Primary',
      table: {
        defaultValue: {
          summary: buttonConfig.defaultVariants.variant,
        },
        type: {
          summary: 'string',
          detail: Object.keys(buttonConfig.variants.variant).join(' | '),
        },
      },
    },
    color: {
      control: 'select',
      options: Object.keys(buttonConfig.variants.color),
      table: {
        type: {
          summary: 'string',
          detail: Object.keys(buttonConfig.variants.color).join(' | '),
        },
        defaultValue: {
          summary: buttonConfig.defaultVariants.color,
        },
      },
    },
    size: {
      control: 'select',
      options: Object.keys(buttonConfig.variants.size),
      table: {
        type: {
          summary: 'string',
          detail: Object.keys(buttonConfig.variants.size).join(' | '),
        },
        defaultValue: {
          summary: buttonConfig.defaultVariants.size,
        },
      },
    },
    wrapping: {
      control: 'radio',
      options: Object.keys(buttonConfig.variants.wrapping),
      type: 'boolean',
      table: {
        defaultValue: {
          summary: buttonConfig.defaultVariants.wrapping.toString(),
        },
      },
      description:
        'Only applies to variant `Text`. If `false`, the button will use smaller padding.',
    },
  },
}

const globalStoryPropsInverted = {
  backgrounds: { value: 'scandicPrimaryDark' },
}
export default meta

type Story = StoryObj<typeof Button>

export const Default: Story = {
  args: {
    onPress: fn(),
    children: 'Button',
    typography: 'Body/Paragraph/mdBold',
  },
  play: async ({ canvas, userEvent, args }) => {
    await userEvent.click(await canvas.findByRole('button'))
    expect(args.onPress).toHaveBeenCalledTimes(1)
  },
}

export const PrimaryLarge: Story = {
  args: {
    ...Default.args,
    variant: 'Primary',
    size: 'Large',
  },
  play: async ({ canvas, userEvent, args }) => {
    await userEvent.click(await canvas.findByRole('button'))
    expect(args.onPress).toHaveBeenCalledTimes(1)
  },
}

export const PrimaryMedium: Story = {
  args: {
    ...PrimaryLarge.args,
    size: 'Medium',
  },
  play: async ({ canvas, userEvent, args }) => {
    await userEvent.click(await canvas.findByRole('button'))
    expect(args.onPress).toHaveBeenCalledTimes(1)
  },
}

export const PrimarySmall: Story = {
  args: {
    ...PrimaryLarge.args,
    typography: 'Body/Supporting text (caption)/smBold',
    size: 'Small',
  },
  play: async ({ canvas, userEvent, args }) => {
    await userEvent.click(await canvas.findByRole('button'))
    expect(args.onPress).toHaveBeenCalledTimes(1)
  },
}

export const PrimaryDisabled: Story = {
  args: {
    ...PrimaryLarge.args,
    isDisabled: true,
    onPress: fn(), // Fresh spy instance for disabled test
  },
  play: async ({ canvas, userEvent, args }) => {
    await userEvent.click(await canvas.findByRole('button'))
    expect(args.onPress).toHaveBeenCalledTimes(0)
  },
}

export const PrimaryLoading: Story = {
  args: {
    ...PrimaryLarge.args,
    isPending: true,
    onPress: fn(), // Fresh spy instance for loading test
  },
  play: async ({ canvas, userEvent, args }) => {
    await userEvent.click(await canvas.findByRole('button'))
    expect(args.onPress).toHaveBeenCalledTimes(0)
  },
}

export const PrimaryOnDarkBackground: Story = {
  globals: globalStoryPropsInverted,
  args: {
    ...PrimaryLarge.args,
    onPress: fn(), // Fresh spy instance
  },
  play: async ({ canvas, userEvent, args }) => {
    await userEvent.click(await canvas.findByRole('button'))
    expect(args.onPress).toHaveBeenCalledTimes(1)
  },
}

export const PrimaryInvertedLarge: Story = {
  globals: globalStoryPropsInverted,
  args: {
    ...Default.args,
    size: 'Large',
    color: 'Inverted',
  },
  play: async ({ canvas, userEvent, args }) => {
    await userEvent.click(await canvas.findByRole('button'))
    expect(args.onPress).toHaveBeenCalledTimes(1)
  },
}

export const PrimaryInvertedMedium: Story = {
  globals: globalStoryPropsInverted,
  args: {
    ...PrimaryInvertedLarge.args,
    size: 'Medium',
  },
  play: async ({ canvas, userEvent, args }) => {
    await userEvent.click(await canvas.findByRole('button'))
    expect(args.onPress).toHaveBeenCalledTimes(1)
  },
}

export const PrimaryInvertedSmall: Story = {
  globals: globalStoryPropsInverted,
  args: {
    ...PrimaryInvertedLarge.args,
    typography: 'Body/Supporting text (caption)/smBold',
    size: 'Small',
  },
  play: async ({ canvas, userEvent, args }) => {
    await userEvent.click(await canvas.findByRole('button'))
    expect(args.onPress).toHaveBeenCalledTimes(1)
  },
}

export const PrimaryInvertedDisabled: Story = {
  globals: globalStoryPropsInverted,
  args: {
    ...PrimaryInvertedLarge.args,
    isDisabled: true,
    onPress: fn(), // Fresh spy instance for disabled test
  },
  play: async ({ canvas, userEvent, args }) => {
    await userEvent.click(await canvas.findByRole('button'))
    expect(args.onPress).toHaveBeenCalledTimes(0)
  },
}

export const PrimaryInvertedLoading: Story = {
  globals: globalStoryPropsInverted,
  args: {
    ...PrimaryInvertedLarge.args,
    isPending: true,
    onPress: fn(), // Fresh spy instance for loading test
  },
  play: async ({ canvas, userEvent, args }) => {
    await userEvent.click(await canvas.findByRole('button'))
    expect(args.onPress).toHaveBeenCalledTimes(0)
  },
}

export const SecondaryLarge: Story = {
  args: {
    ...Default.args,
    variant: 'Secondary',
    size: 'Large',
  },
  play: async ({ canvas, userEvent, args }) => {
    await userEvent.click(await canvas.findByRole('button'))
    expect(args.onPress).toHaveBeenCalledTimes(1)
  },
}

export const SecondaryMedium: Story = {
  args: {
    ...SecondaryLarge.args,
    size: 'Medium',
  },
  play: async ({ canvas, userEvent, args }) => {
    await userEvent.click(await canvas.findByRole('button'))
    expect(args.onPress).toHaveBeenCalledTimes(1)
  },
}

export const SecondarySmall: Story = {
  args: {
    ...SecondaryLarge.args,
    typography: 'Body/Supporting text (caption)/smBold',
    size: 'Small',
  },
  play: async ({ canvas, userEvent, args }) => {
    await userEvent.click(await canvas.findByRole('button'))
    expect(args.onPress).toHaveBeenCalledTimes(1)
  },
}

export const SecondaryDisabled: Story = {
  args: {
    ...SecondaryLarge.args,
    isDisabled: true,
    onPress: fn(), // Fresh spy instance for disabled test
  },
  play: async ({ canvas, userEvent, args }) => {
    await userEvent.click(await canvas.findByRole('button'))
    expect(args.onPress).toHaveBeenCalledTimes(0)
  },
}

export const SecondaryLoading: Story = {
  args: {
    ...SecondaryLarge.args,
    isPending: true,
    onPress: fn(), // Fresh spy instance for loading test
  },
  play: async ({ canvas, userEvent, args }) => {
    await userEvent.click(await canvas.findByRole('button'))
    expect(args.onPress).toHaveBeenCalledTimes(0)
  },
}

export const SecondaryInvertedLarge: Story = {
  globals: globalStoryPropsInverted,
  args: {
    ...Default.args,
    variant: 'Secondary',
    color: 'Inverted',
    size: 'Large',
  },
  play: async ({ canvas, userEvent, args }) => {
    await userEvent.click(await canvas.findByRole('button'))
    expect(args.onPress).toHaveBeenCalledTimes(1)
  },
}

export const SecondaryInvertedMedium: Story = {
  globals: globalStoryPropsInverted,
  args: {
    ...SecondaryInvertedLarge.args,
    size: 'Medium',
  },
  play: async ({ canvas, userEvent, args }) => {
    await userEvent.click(await canvas.findByRole('button'))
    expect(args.onPress).toHaveBeenCalledTimes(1)
  },
}

export const SecondaryInvertedSmall: Story = {
  globals: globalStoryPropsInverted,
  args: {
    ...SecondaryInvertedLarge.args,
    typography: 'Body/Supporting text (caption)/smBold',
    size: 'Small',
  },
  play: async ({ canvas, userEvent, args }) => {
    await userEvent.click(await canvas.findByRole('button'))
    expect(args.onPress).toHaveBeenCalledTimes(1)
  },
}

export const SecondaryInvertedDisabled: Story = {
  globals: globalStoryPropsInverted,
  args: {
    ...SecondaryInvertedLarge.args,
    isDisabled: true,
    onPress: fn(), // Fresh spy instance for disabled test
  },
  play: async ({ canvas, userEvent, args }) => {
    await userEvent.click(await canvas.findByRole('button'))
    expect(args.onPress).toHaveBeenCalledTimes(0)
  },
}

export const SecondaryInvertedLoading: Story = {
  globals: globalStoryPropsInverted,
  args: {
    ...SecondaryInvertedLarge.args,
    isPending: true,
    onPress: fn(), // Fresh spy instance for loading test
  },
  play: async ({ canvas, userEvent, args }) => {
    await userEvent.click(await canvas.findByRole('button'))
    expect(args.onPress).toHaveBeenCalledTimes(0)
  },
}

export const TertiaryLarge: Story = {
  args: {
    ...Default.args,
    variant: 'Tertiary',
    size: 'Large',
  },
  play: async ({ canvas, userEvent, args }) => {
    await userEvent.click(await canvas.findByRole('button'))
    expect(args.onPress).toHaveBeenCalledTimes(1)
  },
}

export const TertiaryMedium: Story = {
  args: {
    ...TertiaryLarge.args,
    size: 'Medium',
  },
  play: async ({ canvas, userEvent, args }) => {
    await userEvent.click(await canvas.findByRole('button'))
    expect(args.onPress).toHaveBeenCalledTimes(1)
  },
}

export const TertiarySmall: Story = {
  args: {
    ...TertiaryLarge.args,
    typography: 'Body/Supporting text (caption)/smBold',
    size: 'Small',
  },
  play: async ({ canvas, userEvent, args }) => {
    await userEvent.click(await canvas.findByRole('button'))
    expect(args.onPress).toHaveBeenCalledTimes(1)
  },
}

export const TertiaryDisabled: Story = {
  args: {
    ...TertiaryLarge.args,
    isDisabled: true,
    onPress: fn(), // Fresh spy instance for disabled test
  },
  play: async ({ canvas, userEvent, args }) => {
    await userEvent.click(await canvas.findByRole('button'))
    expect(args.onPress).toHaveBeenCalledTimes(0)
  },
}

export const TertiaryLoading: Story = {
  args: {
    ...TertiaryLarge.args,
    isPending: true,
    onPress: fn(), // Fresh spy instance for loading test
  },
  play: async ({ canvas, userEvent, args }) => {
    await userEvent.click(await canvas.findByRole('button'))
    expect(args.onPress).toHaveBeenCalledTimes(0)
  },
}

export const TextLarge: Story = {
  args: {
    ...Default.args,
    variant: 'Text',
    size: 'Large',
  },
  play: async ({ canvas, userEvent, args }) => {
    await userEvent.click(await canvas.findByRole('button'))
    expect(args.onPress).toHaveBeenCalledTimes(1)
  },
}

export const TextMedium: Story = {
  args: {
    ...TextLarge.args,
    size: 'Medium',
  },

  play: async ({ canvas, userEvent, args }) => {
    await userEvent.click(await canvas.findByRole('button'))
    expect(args.onPress).toHaveBeenCalledTimes(1)
  },
}

export const TextSmall: Story = {
  args: {
    ...TextLarge.args,
    typography: 'Body/Supporting text (caption)/smBold',
    size: 'Small',
  },

  play: async ({ canvas, userEvent, args }) => {
    await userEvent.click(await canvas.findByRole('button'))
    expect(args.onPress).toHaveBeenCalledTimes(1)
  },
}

export const TextDisabled: Story = {
  args: {
    ...TextLarge.args,
    isDisabled: true,
    onPress: fn(), // Fresh spy instance for disabled test
  },
  play: async ({ canvas, userEvent, args }) => {
    await userEvent.click(await canvas.findByRole('button'))
    expect(args.onPress).toHaveBeenCalledTimes(0)
  },
}

export const TextNoWrapping: Story = {
  args: {
    ...TextLarge.args,
    wrapping: false,
  },
  play: async ({ canvas, userEvent, args }) => {
    await userEvent.click(await canvas.findByRole('button'))
    expect(args.onPress).toHaveBeenCalledTimes(1)
  },
}

export const TextInvertedLarge: Story = {
  globals: globalStoryPropsInverted,
  args: {
    ...Default.args,
    variant: 'Text',
    color: 'Inverted',
    size: 'Large',
  },
  play: async ({ canvas, userEvent, args }) => {
    await userEvent.click(await canvas.findByRole('button'))
    expect(args.onPress).toHaveBeenCalledTimes(1)
  },
}

export const TextInvertedMedium: Story = {
  globals: globalStoryPropsInverted,
  args: {
    ...TextInvertedLarge.args,
    size: 'Medium',
  },
  play: async ({ canvas, userEvent, args }) => {
    await userEvent.click(await canvas.findByRole('button'))
    expect(args.onPress).toHaveBeenCalledTimes(1)
  },
}

export const TextInvertedSmall: Story = {
  globals: globalStoryPropsInverted,
  args: {
    ...TextInvertedLarge.args,
    typography: 'Body/Supporting text (caption)/smBold',
    size: 'Small',
  },
  play: async ({ canvas, userEvent, args }) => {
    await userEvent.click(await canvas.findByRole('button'))
    expect(args.onPress).toHaveBeenCalledTimes(1)
  },
}

export const TextInvertedDisabled: Story = {
  globals: globalStoryPropsInverted,
  args: {
    ...TextInvertedLarge.args,
    isDisabled: true,
    onPress: fn(), // Fresh spy instance for disabled test
  },
  play: async ({ canvas, userEvent, args }) => {
    await userEvent.click(await canvas.findByRole('button'))
    expect(args.onPress).toHaveBeenCalledTimes(0)
  },
}

export const TextWithIcon: Story = {
  args: {
    ...TextLarge.args,
    children: (
      <>
        Text with icon
        <MaterialIcon icon="chevron_right" size={24} color="CurrentColor" />
      </>
    ),
  },
  play: async ({ canvas, userEvent, args }) => {
    await userEvent.click(await canvas.findByRole('button'))
    expect(args.onPress).toHaveBeenCalledTimes(1)

    expect(canvas.getByText('Text with icon')).toBeDefined()
    expect(canvas.getByTestId('MaterialIcon')).toBeDefined()
  },
}

export const TextWithIconInverted: Story = {
  globals: globalStoryPropsInverted,
  args: {
    ...TextWithIcon.args,
    color: 'Inverted',
  },
  play: async ({ canvas, userEvent, args }) => {
    await userEvent.click(await canvas.findByRole('button'))
    expect(args.onPress).toHaveBeenCalledTimes(1)

    expect(canvas.getByText('Text with icon')).toBeDefined()
    expect(canvas.getByTestId('MaterialIcon')).toBeDefined()
  },
}
