local _, addon = ...

local function GetQuestTitle(title, questId)
    return title .. (CzechQuestsAddon_Store.config.DEBUG_MODE and " (" .. questId .. ")" or "")
end

local function ShowQuestTranslation(event)
    local frame = addon.QuestFrame
    frame:Hide()

    -- Classic Era has QuestLogFrame
    if QuestLogFrame then
        if QuestLogFrame:IsVisible() then
            local questIndex = GetQuestLogSelection()
            if questIndex > 0 then
                local questId = select(8, GetQuestLogTitle(questIndex))
                local data = CzechQuestsAddon:GetData("quest", questId)
                if data then
                    local title = GetQuestTitle(data.title, questId)
                    frame:SetData(title, data.objective, "Popis", data.description, QuestLogFrame, -20, -20)
                end
            end
        end
    end

    -- Retail has QuestMapDetailsScrollFrame
    if QuestMapDetailsScrollFrame then
        if QuestMapDetailsScrollFrame:IsVisible() then
            local questId = C_QuestLog.GetSelectedQuest()
            local hasModel = QuestModelScene:IsVisible()
            local data = CzechQuestsAddon:GetData("quest", questId)
            if data then
                local yOffset = hasModel and 210 or 50
                local title = GetQuestTitle(data.title, questId)
                frame:SetData(title, data.objective, "Popis", data.description, QuestMapFrame, 0, yOffset)
            end
        end
    end

    -- If quest is right from NPC, this work for both wow version
    if event ~= nil then
        local questId = GetQuestID()
        if questId == 0 then
            return
        end
        local hasModel = QuestModelScene and QuestModelScene:IsShown() or false
        if questId then
            local data = CzechQuestsAddon:GetData("quest", questId)
            if data then
                local title = GetQuestTitle(data.title, questId)
                local xOffset = hasModel and 210 or 10
                if (WOW_PROJECT_ID == WOW_PROJECT_CLASSIC) then
                    xOffset = -20
                end
                if event == "QUEST_PROGRESS" then
                    frame:SetData(title, data.progress, "", "", QuestFrame, -20, xOffset)
                elseif event == "QUEST_COMPLETE" then
                    frame:SetData(title, data.completion, "", "", QuestFrame, -20, xOffset)
                elseif event == "QUEST_DETAIL" then
                    frame:SetData(title, data.description, "Cíl úkolu", data.objective, QuestFrame, -20, xOffset)
                else
                    frame:Hide()
                end
            end
        end
    end
end

local function ShowBookTranslation()
    local frame = addon.QuestFrame
    frame:Hide()

    -- classic and retail has same frame
    if ItemTextFrame:IsVisible() then
        local itemName = ItemTextGetItem();
        local itemNameTranslation = CzechQuestsAddon:GetData("book", itemName .. "_name")
        if itemNameTranslation then
            local pageNum = ItemTextGetPage()
            local itemPageContentTranslation = CzechQuestsAddon:GetData("book", itemName .. '_page_' .. pageNum)
            if itemPageContentTranslation then
                frame:SetData(
                        itemNameTranslation.m,
                        addon.API.ResolveGender(itemPageContentTranslation),
                        "",
                        "",
                        ItemTextFrame,
                        -20,
                        5
                )
            end
        end
    end
end

local function InitQuests()
    local frame = addon.QuestFrame

    frame:RegisterEvent("PLAYER_LOGIN")
    frame:RegisterEvent("QUEST_PROGRESS")
    frame:RegisterEvent("QUEST_COMPLETE")
    frame:RegisterEvent("QUEST_FINISHED")
    frame:RegisterEvent("QUEST_DETAIL")
    frame:RegisterEvent("ITEM_TEXT_READY")
    frame:RegisterEvent("PLAYER_STARTED_MOVING")
    frame:RegisterEvent("PLAYER_STOPPED_MOVING")

    frame:SetScript("OnEvent", function(_, event)
        if not CzechQuestsAddon_Store.config.QUEST_ENABLED then
            return
        end

        if event == "PLAYER_LOGIN" then
            -- classic
            if QuestLogFrame then
                QuestLogFrame:SetScript("OnEnter", function()
                    ShowQuestTranslation()
                end)
                QuestLogFrame:SetScript("OnLeave", function()
                    frame:Hide()
                end)
            end
            -- retail
            if QuestMapDetailsScrollFrame then
                QuestMapDetailsScrollFrame:SetScript("OnEnter", function()
                    ShowQuestTranslation()
                end)
                QuestMapDetailsScrollFrame:SetScript("OnLeave", function()
                    frame:Hide()
                end)
            end

            frame:Init()
        end

        if event == "QUEST_PROGRESS" or event == "QUEST_COMPLETE" or event == "QUEST_DETAIL" or event == "QUEST_FINISHED" then
            ShowQuestTranslation(event)
        end

        if (event == "ITEM_TEXT_READY") then
            ShowBookTranslation()
        end

        if CzechQuestsAddon_Store.config.QUEST_TEXTURE_ALPHA_ONLY_MOVING then
            if event == "PLAYER_STARTED_MOVING" then
                frame.Texture:SetAlpha(CzechQuestsAddon_Store.config.QUEST_TEXTURE_ALPHA / 100)
            elseif event == "PLAYER_STOPPED_MOVING" then
                frame.Texture:SetAlpha(1)
            end
        end

    end)
end
addon.API.InitQuests = InitQuests