// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only

import React from 'react';
import lodash from 'lodash';
import { action } from '@storybook/addon-actions';
import type { Meta } from '@storybook/react';
import { getDefaultConversation } from '../../test-helpers/getDefaultConversation.std.ts';
import type { PropsType } from './ContactSpoofingReviewDialog.dom.tsx';
import { ContactSpoofingReviewDialog } from './ContactSpoofingReviewDialog.dom.tsx';
import { ContactSpoofingType } from '../../util/contactSpoofing.std.ts';
import { ThemeType } from '../../types/Util.std.ts';

const { times } = lodash;

const { i18n } = window.SignalContext;

export default {
  title: 'Components/Conversation/ContactSpoofingReviewDialog',
} satisfies Meta<PropsType>;

const getCommonProps = () => ({
  acceptConversation: action('acceptConversation'),
  reportSpam: action('reportSpam'),
  blockAndReportSpam: action('blockAndReportSpam'),
  blockConversation: action('blockConversation'),
  conversationId: 'some-conversation-id',
  deleteConversation: action('deleteConversation'),
  getPreferredBadge: () => undefined,
  groupConversationId: 'convo-id',
  i18n,
  onClose: action('onClose'),
  showContactModal: action('showContactModal'),
  toggleSignalConnectionsModal: action('toggleSignalConnectionsModal'),
  removeMember: action('removeMember'),
  theme: ThemeType.light,
});

export function DirectConversationsWithSameTitle(): React.JSX.Element {
  return (
    <ContactSpoofingReviewDialog
      {...getCommonProps()}
      type={ContactSpoofingType.DirectConversationWithSameTitle}
      possiblyUnsafe={{
        conversation: getDefaultConversation(),
        isSignalConnection: false,
        sharedGroupNames: [],
      }}
      safe={{
        conversation: getDefaultConversation(),
        isSignalConnection: true,
        sharedGroupNames: [],
      }}
    />
  );
}

export function NotAdminMany(): React.JSX.Element {
  return (
    <ContactSpoofingReviewDialog
      {...getCommonProps()}
      type={ContactSpoofingType.MultipleGroupMembersWithSameTitle}
      group={{
        ...getDefaultConversation(),
        areWeAdmin: false,
      }}
      collisionInfoByTitle={{
        Alice: times(2, () => ({
          oldName: 'Alicia',
          isSignalConnection: false,
          conversation: getDefaultConversation({ title: 'Alice' }),
          sharedGroupNames: [],
        })),
        Bob: times(3, () => ({
          isSignalConnection: false,
          conversation: getDefaultConversation({ title: 'Bob' }),
          sharedGroupNames: [],
        })),
        Charlie: times(5, () => ({
          isSignalConnection: false,
          conversation: getDefaultConversation({ title: 'Charlie' }),
          sharedGroupNames: [],
        })),
      }}
    />
  );
}

export function NotAdminOne(): React.JSX.Element {
  return (
    <ContactSpoofingReviewDialog
      {...getCommonProps()}
      type={ContactSpoofingType.MultipleGroupMembersWithSameTitle}
      group={{
        ...getDefaultConversation(),
        areWeAdmin: false,
      }}
      collisionInfoByTitle={{
        Alice: [
          {
            oldName: 'Alicia',
            isSignalConnection: false,
            conversation: getDefaultConversation({ title: 'Alice' }),
            sharedGroupNames: [],
          },
          {
            oldName: 'Alice',
            isSignalConnection: true,
            conversation: getDefaultConversation({ title: 'Alice' }),
            sharedGroupNames: [],
          },
        ],
      }}
    />
  );
}

export function AdminMany(): React.JSX.Element {
  return (
    <ContactSpoofingReviewDialog
      {...getCommonProps()}
      type={ContactSpoofingType.MultipleGroupMembersWithSameTitle}
      group={{
        ...getDefaultConversation(),
        areWeAdmin: true,
      }}
      collisionInfoByTitle={{
        Alice: times(2, () => ({
          oldName: 'Alicia',
          isSignalConnection: false,
          conversation: getDefaultConversation({ title: 'Alice' }),
          sharedGroupNames: [],
        })),
        Bob: times(3, () => ({
          isSignalConnection: false,
          conversation: getDefaultConversation({ title: 'Bob' }),
          sharedGroupNames: [],
        })),
        Charlie: times(5, () => ({
          isSignalConnection: false,
          conversation: getDefaultConversation({ title: 'Charlie' }),
          sharedGroupNames: [],
        })),
      }}
    />
  );
}

export function AdminOne(): React.JSX.Element {
  return (
    <ContactSpoofingReviewDialog
      {...getCommonProps()}
      type={ContactSpoofingType.MultipleGroupMembersWithSameTitle}
      group={{
        ...getDefaultConversation(),
        areWeAdmin: true,
      }}
      collisionInfoByTitle={{
        Alice: [
          {
            oldName: 'Alicia',
            isSignalConnection: false,
            conversation: getDefaultConversation({ title: 'Alice' }),
            sharedGroupNames: [],
          },
          {
            isSignalConnection: true,
            conversation: getDefaultConversation({ title: 'Alice' }),
            sharedGroupNames: [],
          },
        ],
      }}
    />
  );
}
