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

import React, { memo } from 'react';
import { useSelector } from 'react-redux';
import { StoriesSettingsModal } from '../../components/StoriesSettingsModal.dom.tsx';
import {
  getCandidateContactsForNewGroup,
  getConversationByServiceIdSelector,
  getGroupStories,
  getMe,
} from '../selectors/conversations.dom.ts';
import { getAllSignalConnections } from '../selectors/conversations-extra.preload.ts';
import { getDistributionListsWithMembers } from '../selectors/storyDistributionLists.dom.ts';
import { getIntl, getTheme } from '../selectors/user.std.ts';
import { getPreferredBadgeSelector } from '../selectors/badges.preload.ts';
import { getHasStoryViewReceiptSetting } from '../selectors/items.dom.ts';
import { useGlobalModalActions } from '../ducks/globalModals.preload.ts';
import { useStoryDistributionListsActions } from '../ducks/storyDistributionLists.preload.ts';
import { useStoriesActions } from '../ducks/stories.preload.ts';
import { useConversationsActions } from '../ducks/conversations.preload.ts';
import { useItemsActions } from '../ducks/items.preload.ts';

export const SmartStoriesSettingsModal = memo(
  function SmartStoriesSettingsModal() {
    const { setStoriesDisabled } = useStoriesActions();
    const { hideStoriesSettings, toggleSignalConnectionsModal } =
      useGlobalModalActions();
    const {
      allowsRepliesChanged,
      createDistributionList,
      deleteDistributionList,
      hideMyStoriesFrom,
      removeMembersFromDistributionList,
      setMyStoriesToAllSignalConnections,
      updateStoryViewers,
    } = useStoryDistributionListsActions();
    const { toggleGroupsForStorySend } = useConversationsActions();
    const { putItem } = useItemsActions();

    const signalConnections = useSelector(getAllSignalConnections);
    const getPreferredBadge = useSelector(getPreferredBadgeSelector);
    const storyViewReceiptsEnabled = useSelector(getHasStoryViewReceiptSetting);
    const onStoryViewReceiptsChange = (value: boolean) => {
      putItem('storyViewReceiptsEnabled', value);
      const account = window.ConversationController.getOurConversationOrThrow();
      account.captureChange('storyViewReceiptsEnabled');
    };
    const i18n = useSelector(getIntl);
    const me = useSelector(getMe);
    const candidateConversations = useSelector(getCandidateContactsForNewGroup);
    const distributionLists = useSelector(getDistributionListsWithMembers);
    const groupStories = useSelector(getGroupStories);

    const getConversationByServiceId = useSelector(
      getConversationByServiceIdSelector
    );
    const theme = useSelector(getTheme);

    return (
      <StoriesSettingsModal
        candidateConversations={candidateConversations}
        distributionLists={distributionLists}
        groupStories={groupStories}
        signalConnections={signalConnections}
        hideStoriesSettings={hideStoriesSettings}
        getPreferredBadge={getPreferredBadge}
        i18n={i18n}
        me={me}
        getConversationByServiceId={getConversationByServiceId}
        onDeleteList={deleteDistributionList}
        toggleGroupsForStorySend={toggleGroupsForStorySend}
        onDistributionListCreated={createDistributionList}
        onHideMyStoriesFrom={hideMyStoriesFrom}
        onRemoveMembers={removeMembersFromDistributionList}
        onRepliesNReactionsChanged={allowsRepliesChanged}
        onViewersUpdated={updateStoryViewers}
        onStoryViewReceiptsChange={onStoryViewReceiptsChange}
        setMyStoriesToAllSignalConnections={setMyStoriesToAllSignalConnections}
        storyViewReceiptsEnabled={storyViewReceiptsEnabled}
        theme={theme}
        toggleSignalConnectionsModal={toggleSignalConnectionsModal}
        setStoriesDisabled={setStoriesDisabled}
      />
    );
  }
);
