SFrames.ToT = {}
local _A = SFrames.ActiveTheme

function SFrames.ToT:Initialize()
    local f = CreateFrame("Button", "SFramesToTFrame", UIParent)
    f:SetWidth(120)
    f:SetHeight(25)
    f:SetPoint("BOTTOMLEFT", SFramesTargetFrame, "BOTTOMRIGHT", 5, 0)
    
    f:RegisterForClicks("LeftButtonUp", "RightButtonUp")
    f:SetScript("OnClick", function()
        if arg1 == "LeftButton" then
            TargetUnit("targettarget")
        end
    end)
    
    -- Health Bar
    f.health = SFrames:CreateStatusBar(f, "SFramesToTHealth")
    f.health:SetPoint("TOPLEFT", f, "TOPLEFT", 1, -1)
    f.health:SetPoint("BOTTOMRIGHT", f, "BOTTOMRIGHT", -1, 1)
    
    local hbg = CreateFrame("Frame", nil, f)
    hbg:SetPoint("TOPLEFT", f.health, "TOPLEFT", -1, 1)
    hbg:SetPoint("BOTTOMRIGHT", f.health, "BOTTOMRIGHT", 1, -1)
    hbg:SetFrameLevel(f:GetFrameLevel() - 1)
    SFrames:CreateUnitBackdrop(hbg)
    
    f.health.bg = f.health:CreateTexture(nil, "BACKGROUND")
    f.health.bg:SetAllPoints()
    f.health.bg:SetTexture(SFrames:GetTexture())
    f.health.bg:SetVertexColor(_A.slotBg[1], _A.slotBg[2], _A.slotBg[3], _A.slotBg[4] or 1)

    f.nameText = SFrames:CreateFontString(f.health, 10, "CENTER")
    f.nameText:SetPoint("CENTER", f.health, "CENTER", 0, 0)
    
    self.frame = f
    f:Hide()
    
    -- Update loop since targettarget changes don't fire precise events in Vanilla
    self.updater = CreateFrame("Frame")
    self.updater.timer = 0
    self.updater:SetScript("OnUpdate", function()
        this.timer = this.timer + arg1
        if this.timer >= 0.2 then
            SFrames.ToT:Update()
            this.timer = 0
        end
    end)
end

function SFrames.ToT:Update()
    if UnitExists("targettarget") then
        self.frame:Show()
        
        local hp = UnitHealth("targettarget")
        local maxHp = UnitHealthMax("targettarget")
        self.frame.health:SetMinMaxValues(0, maxHp)
        self.frame.health:SetValue(hp)
        
        self.frame.nameText:SetText(UnitName("targettarget"))
        
        if UnitIsPlayer("targettarget") then
            local _, class = UnitClass("targettarget")
            local color = SFrames.Config.colors.class[class]
            if color then
                self.frame.health:SetStatusBarColor(color.r, color.g, color.b)
                self.frame.nameText:SetTextColor(color.r, color.g, color.b)
            else
                self.frame.health:SetStatusBarColor(0, 1, 0)
                self.frame.nameText:SetTextColor(1, 1, 1)
            end
        else
            local r, g, b = 0.85, 0.77, 0.36 -- Neutral
            if UnitIsEnemy("player", "targettarget") then
                r, g, b = 0.78, 0.25, 0.25 -- Enemy
            elseif UnitIsFriend("player", "targettarget") then
                r, g, b = 0.33, 0.59, 0.33 -- Friend
            end
            self.frame.health:SetStatusBarColor(r, g, b)
            self.frame.nameText:SetTextColor(r, g, b)
        end
    else
        self.frame:Hide()
    end
end
