import {
  getArgs,
  getArgTypes,
  getSlotNames,
  getSlotsFragment,
  getDocsDescription,
} from "../../utils/storybook";

import UDropdownLink from "../../ui.dropdown-link/UDropdownLink.vue";
import URow from "../../ui.container-row/URow.vue";
import UCol from "../../ui.container-col/UCol.vue";
import UIcon from "../../ui.image-icon/UIcon.vue";
import UBadge from "../../ui.text-badge/UBadge.vue";
import ULink from "../../ui.button-link/ULink.vue";
import UAvatar from "../../ui.image-avatar/UAvatar.vue";
import UText from "../../ui.text-block/UText.vue";
import ULoader from "../../ui.loader/ULoader.vue";

import type { Meta, StoryFn } from "@storybook/vue3-vite";
import type { Props } from "../types";

import johnDoe from "../../ui.form-select/storybook/assets/images/john-doe.png";
import emilyDavis from "../../ui.form-select/storybook/assets/images/emily-davis.png";
import alexJohnson from "../../ui.form-select/storybook/assets/images/alex-johnson.png";
import patMorgan from "../../ui.form-select/storybook/assets/images/pat-morgan.png";

interface DefaultUDropdownLinkArgs extends Props {
  slotTemplate?: string;
}

interface EnumUDropdownLinkArgs extends DefaultUDropdownLinkArgs {
  enum: keyof Pick<Props, "size" | "color" | "xPosition" | "yPosition">;
  class?: string;
}

export default {
  id: "2030",
  title: "Dropdowns / Dropdown Link",
  component: UDropdownLink,
  args: {
    label: "Account",
    options: [
      { label: "Profile", value: "profile" },
      { label: "Settings", value: "settings" },
      { label: "Logout", value: "logout" },
    ],
  },
  argTypes: {
    ...getArgTypes(UDropdownLink.__name),
  },
  parameters: {
    docs: {
      ...getDocsDescription(UDropdownLink.__name),
      story: {
        height: "200px",
      },
    },
  },
} as Meta;

const DefaultTemplate: StoryFn<DefaultUDropdownLinkArgs> = (args: DefaultUDropdownLinkArgs) => ({
  components: { UDropdownLink, UIcon, ULink, UBadge, UAvatar, ULoader, URow, UText },
  setup: () => ({ args, slots: getSlotNames(UDropdownLink.__name) }),
  template: `
    <UDropdownLink v-bind="args">
      ${args.slotTemplate || getSlotsFragment("")}
    </UDropdownLink>
  `,
});

const SelectableTemplate: StoryFn<DefaultUDropdownLinkArgs> = (args: DefaultUDropdownLinkArgs) => ({
  components: { UDropdownLink, UIcon, ULink },
  setup: () => ({ args, slots: getSlotNames(UDropdownLink.__name) }),
  template: `
    <UDropdownLink v-bind="args" v-model="args.modelValue">
      ${args.slotTemplate || getSlotsFragment("")}
    </UDropdownLink>
  `,
});

const EnumTemplate: StoryFn<EnumUDropdownLinkArgs> = (
  args: EnumUDropdownLinkArgs,
  { argTypes },
) => ({
  components: { UDropdownLink, URow },
  setup: () => ({ args, argTypes, getArgs }),
  template: `
    <URow>
      <UDropdownLink
        v-for="option in argTypes?.[args.enum]?.options"
        v-bind="getArgs(args, option)"
        :key="option"
      />
    </URow>
  `,
});

const GroupValuesTemplate: StoryFn<DefaultUDropdownLinkArgs> = (
  args: DefaultUDropdownLinkArgs,
) => ({
  components: { UDropdownLink, URow },
  setup() {
    return {
      args,
    };
  },
  template: `
    <URow gap="2xl">
      <UDropdownLink
        v-bind="args"
        v-model="args.modelValue"
        label="Single"
        :config="{ listbox: 'min-w-[200px]' }"
      />

      <UDropdownLink
        v-bind="args"
        v-model="args.modelValueMultiple"
        label="Multiple"
        multiple
        :config="{ listbox: 'min-w-[200px]' }"
      />
    </URow>
  `,
});

export const Default = DefaultTemplate.bind({});
Default.args = {};
Default.parameters = {
  docs: {
    story: {
      height: "250px",
    },
  },
};

