const Queue = require('queuejs')

function replayer(remoteClient, localClient, callback) {
    let packets = new Queue()
    let sentRespawn = false

    localClient.on('packet', (data, metadata) => {
        if (!sentRespawn) {
            packets.enq({
                packet: data,
                metadata: metadata
            })
        }
    })

    localClient.once('login', (data, metadata) => {
        sentRespawn = true
        remoteClient.write('respawn', {
            dimension: data.dimension,
            difficulty: data.difficulty,
            gamemode: data.gameMode,
            levelType: data.levelType,
            worldName: data.worldName,
            hashedSeed: data.hashedSeed,
            isDebug: data.isDebug,
            isFlat: data.isFlat,
            copyMetadata: false
        })
        while (packets.size > 0) {
            let element = packets.deq()
            console.log(element)
            remoteClient.write(element.metadata.name, element.packet)
        }

        callback()
    })
}

module.exports = replayer