local S, L, O, U, D, E = unpack(select(2, ...));
local Module = S:NewNameplateModule('MythicPlusSpiteful');
local Stripes = S:GetNameplateModule('Handler');

-- WoW API
local UnitName = UnitName;

-- Libraires
local LCG = S.Libraries.LCG;
local LCG_ButtonGlow_Start, LCG_ButtonGlow_Stop = LCG.ButtonGlow_Start, LCG.ButtonGlow_Stop;

-- Local config
local ENABLED, ONLY_ON_ME, GLOW, GLOW_COLOR;

local PlayerData = D.Player;

local SPITEFUL_NPC_ID  = 174773;
local SPITEFUL_TEXTURE = 135945;

local function Create(unitframe)
    if unitframe.Spiteful then
        return;
    end

    local frame = CreateFrame('Frame', '$parentSpiteful', unitframe.healthBar);
    frame:SetPoint('BOTTOM', unitframe, 'TOP', 0, 4);
    frame:SetSize(32, 32);
    frame:SetFrameStrata('LOW');

    frame.icon = frame:CreateTexture(nil, 'OVERLAY');
    frame.icon:SetAllPoints();
    frame.icon:SetTexture(SPITEFUL_TEXTURE);
    frame.icon:SetTexCoord(0.1, 0.9, 0.1, 0.9);

    frame:SetShown(false);

    unitframe.Spiteful = frame;
end

local function Update(unitframe)
    if not unitframe.Spiteful then
        return;
    end

    if not ENABLED or unitframe.data.unitType == 'SELF' then
        unitframe.Spiteful:SetShown(false);
        return;
    end

    if unitframe:IsShown() then
        if ENABLED and unitframe.data.npcId == SPITEFUL_NPC_ID then
            if ONLY_ON_ME then
                if unitframe.data.targetName == PlayerData.Name then
                    if GLOW then
                        LCG_ButtonGlow_Start(unitframe.Spiteful, GLOW_COLOR);
                    else
                        LCG_ButtonGlow_Stop(unitframe.Spiteful);
                    end

                    unitframe.Spiteful:SetShown(true);
                else
                    LCG_ButtonGlow_Stop(unitframe.Spiteful);
                    unitframe.Spiteful:SetShown(false);
                end
            else
                if GLOW then
                    LCG_ButtonGlow_Start(unitframe.Spiteful, GLOW_COLOR);
                else
                    LCG_ButtonGlow_Stop(unitframe.Spiteful);
                end

                unitframe.Spiteful:SetShown(true);
            end
        else
            LCG_ButtonGlow_Stop(unitframe.Spiteful);
            unitframe.Spiteful:SetShown(false);
        end
    end
end

local function OnUpdate(unitframe)
    if not unitframe.Spiteful then
        return;
    end

    local name = UnitName(unitframe.data.unit .. 'target');

    if not unitframe.data.targetName or (name and name == PlayerData.Name) then
        Update(unitframe);
    end

    if unitframe.data.targetName ~= name then
        unitframe.data.targetName = name;
        Update(unitframe);
    end
end

local function Reset(unitframe)
    if unitframe.Spiteful then
        LCG_ButtonGlow_Stop(unitframe.Spiteful);
        unitframe.Spiteful:SetShown(false);
    end
end

function Module:UnitAdded(unitframe)
    Create(unitframe);
    Update(unitframe);
end

function Module:UnitRemoved(unitframe)
    Reset(unitframe);
end

function Module:Update(unitframe)
    Update(unitframe);
end

function Module:UpdateLocalConfig()
    ENABLED    = O.db.spiteful_enabled;
    ONLY_ON_ME = O.db.spiteful_show_only_on_me;
    GLOW       = O.db.spiteful_glow;
    GLOW_COLOR    = GLOW_COLOR or {};
    GLOW_COLOR[1] = O.db.spiteful_glow_color[1];
    GLOW_COLOR[2] = O.db.spiteful_glow_color[2];
    GLOW_COLOR[3] = O.db.spiteful_glow_color[3];
    GLOW_COLOR[4] = O.db.spiteful_glow_color[4] or 1;

    if ENABLED then
        Stripes.Updater:Add('MythicPlusSpiteful', OnUpdate);
    else
        Stripes.Updater:Remove('MythicPlusSpiteful');
    end
end

function Module:StartUp()
    self:UpdateLocalConfig();
end