import { Attachment, AttachmentType } from "@/app/providers/ProtocolProvider/protocol/packets/packet.message";
import { DeliveredMessageState } from "@/app/providers/DialogProvider/DialogProvider";
import { Flex } from "@mantine/core";
import { MessageImage } from "./MessageImage";
import { MessageReplyMessages } from "./MessageReplyMessages";
import { MessageFile } from "./MessageFile";
import { ErrorBoundaryProvider } from "@/app/providers/ErrorBoundaryProvider/ErrorBoundaryProvider";
import { AttachmentError } from "../AttachmentError/AttachmentError";
import { MessageAvatar } from "./MessageAvatar";
import { MessageProps } from "../Messages/Message";
import { MessageCall } from "./MessageCall";

export interface MessageAttachmentsProps {
    attachments: Attachment[];
    delivered: DeliveredMessageState;
    timestamp: number;
    text: string;
    chacha_key_plain: string;
    parent: MessageProps;
}

export interface AttachmentProps {
    attachment: Attachment;
    attachments: Attachment[];
    delivered: DeliveredMessageState;
    timestamp: number;
    text: string;
    chacha_key_plain: string;
    parent: MessageProps;
}

export function MessageAttachments(props: MessageAttachmentsProps) {
    return (
        <ErrorBoundaryProvider fallback={<AttachmentError></AttachmentError>}>
            <Flex gap={'xs'} direction={'column'} mt={'sm'} wrap={'wrap'}>
                {props.attachments.map((att, index) => {
                    const attachProps : AttachmentProps = {
                        chacha_key_plain: props.chacha_key_plain,
                        attachment: att,
                        attachments: props.attachments,
                        delivered: props.delivered,
                        timestamp: props.timestamp,
                        text: props.text,
                        parent: props.parent,
                    }
                    switch (att.type) {
                        case AttachmentType.MESSAGES:
                            return <MessageReplyMessages {...attachProps} key={index}></MessageReplyMessages>
                        case AttachmentType.IMAGE:
                            return <MessageImage {...attachProps} key={index}></MessageImage>
                        case AttachmentType.FILE:
                            return <MessageFile {...attachProps} key={index}></MessageFile>
                        case AttachmentType.AVATAR:
                            return <MessageAvatar {...attachProps} key={index}></MessageAvatar>
                        case AttachmentType.CALL:
                            return <MessageCall {...attachProps} key={index}></MessageCall>
                        default:
                            return <AttachmentError key={index}></AttachmentError>;
                    }
                })}
            </Flex>            
        </ErrorBoundaryProvider>
    );
}