local _, addon = ...

local function DebugQuest(questId)
    if (CzechQuestsAddon_Store.config.DEBUG_MODE == true) then
        print(questId)
    end
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))
                DebugQuest(questId)
                local data = CzechQuestsAddon:GetData("quest", questId)
                if data then
                    local xOffset = -20
                    local yOffset = -30
                    -- Mists has wide quest log
                    if (WOW_PROJECT_ID == WOW_PROJECT_MISTS_CLASSIC) then
                        xOffset = 10
                        yOffset = -20
                    end
                    frame:SetData(data.title, data.objective, "Popis", data.description, QuestLogFrame, yOffset, xOffset)
                end
            end
        end
    end

    -- Mists has QuestLogDetailFrame
    if QuestLogDetailFrame then
        if QuestLogDetailFrame:IsVisible() then
            local questIndex = GetQuestLogSelection()
            if questIndex > 0 then
                local questId = select(8, GetQuestLogTitle(questIndex))
                DebugQuest(questId)
                local data = CzechQuestsAddon:GetData("quest", questId)
                if data then
                    local xOffset = 10
                    local yOffset = -20
                    frame:SetData(data.title, data.objective, "Popis", data.description, QuestLogDetailFrame, yOffset, xOffset)
                end
            end
        end
    end

    -- Retail has QuestMapDetailsScrollFrame
    if QuestMapDetailsScrollFrame then
        if QuestMapDetailsScrollFrame:IsVisible() then
            local questId = C_QuestLog.GetSelectedQuest()
            DebugQuest(questId)
            local hasModel = QuestModelScene:IsVisible()
            local data = CzechQuestsAddon:GetData("quest", questId)
            if data then
                local yOffset = hasModel and 210 or 50
                frame:SetData(data.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()
        DebugQuest(questId)
        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 yOffset = -20
                local xOffset = hasModel and 210 or 10
                if (WOW_PROJECT_ID == WOW_PROJECT_CLASSIC) then
                    xOffset = -20
                    yOffset = -40
                end
                if (WOW_PROJECT_ID == WOW_PROJECT_BURNING_CRUSADE_CLASSIC) then
                    xOffset = 10
                    yOffset = -20
                end
                if event == "QUEST_PROGRESS" then
                    frame:SetData(data.title, data.progress, "", "", QuestFrame, yOffset, xOffset)
                elseif event == "QUEST_COMPLETE" then
                    frame:SetData(data.title, data.completion, "", "", QuestFrame, yOffset, xOffset)
                elseif event == "QUEST_DETAIL" then
                    frame:SetData(data.title, data.description, "Cíl úkolu", data.objective, QuestFrame, yOffset, 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 book = CzechQuestsAddon:GetData("book", itemName)
        if book == nil then
            return nil
        end
        if book.name then
            local pageNum = ItemTextGetPage()
            local pageContent = book.pages[pageNum]
            if pageContent then
                frame:SetData(
                        book.name,
                        pageContent,
                        "",
                        "",
                        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
            -- mists
            if QuestLogDetailFrame then
                QuestLogDetailFrame:SetScript("OnEnter", function()
                    ShowQuestTranslation()
                end)
                QuestLogDetailFrame: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
            if QuestMapFrame and QuestMapFrame.QuestFrame and QuestMapFrame.QuestFrame.DetailsFrame and QuestMapFrame.QuestsFrame.DetailsFrame.RewardsFrameContainer then
                QuestMapFrame.QuestsFrame.DetailsFrame.RewardsFrameContainer: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((100 - 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