import {
    aesCbcDecrypt,
    aesCbcEncrypt,
    hkdf,
    hmacSha256Sign,
    prependVersion,
    toSerializedPubKey
} from '@crypto'
import { proto } from '@proto'
import {
    CHAIN_KEY_LABEL,
    FUTURE_MESSAGES_MAX,
    MAX_UNUSED_KEYS,
    MESSAGE_KEY_LABEL,
    SIGNAL_MAC_SIZE,
    SIGNAL_VERSION,
    WHISPER_MESSAGE_KEYS_INFO
} from '@signal/constants'
import {
    decodeSignalMessageKey,
    decodeSignalRecvChain,
    decodeSignalSessionSnapshot,
    encodeSignalRecvChain,
    encodeSignalSessionSnapshot
} from '@signal/session/encoding'
import {
    calculateRatchet,
    generateSerializedKeyPair,
    snapshotToRecord
} from '@signal/session/SignalSession'
import type {
    ParsedPreKeySignalMessage,
    ParsedSignalMessage,
    RawSignalRecvChain,
    SignalMessageKey,
    SignalRecvChain,
    SignalSessionRecord
} from '@signal/types'
import { removeAt, uint8Equal, uint8TimingSafeEqual } from '@util/bytes'
import { toError } from '@util/primitives'

const MAX_TRACKED_RECV_CHAINS = 4

export interface DecryptOutcome {
    readonly updatedSession: SignalSessionRecord
    readonly plaintext: Uint8Array
    readonly newSessionInfo: {
        readonly newIdentity: Uint8Array | null
        readonly baseSession: SignalSessionRecord
        readonly usedPreKey: number | null
    } | null
}

function splitMsgKey(index: number, bytes: Uint8Array): SignalMessageKey {
    if (bytes.length < 80) {
        throw new Error('invalid message key length')
    }
    return {
        index,
        cipherKey: bytes.subarray(0, 32),
        macKey: bytes.subarray(32, 64),
        iv: bytes.subarray(64, 80)
    }
}

export function deriveMsgKey(
    index: number,
    chainKey: Uint8Array
): { readonly nextChainKey: Uint8Array; readonly messageKey: SignalMessageKey } {
    return deriveMsgKeyFromChainKey(index, chainKey)
}

export function selectMessageKey(
    chain: SignalRecvChain,
    targetCounter: number
): { readonly messageKey: SignalMessageKey; readonly updatedChain: SignalRecvChain } {
    const delta = targetCounter - chain.nextMsgIndex
    if (delta > FUTURE_MESSAGES_MAX) {
        throw new Error('message too far in future')
    }
    const unused = chain.unusedMsgKeys
    if (delta < 0) {
        const idx = unused.findIndex((entry) => entry.index === targetCounter)
        if (idx === -1) {
            throw new Error('duplicate message')
        }
        const messageKey = decodeSignalMessageKey(unused[idx], `unusedMsgKeys[${idx}]`)
        const nextUnused = removeAt(unused, idx)
        return {
            messageKey,
            updatedChain: {
                ratchetPubKey: chain.ratchetPubKey,
                nextMsgIndex: chain.nextMsgIndex,
                chainKey: chain.chainKey,
                unusedMsgKeys: nextUnused
            }
        }
    }

    let chainKey = chain.chainKey
    const first = deriveMsgKeyFromChainKey(chain.nextMsgIndex, chainKey)
    let currentMessageKey = first.messageKey
    chainKey = first.nextChainKey
    if (delta === 0) {
        return {
            messageKey: currentMessageKey,
            updatedChain: {
                ratchetPubKey: chain.ratchetPubKey,
                nextMsgIndex: targetCounter + 1,
                chainKey,
                unusedMsgKeys: unused
            }
        }
    }
    const nextUnused = unused.slice()

    let overflow = delta + unused.length - MAX_UNUSED_KEYS
    if (overflow > 0) {
        nextUnused.splice(0, overflow)
        overflow -= unused.length
    }
    for (let counter = chain.nextMsgIndex + 1; counter <= targetCounter; counter += 1) {
        if (overflow > 0) {
            overflow -= 1
        } else {
            nextUnused.push({
                index: currentMessageKey.index,
                cipherKey: currentMessageKey.cipherKey,
                macKey: currentMessageKey.macKey,
                iv: currentMessageKey.iv
            })
        }
        const derived = deriveMsgKeyFromChainKey(counter, chainKey)
        currentMessageKey = derived.messageKey
        chainKey = derived.nextChainKey
    }

    return {
        messageKey: currentMessageKey,
        updatedChain: {
            ratchetPubKey: chain.ratchetPubKey,
            nextMsgIndex: targetCounter + 1,
            chainKey,
            unusedMsgKeys: nextUnused
        }
    }
}

