import cx from 'classnames';
import { useCallback, useEffect, useRef, useState } from 'react';
import { NavLink, useLocation } from 'react-router-dom';
import { Display } from 'src/components';
import styles from './SettingsMenu.module.scss';

type MenuItem = {
  text: string;
  link: string;
  icon: string;
};

const links: Array<MenuItem[]> = [
  [
    {
      text: 'Keys',
      link: './keys',
      icon: '๐Ÿ—',
    },
  ],
  [
    {
      text: 'Drive',
      link: '.',
      icon: '๐ŸŸฅ',
    },
  ],
  [
    {
      text: 'Signer',
      link: './signer',
      icon: '๐Ÿ–‹๏ธ',
    },
  ],
  [
    {
      text: 'Tokens',
      link: './tokens',
      icon: '๐ŸŸข',
    },
    {
      text: 'Networks',
      link: './networks',
      icon: '๐ŸŒ',
    },
    {
      text: 'Channels',
      link: './channels',
      icon: '๐Ÿ“ก',
    },
  ],
  // [
  //   {
  //     text: 'Audio',
  //     link: './audio',
  //     icon: '๐Ÿ”Š',
  //   },
  // ],
  [{ text: 'Hotkeys', link: './hotkeys', icon: 'โŒจ๏ธ' }],
  [{ text: 'LLM', link: './llm', icon: '๐Ÿ‘พ' }],
].filter(Boolean);

function SettingsMenu() {
  const [expanded, setExpanded] = useState(false);
  const wrapperRef = useRef<HTMLDivElement>(null);
  const location = useLocation();

  // collapse on route change (covers browser back/forward)
  useEffect(() => {
    setExpanded(false);
  }, [location.pathname]);

  // collapse on any click outside the menu
  useEffect(() => {
    if (!expanded) return;

    const handleOutside = (e: MouseEvent) => {
      if (wrapperRef.current && !wrapperRef.current.contains(e.target as Node)) {
        setExpanded(false);
      }
    };

    document.addEventListener('click', handleOutside);
    return () => document.removeEventListener('click', handleOutside);
  }, [expanded]);

  const handleToggle = useCallback(() => {
    setExpanded((prev) => !prev);
  }, []);

  const handleItemClick = useCallback((e: React.MouseEvent) => {
    e.stopPropagation();
    setExpanded(false);
  }, []);

  return (
    <div
      ref={wrapperRef}
      className={cx(styles.wrapper, { [styles.expanded]: expanded })}
      onClick={handleToggle}
    >
      <Display>
        <div className={styles.links}>
          {links.map((link, indexUl) => (
            <ul key={indexUl}>
              {link.map((item, index) => (
                <li key={index} onClick={handleItemClick}>
                  <NavLink
                    className={({ isActive }) =>
                      cx({
                        [styles.active]: isActive,
                      })
                    }
                    to={item.link}
                    end
                  >
                    <span className={styles.icon}>{item.icon}</span>
                    <span className={styles.text}>{item.text}</span>
                  </NavLink>
                </li>
              ))}
            </ul>
          ))}
        </div>
      </Display>
    </div>
  );
}

export default SettingsMenu;

Neighbours