local _, addonTable = ...

local ComboPointsMixin = {}

---------------------------------------------------------------------------

function ComboPointsMixin:Init()
    local config = addonTable.getConfig()
    local mixin = self

    mixin.blinkState = true
    mixin.PointsTextures = {}
    mixin.Frame = CreateFrame("Frame", "ComboPointsParentFrame", UIParent, BackdropTemplateMixin and "BackdropTemplate")

    mixin.Frame:SetFrameLevel(501)
    mixin.Frame:SetSize(5*256, 256)
    mixin.Frame:SetScale(config.scale)
    mixin.Frame:SetPoint("CENTER", UIParent, "CENTER", config.position_x, config.position_y)
    mixin.Frame:Show()
    mixin:ApplyLayout()
    
    mixin.Frame:SetMovable(config.movable)
    mixin.Frame:EnableMouse(config.movable)
    mixin.Frame:RegisterForDrag("LeftButton")
    mixin.Frame:SetScript("OnDragStart", mixin.Frame.StartMoving)
    mixin.Frame:SetScript("OnDragStop", function(frame)
        frame:StopMovingOrSizing()
        local config = addonTable.getConfig()
        local _, _, _, xOfs, yOfs = frame:GetPoint()
        config.position_x = xOfs
        config.position_y = yOfs
    end)

    
    mixin.Frame:RegisterUnitEvent("UNIT_POWER_UPDATE", "player")
    mixin.Frame:SetScript("OnEvent", function(self, event, unit)
        if unit and unit ~= "player" then return end
        mixin:UpdateDisplay()
    end)

    local blinkFrame = CreateFrame("Frame", nil, mixin.Frame)
    blinkFrame.elapsed = 0
    blinkFrame.blinkSpeed = config.blinkSpeed
    blinkFrame:SetScript("OnUpdate", function(self, elapsed)
        self.elapsed = self.elapsed + elapsed
        if self.elapsed >= self.blinkSpeed then
            mixin.blinkState = not mixin.blinkState
            mixin:UpdateDisplay()
            self.elapsed = 0
        end
    end)
end

---------------------------------------------------------------------------

function ComboPointsMixin:Show()
    self.Frame:Show()
end

function ComboPointsMixin:Hide()
    self.Frame:Hide()
end

function ComboPointsMixin:SetScale(scale)
    self.Frame:SetScale(scale)
end

function ComboPointsMixin:GetFrame()
    return self.Frame
end

---------------------------------------------------------------------------

function ComboPointsMixin:UpdateDisplay()
    if not self.initialized then return end

    local current = UnitPower("player", Enum.PowerType.ComboPoints)
    local max = UnitPowerMax("player", Enum.PowerType.ComboPoints) or 5
    max = max > 0 and max or 5

    for i = 1, max do
        if i <= current then
            self.PointsTextures[i]:SetBackdrop({bgFile = getPop(self.blinkState)})
        else
            self.PointsTextures[i]:SetBackdrop({bgFile = getEmpty()})
        end
    end

end

function ComboPointsMixin:ApplyLayout()
    local config = addonTable.getConfig()
    local max = UnitPowerMax("player", Enum.PowerType.ComboPoints) or 5
    max = max > 0 and max or 5

    self.Frame:ClearAllPoints()
    self.Frame:SetPoint("CENTER", UIParent, "CENTER", config.position_x, config.position_y)

    for i = 1, max do
        self.PointsTextures[i] = CreateFrame("Frame", nil, self.Frame, BackdropTemplateMixin and "BackdropTemplate")
        self.PointsTextures[i]:SetFrameLevel(502)
        self.PointsTextures[i]:SetSize(256, 256)
        self.PointsTextures[i]:SetBackdrop({bgFile = getEmpty()})
        self.PointsTextures[i]:SetPoint("LEFT", self.Frame, "LEFT", (i-1)*256, 0)
        self.PointsTextures[i]:Show()    
    end
    self.initialized = true
end

addonTable.ComboPointsMixin = ComboPointsMixin
