local _, addon = ...

local QuestFrame = CreateFrame("Frame", "CzechQuestsQuestFrame", UIParent, "BackdropTemplate")
QuestFrame:Hide()

addon.QuestFrame = QuestFrame

function QuestFrame:CreateHeader()
    local font = addon.API.CreateCzechFont(
            self,
            CzechQuestsAddon_Store.config.QUEST_HEADER_FONT_NAME,
            CzechQuestsAddon_Store.config.QUEST_HEADER_FONT_SIZE
    )
    font:SetWidth(self:GetWidth() - 20)
    return font;
end

function QuestFrame:CreateText()
    local font = addon.API.CreateCzechFont(
            self,
            CzechQuestsAddon_Store.config.QUEST_TEXT_FONT_NAME,
            CzechQuestsAddon_Store.config.QUEST_TEXT_FONT_SIZE
    )
    font:SetWidth(self:GetWidth() - 20)
    return font;
end

function QuestFrame:Init()
    -- Default style
    self:SetSize(300, 200)
    self:SetBackdrop({ edgeFile = "Interface/Tooltips/UI-Tooltip-Border", edgeSize = 8 })

    -- Background Parchment texture
    self.Texture = self:CreateTexture(nil, "BACKGROUND", nil, -1);
    self.Texture:SetAllPoints(true)
    self.Texture:SetHorizTile(true)

    -- Create fonts
    self.Title = self:CreateHeader()
    self.Text1 = self:CreateText()
    self.Header = self:CreateHeader()
    self.Text2 = self:CreateText()

    -- Load settings
    self:UpdateSettings()
end

function QuestFrame:UpdateSettings()
    addon.API.UpdateCzechFont(
            self.Title,
            CzechQuestsAddon_Store.config.QUEST_HEADER_FONT_NAME,
            CzechQuestsAddon_Store.config.QUEST_HEADER_FONT_SIZE
    )
    addon.API.UpdateCzechFont(
            self.Text1,
            CzechQuestsAddon_Store.config.QUEST_TEXT_FONT_NAME,
            CzechQuestsAddon_Store.config.QUEST_TEXT_FONT_SIZE
    )
    addon.API.UpdateCzechFont(
            self.Header,
            CzechQuestsAddon_Store.config.QUEST_HEADER_FONT_NAME,
            CzechQuestsAddon_Store.config.QUEST_HEADER_FONT_SIZE
    )
    addon.API.UpdateCzechFont(
            self.Text2,
            CzechQuestsAddon_Store.config.QUEST_TEXT_FONT_NAME,
            CzechQuestsAddon_Store.config.QUEST_TEXT_FONT_SIZE
    )

    if not CzechQuestsAddon_Store.config.QUEST_TEXTURE_ALPHA_ONLY_MOVING then
        self.Texture:SetAlpha(CzechQuestsAddon_Store.config.QUEST_TEXTURE_ALPHA / 100)
    end
end

function QuestFrame:SetTextColor(r, g, b, a)
    self.Title:SetTextColor(r, g, b, a)
    self.Header:SetTextColor(r, g, b, a)
    self.Text1:SetTextColor(r, g, b, a)
    self.Text2:SetTextColor(r, g, b, a)
end

function QuestFrame:ApplyTheme()
    if (WOW_PROJECT_ID == WOW_PROJECT_CLASSIC) then
        self.Texture:SetTexture("Interface/QUESTFRAME/QuestBG");
        if CzechQuestsAddon_Store.config.QUEST_DARK_MODE then
            self:SetBackdropColor(0, 0, 0, 1)
            self.Texture:SetColorTexture(0, 0, 0, 1)
            self:SetTextColor(255, 255, 255, 1)
        else
            self.Texture:SetTexCoord(0, .58, 0.005, 0.66)
            self:SetTextColor(0, 0, 0, 1)
        end
    else
        self.Texture:SetTexture("Interface/QUESTFRAME/QuestBackgroundParchment");
        self.Texture:SetTexCoord(0, .01, 0, 0.4)
        self:SetTextColor(0, 0, 0, 1)
        local questTextContrast = C_CVar.GetCVar("questTextContrast")
        if questTextContrast == "1" then
            -- Brown
            self.Texture:SetTexCoord(0, .01, .4, .8)
        elseif questTextContrast == "2" or questTextContrast == "3" then
            -- Gray
            self.Texture:SetColorTexture(255, 255, 255, 1)
        elseif questTextContrast == "4" then
            -- Black
            self.Texture:SetColorTexture(0, 0, 0, 1)
            self:SetTextColor(255, 255, 255, 1)
        end
    end
end

function QuestFrame:ResetFontText()
    self.Title:SetText("")
    self.Text1:SetText("")
    self.Header:SetText("")
    self.Text2:SetText("")
end

function QuestFrame:SetData(name, text1, header, text2, parentFrame, yOffset, xOffset)
    -- Reset previously values
    self:ResetFontText()
    self:ApplyTheme()

    -- Set parent
    self:SetParent(parentFrame)

    -- set text to labels
    self.Title:SetText(name or "")
    self.Text1:SetText(text1 or "")
    self.Header:SetText(header or "")
    self.Text2:SetText(text2 or "")

    -- apply text positions
    self.Title:SetPoint("TOPLEFT", self, "TOPLEFT", 10, -10)
    self.Text1:SetPoint("TOPLEFT", self.Title, "BOTTOMLEFT", 0, -5)
    self.Header:SetPoint("TOPLEFT", self.Text1, "BOTTOMLEFT", 0, -20)
    self.Text2:SetPoint("TOPLEFT", self.Header, "BOTTOMLEFT", 0, -5)

    -- apply frame position
    self:SetPoint("TOPLEFT", parentFrame, "TOPRIGHT", xOffset, yOffset)

    self:Show()

    -- wait for font render before calculate height
    local height = self.Title:GetStringHeight()
    if height == 0 then
        C_Timer.After(0.5, function()
            self:CalculateHeight()
        end)
    else
        self:CalculateHeight()
    end
end

function QuestFrame:CalculateHeight()
    local height = 0;
    local heights = {
        self.Title:GetHeight(),
        self.Text1:GetHeight(),
        self.Header:GetHeight(),
        self.Text2:GetHeight(),
        10
    }
    for _, value in ipairs(heights) do
        height = height + value + 10
    end
    self:SetHeight(height)
end