local _, addon = ...

local WowQuestTextMethod = {
    Description = GetQuestText,
    Objective = GetObjectiveText,
};

local function GetWowQuestText(method)
    local text = WowQuestTextMethod[method]();
    if text then
        return text
    else
        return ""
    end
end

local function GetQuestLogQuestIndex(questID)
    local numEntries = C_QuestLog.GetNumQuestLogEntries()
    for questLogIndex = 1, numEntries do
        local info = C_QuestLog.GetInfo(questLogIndex)
        if info and info.questID == questID then
            return questLogIndex
        end
    end
    return nil
end

local function FillPlaceholders(text)
    if text == nil then
        return text
    end

    local playerName = UnitName("player")
    local _, playerClass = UnitClass("player")
    local _, playerRace = UnitRace("player")

    local class = CzechQuestsAddon:GetData("class", playerClass);
    local race = CzechQuestsAddon:GetData("race", playerRace);

    local formatted = text;
    formatted = string.gsub(formatted, '<name>', playerName);

    formatted = string.gsub(formatted, '<cF0>', class.f0);
    formatted = string.gsub(formatted, '<cF1>', class.f1);
    formatted = string.gsub(formatted, '<cF2>', class.f2);
    formatted = string.gsub(formatted, '<cF3>', class.f3);
    formatted = string.gsub(formatted, '<cF4>', class.f4);
    formatted = string.gsub(formatted, '<cF5>', class.f5);

    formatted = string.gsub(formatted, '<cM0>', class.m0);
    formatted = string.gsub(formatted, '<cM1>', class.m1);
    formatted = string.gsub(formatted, '<cM2>', class.m2);
    formatted = string.gsub(formatted, '<cM3>', class.m3);
    formatted = string.gsub(formatted, '<cM4>', class.m4);

    formatted = string.gsub(formatted, '<cP0>', class.p0);
    formatted = string.gsub(formatted, '<cP1>', class.p1);
    formatted = string.gsub(formatted, '<cP3>', class.m3);

    formatted = string.gsub(formatted, '<rF0>', race.f0);
    formatted = string.gsub(formatted, '<rF1>', race.f1);
    formatted = string.gsub(formatted, '<rF2>', race.f2);
    formatted = string.gsub(formatted, '<rF3>', race.f3);
    formatted = string.gsub(formatted, '<rF4>', race.f4);
    formatted = string.gsub(formatted, '<rF5>', race.f5);

    formatted = string.gsub(formatted, '<rM0>', race.m0);
    formatted = string.gsub(formatted, '<rM1>', race.m1);
    formatted = string.gsub(formatted, '<rM2>', race.m2);
    formatted = string.gsub(formatted, '<rM3>', race.m3);
    formatted = string.gsub(formatted, '<rM4>', race.m4);

    formatted = string.gsub(formatted, '<rP0>', race.p0);
    formatted = string.gsub(formatted, '<rP1>', race.p1);
    formatted = string.gsub(formatted, '<rP3>', race.p3);

    return formatted
end

local function GetQuestText(questTranslation, original)
    if questTranslation == nil then
        return nil
    end
    local translation = addon.API.ResolveGender(questTranslation)
    local paragraphs = addon.API.ParseParagraphs(original, translation)
    return FillPlaceholders(paragraphs)
end

local function GetQuest(id)
    local questName = addon.data.quest['q' .. id .. '_name'];
    local questObjective = addon.data.quest['q' .. id .. '_objective'];
    local questDescription = addon.data.quest['q' .. id .. '_description'];
    local questProgress = addon.data.quest['q' .. id .. '_progress'];
    local questCompletion = addon.data.quest['q' .. id .. '_completion'];

    if questName or questObjective or questDescription or questProgress or questCompletion then
        local description = ""
        local objective = ""

        if QuestMapDetailsScrollFrame and QuestMapDetailsScrollFrame:IsVisible() then
            local index = GetQuestLogQuestIndex(id)
            description, objective = GetQuestLogQuestText(index)
        else
            description = GetWowQuestText("Description")
            objective = GetWowQuestText("Objective")
        end

        return {
            title = GetQuestText(questName),
            objective = GetQuestText(questObjective, objective),
            description = GetQuestText(questDescription, description),
            progress = GetQuestText(questProgress),
            completion = GetQuestText(questCompletion),
        }
    end
end
addon.API.GetQuest = GetQuest