function deriveMsgKeyFromChainKey(
    index: number,
    chainKey: Uint8Array
): { readonly nextChainKey: Uint8Array; readonly messageKey: SignalMessageKey } {
    const nextChainRaw = hmacSha256Sign(chainKey, CHAIN_KEY_LABEL)
    const messageInputKey = hmacSha256Sign(chainKey, MESSAGE_KEY_LABEL)
    const nextChainKey = nextChainRaw.subarray(0, 32)
    const expanded = hkdf(messageInputKey, null, WHISPER_MESSAGE_KEYS_INFO, 80)
    return {
        nextChainKey,
        messageKey: splitMsgKey(index, expanded)
    }
}

export function encryptMsg(
    session: SignalSessionRecord,
    plaintext: Uint8Array
): readonly [SignalSessionRecord, { type: 'msg' | 'pkmsg'; ciphertext: Uint8Array }] {
    const { nextChainKey, messageKey } = deriveMsgKey(
        session.sendChain.nextMsgIndex,
        session.sendChain.chainKey
    )
    const ciphertext = aesCbcEncrypt(messageKey.cipherKey, messageKey.iv, plaintext)

    const signalPayload = proto.SignalMessage.encode({
        ratchetKey: session.sendChain.ratchetKey.pubKey,
        counter: messageKey.index,
        previousCounter: session.prevSendChainHighestIndex,
        ciphertext
    }).finish()
    const versionedLength = 1 + signalPayload.length
    const signalMessage = new Uint8Array(versionedLength + SIGNAL_MAC_SIZE)
    signalMessage[0] = ((SIGNAL_VERSION << 4) | SIGNAL_VERSION) & 0xff
    signalMessage.set(signalPayload, 1)
    const mac = hmacSha256Sign(messageKey.macKey, [
        session.local.pubKey,
        session.remote.pubKey,
        signalMessage.subarray(0, versionedLength)
    ])
    signalMessage.set(mac.subarray(0, SIGNAL_MAC_SIZE), versionedLength)

    let type: 'msg' | 'pkmsg' = 'msg'
    let output: Uint8Array = signalMessage
    if (session.initialExchangeInfo) {
        const preKeyPayload = proto.PreKeySignalMessage.encode({
            registrationId: session.local.regId,
            preKeyId: session.initialExchangeInfo.remoteOneTimeId ?? undefined,
            signedPreKeyId: session.initialExchangeInfo.remoteSignedId,
            baseKey: session.initialExchangeInfo.localOneTimePubKey,
            identityKey: session.local.pubKey,
            message: signalMessage
        }).finish()
        type = 'pkmsg'
        output = prependVersion(preKeyPayload, SIGNAL_VERSION)
    }

    const updated = {
        ...session,
        sendChain: {
            ratchetKey: session.sendChain.ratchetKey,
            nextMsgIndex: messageKey.index + 1,
            chainKey: nextChainKey
        }
    }
    return [updated, { type, ciphertext: output }]
}

