import { useState, useCallback } from 'react';
import Taro from '@tarojs/taro';
import { View, Text } from '@tarojs/components';
import './index.module.scss';

interface AgentBubbleProps {
  unread?: boolean;
}

/** 在 live 页面和管理端页面不显示气泡 */
const HIDDEN_PAGES = ['pages/live/index'];

const AgentBubble: React.FC<AgentBubbleProps> = ({ unread = false }) => {
  const [pressed, setPressed] = useState(false);

  const handleTap = useCallback(() => {
    Taro.navigateTo({ url: '/pages/live/index' });
  }, []);

  // During render, check current page path
  const getCurrentPath = (): string => {
    try {
      const instance = Taro.getCurrentInstance();
      return instance?.router?.path || '';
    } catch {
      return '';
    }
  };

  const currentPath = getCurrentPath();
  // Normalize: remove leading slash
  const normalizedPath = currentPath.replace(/^\//, '');

  if (normalizedPath.startsWith('pages/admin-') || HIDDEN_PAGES.some((p) => normalizedPath === p || normalizedPath === `/${p}`)) {
    return null;
  }

  return (
    <View
      className={`agent-bubble ${pressed ? 'agent-bubble--pressed' : ''}`}
      onClick={handleTap}
      onTouchStart={() => setPressed(true)}
      onTouchEnd={() => setPressed(false)}
    >
      <Text className="agent-bubble__icon">🤖</Text>
      {unread && <View className="agent-bubble__dot" />}
    </View>
  );
};

export default AgentBubble;
