import * as RadixDialog from '@radix-ui/react-dialog';
import { Cross1Icon } from '@radix-ui/react-icons';
import { Flex, Theme } from '@radix-ui/themes';
import { usePortalContainer } from '@/theme/portal-provider';
import { Heading } from './heading';
import { IconButton } from './icon-button';
import { ToastContainer } from 'react-toastify';

type ContentProps = RadixDialog.DialogContentProps & {
  width?: string;
  maxWidth?: string;
  minWidth?: string;
  height?: string;
  maxHeight?: string;
  minHeight?: string;
  title?: string;
  hideCloseButton?: boolean;
  buttons?: React.ReactNode;
};

const preventDefault: RadixDialog.DialogContentProps['onPointerDownOutside'] = (
  e
) => {
  e.preventDefault();
};

function Content(props: ContentProps): React.ReactNode {
  const {
    children,
    style = {},
    width,
    maxWidth,
    minWidth,
    maxHeight,
    height,
    minHeight,
    title,
    buttons,
    hideCloseButton,
    className,
    ...rest
  } = props;
  const { dialogContainerRef } = usePortalContainer();

  return (
    <RadixDialog.Portal container={dialogContainerRef.current}>
      <Theme asChild>
        <RadixDialog.Overlay className="DialogOverlay">
          <RadixDialog.Content
            className={`DialogContent ${className || ''}`}
            onPointerDownOutside={preventDefault}
            style={{
              width,
              maxWidth,
              minWidth,
              maxHeight,
              height,
              minHeight,
              ...style,
            }}
            {...rest}
          >
            <Flex gap="2" justify="between" width="100%">
              <RadixDialog.Title asChild>
                {typeof title === 'string' && title !== '' ? (
                  <Heading mb="2" variant="heading3">
                    {title}
                  </Heading>
                ) : (
                  title
                )}
              </RadixDialog.Title>
              {!hideCloseButton && (
                <RadixDialog.Close asChild className="DialogClose">
                  <IconButton color="gray" variant="ghost">
                    <Cross1Icon />
                  </IconButton>
                </RadixDialog.Close>
              )}
            </Flex>
            {children}
            {buttons ? (
              <Flex gap="3" justify="end" mt="3" width="100%">
                {buttons}
              </Flex>
            ) : null}
          </RadixDialog.Content> 
        </RadixDialog.Overlay>
      </Theme>
    </RadixDialog.Portal>
  );
}

function Close({
  children,
  ...rest
}: RadixDialog.DialogCloseProps): React.ReactNode {
  return (
    <RadixDialog.Close asChild {...rest}>
      {children}
    </RadixDialog.Close>
  );
}

function Trigger({
  children,
  ...rest
}: RadixDialog.DialogTriggerProps): React.ReactNode {
  return (
    <RadixDialog.Trigger asChild {...rest}>
      {children}
    </RadixDialog.Trigger>
  );
}

export const Dialog = {
  Root: RadixDialog.Root,
  Trigger,
  Content,
  Close,
  Description: RadixDialog.Description,
  Title: RadixDialog.Title,
};
