import { useRosettaColors } from "@/app/hooks/useRosettaColors";
import { OnlineState } from "@/app/providers/ProtocolProvider/protocol/packets/packet.onlinestate";
import { usePublicKey } from "@/app/providers/AccountProvider/usePublicKey";
import { useDialog } from "@/app/providers/DialogProvider/useDialog";
import { useUserInformation } from "@/app/providers/InformationProvider/useUserInformation";
import { ProtocolState } from "@/app/providers/ProtocolProvider/ProtocolProvider";
import { useProtocolState } from "@/app/providers/ProtocolProvider/useProtocolState";
import { Avatar, Box, Divider, Flex, Loader, Text, useComputedColorScheme, useMantineTheme } from "@mantine/core";
import { modals } from "@mantine/modals";
import { IconBookmark, IconPhone, IconTrashX } from "@tabler/icons-react";
import { useEffect, useRef, useState } from "react";
import { useNavigate } from "react-router-dom";
import { VerifiedBadge } from "../VerifiedBadge/VerifiedBadge";
import { usePacket } from "@/app/providers/ProtocolProvider/usePacket";
import { PacketTyping } from "@/app/providers/ProtocolProvider/protocol/packets/packet.typeing";
import { useAvatars } from "@/app/providers/AvatarProvider/useAvatars";
import { useReplyMessages } from "@/app/providers/DialogProvider/useReplyMessages";
import { ReplyHeader } from "../ReplyHeader/ReplyHeader";
import { useRosettaBreakpoints } from "@/app/hooks/useRosettaBreakpoints";
import { BackToDialogs } from "../BackToDialogs/BackToDialogs";
import { useSystemAccounts } from "@/app/providers/SystemAccountsProvider/useSystemAccounts";
import { useCalls } from "@/app/providers/CallProvider/useCalls";


export function ChatHeader() {
    const colors = useRosettaColors();
    const computedTheme = useComputedColorScheme();
    const navigate = useNavigate();
    const publicKey = usePublicKey();
    const {deleteMessages, dialog} = useDialog();
    const theme = useMantineTheme(); 
    const [opponent, ___, forceUpdateUserInformation] = useUserInformation(dialog);
    const [protocolState] = useProtocolState();
    const [userTypeing, setUserTypeing] = useState(false);
    const timeoutRef = useRef<NodeJS.Timeout>(undefined);
    const avatars = useAvatars(dialog);
    const {replyMessages} = useReplyMessages();
    const {lg} = useRosettaBreakpoints();
    const systemAccounts = useSystemAccounts();
    const isSystemAccount = systemAccounts.find((acc) => acc.publicKey == dialog) != undefined;
    const {call} = useCalls();

    
    useEffect(() => {
        forceUpdateUserInformation();
        setUserTypeing(false);
        clearTimeout(timeoutRef.current);
    }, [dialog]);

    usePacket(0x0B, (packet : PacketTyping) => {
        if(packet.getFromPublicKey() == dialog && packet.getToPublicKey() == publicKey){
            setUserTypeing(true);
            clearTimeout(timeoutRef.current);
            timeoutRef.current = setTimeout(() => {
                setUserTypeing(false);
            }, 3000);
        }
    }, [dialog]);

    const clearMessages = async () => {
        deleteMessages();
        modals.closeAll();
    }

    const onClickClearMessages = () => {
        modals.openConfirmModal({
            title: 'Clear all messages?',
            centered: true,
            children: (
            <Text size="sm">
                Are you sure you want to clear all messages? This action cannot be undone.
            </Text>
            ),
            withCloseButton: false,
            labels: { confirm: 'Continue', cancel: "Cancel" },
            confirmProps: { color: 'red' },
            onConfirm: clearMessages
        });
    }

    const onClickProfile = () => {
        if(opponent.publicKey != "DELETED" && opponent.publicKey != publicKey){
            navigate("/main/profile/" + opponent.publicKey);
        }
    }


    return (<>
        <Box bg={colors.boxColor} style={{
            userSelect: 'none',
        }} h={60}>
            {(replyMessages.messages.length <= 0 || replyMessages.inDialogInput) && <Flex p={'sm'} h={'100%'} justify={'space-between'} align={'center'} gap={'sm'}>
                <Flex style={{
                    cursor: 'pointer'
                }} h={'100%'} align={'center'} gap={'sm'}>
                    {!lg && <BackToDialogs></BackToDialogs>}
                    {
                        publicKey == opponent.publicKey ?  <Avatar
                        size={'md'}
                        color={'blue'}
                        variant={'filled'}
                        onClick={onClickProfile}
                    >
                        <IconBookmark stroke={2} size={20}></IconBookmark>
                    </Avatar> : <Avatar onClick={onClickProfile} bg={avatars.length > 0 ? '#fff' : undefined} color={'initials'} src={avatars.length > 0 ? avatars[0].avatar : undefined} name={opponent.title}></Avatar>
                    }
                    <Flex direction={'column'} onClick={onClickProfile}>
                        <Flex align={'center'} gap={3}>
                            <Text size={'sm'} c={computedTheme == 'light' ? 'black' : 'white'} fw={500}>
                                {(
                                    publicKey == opponent.publicKey ? "Saved messages" : opponent.title
                                )}
                            </Text>
                            {(opponent.verified > 0 && publicKey != opponent.publicKey) && <VerifiedBadge size={17} verified={opponent.verified}></VerifiedBadge>}
                        </Flex>
                        {(publicKey != opponent.publicKey && protocolState == ProtocolState.CONNECTED && !userTypeing) && <>
                            {(
                                opponent.online == OnlineState.ONLINE ? 
                                <Text c={theme.colors.green[7]} fz={12}>online</Text> :
                                <Text c={theme.colors.gray[5]} fz={12}>{isSystemAccount ? 'official account' : 'offline'}</Text>
                            )}
                        </>}
                        {userTypeing && publicKey != opponent.publicKey && protocolState == ProtocolState.CONNECTED && <>
                            <Flex gap={5} align={'center'}>
                                <Text c={theme.colors.blue[3]} fz={12}>typing </Text>
                                <Loader size={15} color={theme.colors.blue[3]} type={'dots'}></Loader>
                            </Flex>
                        </>}
                        {protocolState != ProtocolState.CONNECTED && 
                            <Flex gap={'xs'} align={'center'}>
                                <Loader size={8} color={colors.chevrons.active}></Loader>
                                <Text c={theme.colors.gray[5]} fz={12}>connecting...</Text>
                            </Flex>
                        }
                    </Flex>
                </Flex>              
                <Flex h={'100%'} align={'center'} gap={'sm'}>
                    {publicKey != opponent.publicKey && !isSystemAccount && (
                        <IconPhone 
                        onClick={() => call(dialog)}
                        style={{
                            cursor: 'pointer'
                        }} stroke={1.5} color={theme.colors.blue[7]} size={24}></IconPhone>
                    )}
                    <IconTrashX
                        onClick={onClickClearMessages}
                        style={{
                            cursor: 'pointer'
                        }} stroke={1.5} color={theme.colors.blue[7]} size={24}></IconTrashX>
                </Flex>
            </Flex>}
            {replyMessages.messages.length > 0 && !replyMessages.inDialogInput && <ReplyHeader></ReplyHeader>}
        </Box>
        <Divider color={colors.borderColor}></Divider>
    </>)
}