local _, addon = ...

local SpeechFrame = CreateFrame("ScrollFrame", "CzechQuestsSpeechFrame", UIParent, "UIPanelScrollFrameTemplate")
SpeechFrame:Hide()

local ContentFrame = CreateFrame("Frame", nil, SpeechFrame)
ContentFrame:Hide()

addon.SpeechFrame = SpeechFrame
SpeechFrame.ContentFrame = ContentFrame

function SpeechFrame:Init()
    local frame = self

    -- Default style
    self:SetSize(CzechQuestsAddon_Store.config.SPEECH_FRAME_WIDTH, 150)

    -- Make movable
    self:SetMovable(true)
    self:EnableMouse(true)
    self:RegisterForDrag("LeftButton")

    -- Hide sidebar
    self.ScrollBar:Hide()

    -- Register drag event
    self:SetScript("OnDragStart", function()
        frame:StartMoving()
    end)
    self:SetScript("OnDragStop", function(self)
        frame:StopMovingOrSizing()
        local finalLeft, finalTop = self:GetLeft(), self:GetTop()
        CzechQuestsAddon_Store.config.SPEECH_FRAME_POSITION = {
            x = finalLeft,
            y = finalTop
        }
    end)

    -- Create ContentFrame
    ContentFrame:Init()
    SpeechFrame:SetScrollChild(SpeechFrame.ContentFrame)

    -- Register hover events
    self:SetScript("OnEnter", function() self.ContentFrame.Texture:SetAlpha(0.8) end)
    self:SetScript("OnLeave", function() self.ContentFrame.Texture:SetAlpha(0.2) end)

    self:UpdateSettings()
end

function ContentFrame:Init()
    -- Default style
    self:SetSize(SpeechFrame:GetWidth(), 1)

    -- Texture for control color and alpha
    self.Texture = self:CreateTexture(nil, "ARTWORK")
    self.Texture:SetTexture("Interface\\Buttons\\WHITE8x8")
    self.Texture:SetVertexColor(0, 0, 0, 0.2)

    -- Set points to edges
    self.Texture:SetPoint("TOPLEFT", self ,"TOPLEFT", 0, 0)
    self.Texture:SetPoint("BOTTOMRIGHT", self ,"BOTTOMRIGHT", 0, 0)

    -- Set ContentFrame position
    self:SetPoint("BOTTOMLEFT", SpeechFrame, "BOTTOMLEFT", 0, 0)
end

function ContentFrame:CreateMessage(message)
    -- Default style
    local MessageFrame = CreateFrame("Frame", nil, SpeechFrame.ContentFrame)
    MessageFrame:SetSize(SpeechFrame.ContentFrame:GetWidth() - 20, 1)

    -- Define Message
    MessageFrame.Message = addon.API.CreateCzechFont(
            MessageFrame,
            CzechQuestsAddon_Store.config.SPEECH_TEXT_FONT_NAME,
            CzechQuestsAddon_Store.config.SPEECH_TEXT_FONT_SIZE,
            "THICK"
    )
    MessageFrame.Message:SetTextColor(1, 1, 1)
    MessageFrame.Message:SetPoint("TOPLEFT", MessageFrame, "TOPLEFT", 5, -5)
    MessageFrame.Message:SetPoint("BOTTOMRIGHT", MessageFrame, "BOTTOMRIGHT", -5, 5)

    -- Set text and height
    MessageFrame.Message:SetText(message)
    MessageFrame:SetHeight(MessageFrame.Message:GetHeight() + 10)

    return MessageFrame
end

function SpeechFrame:UpdateSettings()
    self:SetWidth(CzechQuestsAddon_Store.config.SPEECH_FRAME_WIDTH)
    self.ContentFrame:SetWidth(self:GetWidth())

    -- Set position
    self:ClearAllPoints()
    self:SetPoint(
            "TOPLEFT", UIParent, "TOPLEFT",
            CzechQuestsAddon_Store.config.SPEECH_FRAME_POSITION.x,
            CzechQuestsAddon_Store.config.SPEECH_FRAME_POSITION.y
    )
end