export async function decryptMsg(
    session: SignalSessionRecord | null,
    parsed: ParsedSignalMessage,
    onPrevSessionDecryptError?: (error: Error, previousSessionIndex: number) => void
): Promise<DecryptOutcome> {
    if (!session) {
        throw new Error('signal session not found')
    }

    try {
        const [updatedSession, plaintext] = await decryptMsgFromSession(session, parsed)
        return {
            updatedSession,
            plaintext,
            newSessionInfo: null
        }
    } catch (error) {
        for (let i = 0; i < session.prevSessions.length; i += 1) {
            try {
                const decodedPrev = decodeSignalSessionSnapshot(
                    session.prevSessions[i],
                    `prevSessions[${i}]`
                )
                const prevSession = snapshotToRecord(decodedPrev)
                const [updatedPrev, plaintext] = await decryptMsgFromSession(prevSession, parsed)
                const updatedSession = {
                    ...updatedPrev,
                    prevSessions: [
                        encodeSignalSessionSnapshot(session),
                        ...session.prevSessions.slice(0, i),
                        ...session.prevSessions.slice(i + 1)
                    ]
                }
                return {
                    updatedSession,
                    plaintext,
                    newSessionInfo: {
                        newIdentity: uint8Equal(updatedSession.remote.pubKey, session.remote.pubKey)
                            ? null
                            : updatedSession.remote.pubKey,
                        baseSession: prevSession,
                        usedPreKey: null
                    }
                }
            } catch (prevError) {
                onPrevSessionDecryptError?.(toError(prevError), i)
                continue
            }
        }
        throw error
    }
}

export async function decryptMsgFromSession(
    session: SignalSessionRecord,
    message: ParsedSignalMessage | ParsedPreKeySignalMessage
): Promise<readonly [SignalSessionRecord, Uint8Array]> {
    const ratchetPubKey = toSerializedPubKey(message.ratchetPubKey)
    let recvChainIndex = -1
    for (let index = 0; index < session.recvChains.length; index += 1) {
        const key = session.recvChains[index].senderRatchetKey
        if (key !== null && key !== undefined && uint8Equal(key, ratchetPubKey)) {
            recvChainIndex = index
            break
        }
    }
    let selectedMessageKey: SignalMessageKey
    let updatedSession: SignalSessionRecord

    if (recvChainIndex === -1) {
        const recvRatchet = await calculateRatchet(
            session.rootKey,
            session.sendChain.ratchetKey,
            ratchetPubKey
        )
        const freshRecvChain: SignalRecvChain = {
            ratchetPubKey,
            nextMsgIndex: 0,
            chainKey: recvRatchet.chainKey,
            unusedMsgKeys: []
        }
        const selected = selectMessageKey(freshRecvChain, message.counter)
        const newSendRatchet = await generateSerializedKeyPair()
        selectedMessageKey = selected.messageKey

        const sendRatchet = await calculateRatchet(
            recvRatchet.rootKey,
            newSendRatchet,
            ratchetPubKey
        )
        const nextRecvChains: RawSignalRecvChain[] =
            session.recvChains.slice(-MAX_TRACKED_RECV_CHAINS)
        nextRecvChains.push(encodeSignalRecvChain(selected.updatedChain))
        updatedSession = {
            ...session,
            rootKey: sendRatchet.rootKey,
            recvChains: nextRecvChains,
            sendChain: {
                ratchetKey: newSendRatchet,
                nextMsgIndex: 0,
                chainKey: sendRatchet.chainKey
            },
            initialExchangeInfo: null,
            prevSendChainHighestIndex: Math.max(session.sendChain.nextMsgIndex - 1, 0)
        }
    } else {
        const decoded = decodeSignalRecvChain(
            session.recvChains[recvChainIndex],
            `recvChains[${recvChainIndex}]`
        )
        const selected = selectMessageKey(decoded, message.counter)
        selectedMessageKey = selected.messageKey
        const nextRecvChains: RawSignalRecvChain[] = session.recvChains.slice()
        nextRecvChains[recvChainIndex] = encodeSignalRecvChain(selected.updatedChain)
        updatedSession = {
            ...session,
            recvChains: nextRecvChains
        }
    }

    const payloadWithoutMac = message.versionContentMac.subarray(
        0,
        message.versionContentMac.length - SIGNAL_MAC_SIZE
    )
    const expectedMac = hmacSha256Sign(selectedMessageKey.macKey, [
        session.remote.pubKey,
        session.local.pubKey,
        payloadWithoutMac
    ])
    const receivedMac = message.versionContentMac.subarray(
        message.versionContentMac.length - SIGNAL_MAC_SIZE
    )
    if (!uint8TimingSafeEqual(expectedMac.subarray(0, SIGNAL_MAC_SIZE), receivedMac)) {
        throw new Error('invalid message mac')
    }

    const plaintext = aesCbcDecrypt(
        selectedMessageKey.cipherKey,
        selectedMessageKey.iv,
        message.ciphertext
    )
    return [updatedSession, plaintext]
}