export const Disabled = DefaultTemplate.bind({});
Disabled.args = { disabled: true };
Disabled.parameters = {
  docs: {
    story: {
      height: "120px",
    },
  },
};

export const Searchable = DefaultTemplate.bind({});
Searchable.args = { searchable: true };
Searchable.parameters = {
  docs: {
    story: {
      height: "250px",
    },
  },
};

export const SearchModelValue = DefaultTemplate.bind({});
SearchModelValue.args = { searchable: true, search: "Settings" };
SearchModelValue.parameters = {
  docs: {
    story: {
      height: "250px",
    },
  },
};

export const NoCloseOnSelect = SelectableTemplate.bind({});
NoCloseOnSelect.args = { modelValue: "logout", closeOnSelect: false };

export const OptionSelection = SelectableTemplate.bind({});
OptionSelection.args = { modelValue: "profile" };

export const MultipleOptionSelection = SelectableTemplate.bind({});
MultipleOptionSelection.args = {
  modelValue: ["profile", "settings", "logout"],
  multiple: true,
};

export const Size = EnumTemplate.bind({});
Size.args = { enum: "size", label: "{enumValue}" };

export const ListboxXPosition = EnumTemplate.bind({});
ListboxXPosition.args = {
  enum: "xPosition",
  label: "{enumValue}",
  config: { dropdownLink: "w-40 py-1 px-2.5 border border-dashed border-primary" },
};

export const ListboxYPosition = EnumTemplate.bind({});
ListboxYPosition.args = { enum: "yPosition", label: "{enumValue}" };
ListboxYPosition.parameters = {
  storyClasses: "h-[350px] flex items-center px-6 pt-8 pb-12",
};

export const GroupValue = GroupValuesTemplate.bind({});
GroupValue.args = {
  modelValue: "",
  groupValueKey: "libs",
  groupLabelKey: "language",
  labelKey: "name",
  valueKey: "name",
  options: [
    {
      language: "Javascript",
      libs: [{ name: "Vue.js" }, { name: "Adonis" }],
    },
    {
      language: "Ruby",
      libs: [
        { name: "Frameworks", isSubGroup: true, level: 2 },
        { name: "Rails", level: 3 },
        { name: "Sinatra", level: 3 },
      ],
    },
    {
      language: "Other",
      libs: [{ name: "Laravel" }, { name: "Phoenix" }],
    },
  ],
};
GroupValue.parameters = {
  docs: {
    story: {
      height: "400px",
    },
  },
};

export const OptionsLimit = DefaultTemplate.bind({});
OptionsLimit.args = { optionsLimit: 2 };
OptionsLimit.parameters = {
  docs: {
    description: {
      story: "`optionsLimit` prop controls the number of options displayed in the dropdown.",
    },
  },
};

export const VisibleOptions = DefaultTemplate.bind({});
VisibleOptions.args = { visibleOptions: 2 };
VisibleOptions.parameters = {
  docs: {
    description: {
      story: "`visibleOptions` prop controls the number of options you can see without a scroll.",
    },
  },
};

export const Color = EnumTemplate.bind({});
Color.args = { enum: "color", label: "{enumValue}" };

export const UnderlineVariants: StoryFn<EnumUDropdownLinkArgs> = (args: EnumUDropdownLinkArgs) => ({
  components: { UDropdownLink, URow },
  setup: () => ({ args, slots: getSlotNames(UDropdownLink.__name) }),
  template: `
    <URow>
      <UDropdownLink label="Default" />
      <UDropdownLink label="Dashed" dashed underlined />
      <UDropdownLink label="Dotted" dotted underlined />
      <UDropdownLink label="Underlined" underlined />
      <UDropdownLink label="Without Underline" :underlined="false" />
    </URow>
  `,
});

export const WithoutToggleIcon = Default.bind({});
WithoutToggleIcon.args = { toggleIcon: false };

export const CustomToggleIcon = DefaultTemplate.bind({});
CustomToggleIcon.args = { toggleIcon: "expand_circle_down" };

export const DefaultSlot = DefaultTemplate.bind({});
DefaultSlot.args = {
  toggleIcon: false,
  slotTemplate: `
    <template #default="{ opened }">
      <UAvatar
        rounded="full"
        src="https://i.pravatar.cc/300"
        :class="{ 'outline-medium outline-primary': opened }"
      />
    </template>
  `,
};
DefaultSlot.parameters = {
  docs: {
    story: {
      height: "220px",
    },
  },
};

