/**
 * ChatScreen 类型定义
 */

import { MessageResponse, UserDTO, GroupMemberResponse, GroupResponse } from '../../../../types/dto';

// 面板类型
export type PanelType = 'none' | 'emoji' | 'more' | 'mention';

// 消息状态
export type MessageStatus = 'normal' | 'recalled' | 'deleted';

// 用户角色
export type UserRole = 'owner' | 'admin' | 'member';

// 扩展消息类型以支持群聊
export interface GroupMessage extends MessageResponse {
  sender?: UserDTO;
  mention_users?: string[];
  mention_all?: boolean;
  is_system_notice?: boolean;
  notice_content?: string;
  // category 继承自 MessageResponse，用于判断消息类别
  // 'chat' - 普通聊天消息
  // 'notification' - 通知类消息（如禁言通知）
  // 'announcement' - 系统公告
}

// 路由参数
export interface ChatRouteParams {
  conversationId?: string;
  userId?: string;
  isGroupChat?: boolean;
  groupId?: string;
  groupName?: string;
}

// 发送者信息
export interface SenderInfo {
  nickname: string;
  avatar: string | null;
  userId?: string;
}

// 更多功能项
export interface MoreAction {
  id: string;
  icon: string;
  name: string;
  color: string;
}

// 长按菜单项
export interface MenuItem {
  id: string;
  label: string;
  icon: string;
  color?: string;
  onPress: () => void;
}

// ChatScreen Props
export interface ChatScreenProps {
  // 路由参数通过 useRoute 获取
}

// 消息气泡 Props
export interface MessageBubbleProps {
  message: GroupMessage;
  index: number;
  currentUserId: string;
  currentUser: { id?: string; nickname?: string; avatar?: string | null } | null;
  otherUser: { id?: string; nickname?: string; avatar?: string | null } | null;
  isGroupChat: boolean;
  groupMembers: GroupMemberResponse[];
  otherUserLastReadSeq: number;
  selectedMessageId: string | null;
  // 消息映射，用于查找被回复的消息
  messageMap?: Map<string, GroupMessage>;
  onLongPress: (message: GroupMessage, position?: MenuPosition) => void;
  onAvatarPress: (userId: string) => void;
  onAvatarLongPress: (senderId: string, nickname: string) => void;
  formatTime: (dateString: string) => string;
  shouldShowTime: (index: number) => boolean;
  // 图片点击回调
  onImagePress?: (images: { id: string; url: string; thumbnail_url?: string; width?: number; height?: number }[], index: number) => void;
  // 滑动回复回调
  onReply?: (message: GroupMessage) => void;
  // 点击回复预览回调（定位到原消息）
  onReplyPress?: (messageId: string) => void;
}

/** 输入框中待发送的本地图片（未上传） */
export interface PendingChatAttachment {
  id: string;
  uri: string;
  mimeType?: string;
}

// 输入框 Props
export interface ChatInputProps {
  inputText: string;
  onInputChange: (text: string) => void;
  onSend: () => void;
  onToggleEmoji: () => void;
  onToggleMore: () => void;
  activePanel: PanelType;
  /** 发送消息或上传附件进行中，用于禁用发送按钮 */
  isComposerBusy: boolean;
  isMuted: boolean;
  isGroupChat: boolean;
  muteAll: boolean;
  replyingTo: GroupMessage | null;
  onCancelReply: () => void;
  onFocus: () => void;
  restrictionHint?: string | null;
  disableMore?: boolean;
  pendingAttachments?: PendingChatAttachment[];
  onRemovePendingAttachment?: (id: string) => void;
}

// 表情面板 Props
export interface EmojiPanelProps {
  onInsertEmoji: (emoji: string) => void;
  onClose: () => void;
}

// 更多功能面板 Props
export interface MorePanelProps {
  onAction: (actionId: string) => void;
  disabledActionIds?: string[];
}

// @成员选择面板 Props
export interface MentionPanelProps {
  members: GroupMemberResponse[];
  currentUserId: string;
  mentionQuery: string;
  currentUserRole: UserRole;
  onSelectMention: (member: GroupMemberResponse) => void;
  onMentionAll: () => void;
  onClose: () => void;
}

// 菜单位置信息
export interface MenuPosition {
  x: number;
  y: number;
  width: number;
  height: number;
  pressX?: number;  // 按压点X坐标
  pressY?: number;  // 按压点Y坐标
}

// 长按菜单 Props
export interface LongPressMenuProps {
  visible: boolean;
  message: GroupMessage | null;
  currentUserId: string;
  position?: MenuPosition;
  onClose: () => void;
  onReply: (message: GroupMessage) => void;
  onRecall: (messageId: string) => void;
  onDelete: (messageId: string) => void;
  onAddSticker?: (imageUrl: string) => void;
}

// 聊天头部 Props
export interface ChatHeaderProps {
  isGroupChat: boolean;
  groupInfo: { name?: string; member_count?: number } | null;
  otherUser: { id?: string; nickname?: string; avatar?: string | null } | null;
  routeGroupName?: string;
  typingHint: string | null;
  onBack: () => void;
  onTitlePress: () => void;
  onMorePress: () => void;
  /** 大屏幕下显示群信息面板的回调 */
  onGroupInfoPress?: () => void;
  /** 是否为大屏幕模式 */
  isWideScreen?: boolean;
}

// 回复预览 Props
export interface ReplyPreviewProps {
  replyingTo: GroupMessage | null;
  currentUserId: string;
  currentUser: UserDTO | null;
  otherUser: UserDTO | null;
  isGroupChat: boolean;
  getSenderInfo: (senderId: string) => SenderInfo;
  onCancel: () => void;
}

// 可滑动消息气泡 Props
export interface SwipeableMessageBubbleProps {
  children: React.ReactNode;
  isMe: boolean;
  onReply: () => void;
  enabled?: boolean;
}

// ChatScreen 状态接口
export interface ChatScreenState {
  // 基础状态
  messages: GroupMessage[];
  conversationId: string | null;
  inputText: string;
  otherUser: UserDTO | null;
  currentUser: UserDTO | null;
  keyboardHeight: number;
  loading: boolean;
  sending: boolean;
  currentUserId: string;
  lastSeq: number;
  otherUserLastReadSeq: number;
  activePanel: PanelType;
  sendingImage: boolean;
  
  // 回复消息状态
  replyingTo: GroupMessage | null;
  
  // 长按菜单状态
  longPressMenuVisible: boolean;
  selectedMessage: GroupMessage | null;
  
  // 群聊相关状态
  groupInfo: GroupResponse | null;
  groupMembers: GroupMemberResponse[];
  typingUsers: string[];
  currentUserRole: UserRole;
  mentionQuery: string;
  selectedMentions: string[];
  mentionAll: boolean;
  isMuted: boolean;
  muteAll: boolean;
}
