import PropTypes from 'prop-types';
import { useCallback, useState } from 'react';

import { FormattedMessage } from 'react-intl';

import classNames from 'classnames';

import { useDispatch } from 'react-redux';

import AlternateEmailIcon from '@/material-icons/400-24px/alternate_email.svg?react';
import BlockIcon from '@/material-icons/400-24px/block.svg?react';
import CampaignIcon from '@/material-icons/400-24px/campaign.svg?react';
import CollectionsIcon from '@/material-icons/400-24px/category.svg?react';
import ReplyIcon from '@/material-icons/400-24px/reply.svg?react';
import VisibilityOffIcon from '@/material-icons/400-24px/visibility_off.svg?react';
import { blockAccount } from 'mastodon/actions/accounts';
import { closeModal } from 'mastodon/actions/modal';
import { NavigationFocusTarget } from 'mastodon/components/navigation_focus_target';
import { Button } from 'mastodon/components/button';
import { Icon } from 'mastodon/components/icon';

export const BlockModal = ({ accountId, acct }) => {
  const dispatch = useDispatch();
  const [expanded, setExpanded] = useState(false);

  const domain = acct.split('@')[1];

  const handleClick = useCallback(() => {
    dispatch(closeModal({ modalType: undefined, ignoreFocus: false }));
    dispatch(blockAccount(accountId));
  }, [dispatch, accountId]);

  const handleCancel = useCallback(() => {
    dispatch(closeModal({ modalType: undefined, ignoreFocus: false }));
  }, [dispatch]);

  const handleToggleLearnMore = useCallback(() => {
    setExpanded(!expanded);
  }, [expanded, setExpanded]);

  return (
    <div className='modal-root__modal safety-action-modal'>
      <div className='safety-action-modal__top'>
        <div className='safety-action-modal__header'>
          <div className='safety-action-modal__header__icon'>
            <Icon icon={BlockIcon} />
          </div>

          <div>
            <NavigationFocusTarget as='h1'>
              <FormattedMessage id='block_modal.title' defaultMessage='Block user?' />
            </NavigationFocusTarget>
            <p>@{acct}</p>
          </div>
        </div>

        <ul className='safety-action-modal__bullet-points'>
          <li>
            <div className='safety-action-modal__bullet-points__icon'><Icon icon={CampaignIcon} /></div>
            <div><FormattedMessage id='block_modal.they_will_know' defaultMessage="They can see that they're blocked." /></div>
          </li>

          <li>
            <div className='safety-action-modal__bullet-points__icon'><Icon icon={VisibilityOffIcon} /></div>
            <div><FormattedMessage id='block_modal.they_cant_see_posts' defaultMessage="They can't see your content and you won't see theirs." /></div>
          </li>

          <li>
            <div className='safety-action-modal__bullet-points__icon'><Icon icon={AlternateEmailIcon} /></div>
            <div><FormattedMessage id='block_modal.you_wont_see_mentions' defaultMessage="You won't see posts from others that mention them." /></div>
          </li>

          <li>
            <div className='safety-action-modal__bullet-points__icon'><Icon icon={ReplyIcon} /></div>
            <div><FormattedMessage id='block_modal.they_cant_mention' defaultMessage="You can't mention, follow, or quote each other." /></div>
          </li>

          <li>
            <div className='safety-action-modal__bullet-points__icon'><Icon icon={CollectionsIcon} /></div>
            <div><FormattedMessage id='block_modal.no_collections' defaultMessage="Neither of you can add each other to collections. You'll be automatically removed from each others' existing collections, if applicable." /></div>
          </li>
        </ul>
      </div>

      <div className={classNames('safety-action-modal__bottom', { active: expanded })}>
        {domain && (
          <div className='safety-action-modal__bottom__collapsible'>
            <div className='safety-action-modal__caveats'>
              <FormattedMessage
                id='block_modal.remote_users_caveat'
                defaultMessage='We will ask the server {domain} to respect your decision. However, compliance is not guaranteed since some servers may handle blocks differently. Public posts may still be visible to non-logged-in users.'
                values={{ domain: <strong>{domain}</strong> }}
              />
            </div>
          </div>
        )}

        <div className='safety-action-modal__actions'>
          {domain && (
            <button onClick={handleToggleLearnMore} className='link-button'>
              {expanded ? <FormattedMessage id='block_modal.show_less' defaultMessage='Show less' /> : <FormattedMessage id='block_modal.show_more' defaultMessage='Show more' />}
            </button>
          )}

          <div className='spacer' />

          <button onClick={handleCancel} className='link-button'>
            <FormattedMessage id='confirmation_modal.cancel' defaultMessage='Cancel' />
          </button>

          <Button onClick={handleClick} dangerous autoFocus>
            <FormattedMessage id='confirmations.block.confirm' defaultMessage='Block' />
          </Button>
        </div>
      </div>
    </div>
  );
};

BlockModal.propTypes = {
  accountId: PropTypes.string.isRequired,
  acct: PropTypes.string.isRequired,
};

export default BlockModal;