export const LeftSlot = DefaultTemplate.bind({});
LeftSlot.args = {
  slotTemplate: `
    <template #left>
      <UIcon name="person" size="xs" color="primary" class="mr-0.5" />
    </template>
  `,
};

export const ToggleSlot = DefaultTemplate.bind({});
ToggleSlot.args = {
  slotTemplate: `
    <template #toggle="{ opened, toggle }">
      <UAvatar
        src="https://i.pravatar.cc/300"
        size="3xs"
        rounded="full"
        :class="{ 'outline-medium outline-primary': opened }"
        class="ml-1 cursor-pointer"
      />
    </template>
  `,
};

export const EmptySlot = DefaultTemplate.bind({});
EmptySlot.args = {
  options: [],
  slotTemplate: `
    <template #empty>
      <URow align="center">
        <ULoader loading size="sm" />
        <UText label="Loading, this may take a while..." />
      </URow>
    </template>
  `,
};

export const OptionSlots: StoryFn<DefaultUDropdownLinkArgs> = (args) => ({
  components: { UDropdownLink, URow, UCol, UAvatar, UIcon, UBadge, UText },
  setup: () => ({ args, johnDoe, emilyDavis, alexJohnson, patMorgan }),
  template: `
    <URow>
      <UDropdownLink
        v-model="args.beforeOptionModel"
        label="Before option slot"
        :options="[
          {
            label: 'John Doe',
            value: '1',
            role: 'Developer',
            avatar: johnDoe,
            status: 'online',
            statusColor: 'success',
          },
          {
            label: 'Jane Smith',
            value: '2',
            role: 'Designer',
            avatar: emilyDavis,
            status: 'away',
            statusColor: 'warning',
          },
          {
            label: 'Mike Johnson',
            value: '3',
            role: 'Product Manager',
            avatar: alexJohnson,
            status: 'offline',
            statusColor: 'grayscale',
          },
          {
            label: 'Sarah Wilson',
            value: '4',
            role: 'QA Engineer',
            avatar: patMorgan,
            status: 'online',
            statusColor: 'success',
          },
        ]"
      >
        <template #before-option="{ option }">
          <UAvatar :src="option.avatar" size="xs" />
        </template>
      </UDropdownLink>

      <UDropdownLink
        v-model="args.optionModel"
        label="Option slot"
        :options="[
          {
            label: 'John Doe',
            value: '1',
            role: 'Developer',
            avatar: johnDoe,
            status: 'online',
            statusColor: 'success',
          },
          {
            label: 'Jane Smith',
            value: '2',
            role: 'Designer',
            avatar: emilyDavis,
            status: 'away',
            statusColor: 'warning',
          },
          {
            label: 'Mike Johnson',
            value: '3',
            role: 'Product Manager',
            avatar: alexJohnson,
            status: 'offline',
            statusColor: 'grayscale',
          },
          {
            label: 'Sarah Wilson',
            value: '4',
            role: 'QA Engineer',
            avatar: patMorgan,
            status: 'online',
            statusColor: 'success',
          },
        ]"
      >
        <template #option="{ option }">
          <URow justify="between" align="center" gap="xs" block>
            <UCol gap="none">
              <UText size="sm">{{ option.label }}</UText>
              <UText variant="lifted" size="xs">{{ option.role }}</UText>
            </UCol>
            <UBadge
              :label="option.status"
              :color="option.statusColor"
              size="sm"
              variant="subtle"
            />
          </URow>
        </template>
      </UDropdownLink>

      <UDropdownLink
        v-model="args.afterOptionModel"
        label="After option slot"
        :options="[
          { label: 'John Doe', value: '1', verified: true },
          { label: 'Jane Smith', value: '2', verified: true },
          { label: 'Mike Johnson', value: '3', verified: false },
        ]"
      >
        <template #after-option="{ option }">
          <UIcon v-if="option.verified" name="verified" size="xs" color="success" />
        </template>
      </UDropdownLink>
    </URow>
  `,
});
OptionSlots.parameters = {
  docs: {
    story: {
      height: "300px",
    },
  },
};
