local core = require('openmw.core')
local ui = require('openmw.ui')

local MOD_PREFIX = '[YAMPT VoiceProbe]'
local lastKey = nil
local lastTime = -1000

local function findInfo(dialogueType, recordId, infoId)
    local bucket = core.dialogue[dialogueType]
    if not bucket then
        return nil, nil
    end

    local record = bucket.records[recordId]
    if not record then
        return nil, nil
    end

    for _, info in ipairs(record.infos) do
        if info.id == infoId then
            return record, info
        end
    end

    return record, nil
end

local function compact(value)
    if not value or value == '' then
        return '-'
    end
    return tostring(value)
end

local function summarizeConditions(info)
    local parts = {}

    if info.filterActorId and info.filterActorId ~= '' then
        table.insert(parts, 'actor=' .. info.filterActorId)
    end
    if info.filterActorRace and info.filterActorRace ~= '' then
        table.insert(parts, 'race=' .. info.filterActorRace)
    end
    if info.filterActorClass and info.filterActorClass ~= '' then
        table.insert(parts, 'class=' .. info.filterActorClass)
    end
    if info.filterActorFaction and info.filterActorFaction ~= '' then
        table.insert(parts, 'faction=' .. info.filterActorFaction)
    end
    if info.filterActorGender ~= nil and info.filterActorGender ~= '' then
        table.insert(parts, 'gender=' .. tostring(info.filterActorGender))
    end
    if info.filterPlayerCell and info.filterPlayerCell ~= '' then
        table.insert(parts, 'cell=' .. info.filterPlayerCell)
    end
    if info.filterActorDisposition ~= nil and info.filterActorDisposition ~= 0 then
        table.insert(parts, 'disp>=' .. tostring(info.filterActorDisposition))
    end

    return #parts > 0 and table.concat(parts, ', ') or 'none'
end

local function logVoiceResponse(e)
    if e.type ~= 'voice' then
        return
    end

    local record, info = findInfo(e.type, e.recordId, e.infoId)
    local text = info and info.text or '<missing info text>'
    local actorId = (e.actor and e.actor.recordId) or '<missing actor>'
    local actorSource = (e.actor and e.actor.contentFile) or '<dynamic>'
    local sound = info and compact(info.sound) or '-'
    local conditions = info and summarizeConditions(info) or 'record-missing'

    local dedupeKey = table.concat({ actorId, e.recordId, e.infoId, text }, '|')
    local now = core.getRealTime()
    if dedupeKey == lastKey and now - lastTime < 0.25 then
        return
    end
    lastKey = dedupeKey
    lastTime = now

    local brief = string.format('%s %s / %s / %s', MOD_PREFIX, actorId, e.infoId, text)
    local detail = table.concat({
        MOD_PREFIX,
        'actor=' .. actorId,
        'actorSource=' .. actorSource,
        'record=' .. compact(e.recordId),
        'info=' .. compact(e.infoId),
        'recordName=' .. compact(record and record.name),
        'text=' .. text,
        'sound=' .. sound,
        'filters=' .. conditions,
    }, ' | ')

    print(detail)
    ui.printToConsole(detail, ui.CONSOLE_COLOR.Info)
    ui.showMessage(brief, { showInDialogue = false })
end

return {
    eventHandlers = {
        DialogueResponse = logVoiceResponse,
    },
}
