import { Global, css } from '@emotion/react';
import React from 'react';

import {
  ContextMenuItem,
  ContextMenuTrigger,
} from '@/components/contextMenu/ContextMenu';
import { PlusIcon } from '@/icons';

import { ActionButton } from './ActionButton';

// Custom upload icon
const CustomUploadIcon = () => (
  <svg
    width='16'
    height='16'
    viewBox='0 0 16 16'
    fill='none'
    xmlns='http://www.w3.org/2000/svg'
  >
    <path
      d='M4.49992 14C4.17909 14 3.90443 13.8825 3.67596 13.6475C3.44749 13.4125 3.33325 13.13 3.33325 12.8V3.2C3.33325 2.87 3.44749 2.5875 3.67596 2.3525C3.90443 2.1175 4.17909 2 4.49992 2H8.68534C8.84089 2 8.98916 2.03 9.13013 2.09C9.2711 2.15 9.39506 2.235 9.502 2.345L12.3312 5.255C12.4381 5.365 12.5208 5.4925 12.5791 5.6375C12.6374 5.7825 12.6666 5.935 12.6666 6.095V12.8C12.6666 13.13 12.5524 13.4125 12.3239 13.6475C12.0954 13.8825 11.8208 14 11.4999 14H4.49992ZM8.58325 5.6C8.58325 5.77 8.63916 5.9125 8.75096 6.0275C8.86277 6.1425 9.00131 6.2 9.16659 6.2H11.4999L8.58325 3.2V5.6ZM7.27075 12.2C7.6402 12.2 7.95131 12.07 8.20409 11.81C8.45686 11.55 8.58325 11.23 8.58325 10.85V8.6H9.74992C9.9152 8.6 10.0537 8.5425 10.1655 8.4275C10.2773 8.3125 10.3333 8.17 10.3333 8C10.3333 7.83 10.2773 7.6875 10.1655 7.5725C10.0537 7.4575 9.9152 7.4 9.74992 7.4H8.58325C8.41797 7.4 8.27943 7.4575 8.16763 7.5725C8.05582 7.6875 7.99992 7.83 7.99992 8V9.725C7.89297 9.645 7.77874 9.5875 7.65721 9.5525C7.53568 9.5175 7.40686 9.5 7.27075 9.5C6.90131 9.5 6.5902 9.63 6.33742 9.89C6.08464 10.15 5.95825 10.47 5.95825 10.85C5.95825 11.23 6.08464 11.55 6.33742 11.81C6.5902 12.07 6.90131 12.2 7.27075 12.2Z'
      fill='#FAF7F5'
    />
  </svg>
);

// Custom record icon
const RecordIcon = () => (
  <svg
    width='16'
    height='16'
    viewBox='0 0 16 16'
    fill='none'
    xmlns='http://www.w3.org/2000/svg'
  >
    <path
      d='M7.99992 11.3333C8.92214 11.3333 9.70825 11.0083 10.3583 10.3583C11.0083 9.70831 11.3333 8.9222 11.3333 7.99998C11.3333 7.07776 11.0083 6.29165 10.3583 5.64165C9.70825 4.99165 8.92214 4.66665 7.99992 4.66665C7.0777 4.66665 6.29159 4.99165 5.64159 5.64165C4.99159 6.29165 4.66659 7.07776 4.66659 7.99998C4.66659 8.9222 4.99159 9.70831 5.64159 10.3583C6.29159 11.0083 7.0777 11.3333 7.99992 11.3333ZM7.99992 14.6666C7.0777 14.6666 6.21103 14.4916 5.39992 14.1416C4.58881 13.7916 3.88325 13.3166 3.28325 12.7166C2.68325 12.1166 2.20825 11.4111 1.85825 10.6C1.50825 9.78887 1.33325 8.9222 1.33325 7.99998C1.33325 7.07776 1.50825 6.21109 1.85825 5.39998C2.20825 4.58887 2.68325 3.88331 3.28325 3.28331C3.88325 2.68331 4.58881 2.20831 5.39992 1.85831C6.21103 1.50831 7.0777 1.33331 7.99992 1.33331C8.92214 1.33331 9.78881 1.50831 10.5999 1.85831C11.411 2.20831 12.1166 2.68331 12.7166 3.28331C13.3166 3.88331 13.7916 4.58887 14.1416 5.39998C14.4916 6.21109 14.6666 7.07776 14.6666 7.99998C14.6666 8.9222 14.4916 9.78887 14.1416 10.6C13.7916 11.4111 13.3166 12.1166 12.7166 12.7166C12.1166 13.3166 11.411 13.7916 10.5999 14.1416C9.78881 14.4916 8.92214 14.6666 7.99992 14.6666ZM7.99992 13.3333C9.48881 13.3333 10.7499 12.8166 11.7833 11.7833C12.8166 10.75 13.3333 9.48887 13.3333 7.99998C13.3333 6.51109 12.8166 5.24998 11.7833 4.21665C10.7499 3.18331 9.48881 2.66665 7.99992 2.66665C6.51103 2.66665 5.24992 3.18331 4.21659 4.21665C3.18325 5.24998 2.66659 6.51109 2.66659 7.99998C2.66659 9.48887 3.18325 10.75 4.21659 11.7833C5.24992 12.8166 6.51103 13.3333 7.99992 13.3333Z'
      fill='#FAF7F5'
    />
  </svg>
);

const contextMenuStyles = css`
  /* Target the VisibleWrapper with high specificity */
  div[data-context-menu] > div {
    background-color: #252020 !important;
    border: 1px solid rgba(255, 255, 255, 0.1) !important;
  }

  /* Target by class name pattern for additional coverage */
  [class*='VisibleWrapper'] {
    background-color: #252020 !important;
    border: 1px solid rgba(255, 255, 255, 0.1) !important;
  }
`;

interface ActionButtonMenuProps {
  onUpload: () => void;
  onRecord: () => void;
}

export const ActionButtonMenu: React.FC<ActionButtonMenuProps> = ({
  onUpload,
  onRecord,
}) => {
  return (
    <>
      <Global styles={contextMenuStyles} />
      <ContextMenuTrigger
        placement='top-right'
        ButtonComponent={(props) => <ActionButton icon={PlusIcon} {...props} />}
        ContentsComponent={({ onClose }: { onClose: () => void }) => (
          <>
            <ContextMenuItem
              onClick={() => {
                onRecord();
                onClose();
              }}
              icon={<RecordIcon />}
            >
              Record audio
            </ContextMenuItem>
            <ContextMenuItem
              onClick={() => {
                onUpload();
                onClose();
              }}
              icon={<CustomUploadIcon />}
            >
              Upload file
            </ContextMenuItem>
          </>
        )}
      />
    </>
  );
};
