--------------------------------------------------------------------------------
-- File: AoUMeterGUI.lua
-- Desc: Graphical user interface
-- Original: DarkDPSMeter.lua from DarkDPSMeter2
-- Mod by: Resha
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
-- Type TMainPanelGUI
Global("TMainPanelGUI", {})
--------------------------------------------------------------------------------
function TMainPanelGUI:CreateNewObject(name)
    local widget = TWidget:CreateNewObject(name)
    return setmetatable({
            FightBtn = widget:GetChildByName("FightPanel"),
            FightText = widget:GetChildByName("FightPanel"):GetChildByName("FightNameTextView").Widget, -- Button to switch the active fight
            ModeBtn = widget:GetChildByName("ModePanel"),
            ModeText = widget:GetChildByName("ModePanel"):GetChildByName("ModeNameTextView").Widget, -- Button to switch the active mode       
            TotalPanel = widget:GetChildByName("TotalInfoPanel"), -- Panel to display the total
            PlayerList = {}, -- Panel player list
        }, { __index = widget })
end

--------------------------------------------------------------------------------
-- Type TMainPanelGUI
Global("TDetailsPanelGUI", {})
--------------------------------------------------------------------------------
function TDetailsPanelGUI:CreateNewObject(name)
    local widget = TWidget:CreateNewObject(name)
    return setmetatable({
            CloseButton = widget:GetChildByName("CloseBtn"), -- Close button
            PlayerNameText = widget:GetChildByName("SpellPlayerNameTextViewName").Widget,
            
            SpellHeaderPanel = widget:GetChildByName("SpellHeaderPanel"), -- Header panel
            SpellHeaderNameText = widget.Widget:GetChildChecked("SpellHeaderTextViewName", true),
            SpellHeaderStatsText = widget.Widget:GetChildChecked("SpellHeaderTextViewStats", true),
            SpellList = {},         -- Panel spell list
            
            SpellDetailsHeader = widget:GetChildByName("SpellDetailHeaderPanel"),
            SpellDetailsHeaderNameText = widget.Widget:GetChildChecked("SpellDetailHeaderTextViewName", true),
            SpellDetailsHeaderStatsText = widget.Widget:GetChildChecked("SpellDetailHeaderTextViewStats", true),  
            SpellInfoList = {},     -- Panel spell info list (normal, critical, glancing)
            SpellMissList = {},     -- Panel miss list
            SpellBlockList = {},    -- Panel block list
            SpellExtraList = {},    -- Panel extra info list
        }, { __index = widget })
end
--------------------------------------------------------------------------------
-- Type TTotalPanelGUI
Global("TTotalPanelGUI", {})
--------------------------------------------------------------------------------
function TTotalPanelGUI:CreateNewObjectByDesc(name, desc, owner)
    local widget = TWidget:CreateNewObjectByDesc(name, desc, owner)
    return setmetatable({
            Bar = widget:GetChildByName("PlayerInfoBar"),
            Name = widget:GetChildByName("TotalInfoTextViewName").Widget,
            Value = widget:GetChildByName("PlayerInfoTextViewStats").Widget
        }, { __index = widget })
end
--------------------------------------------------------------------------------
-- Type TPlayerPanelGUI
Global("TPlayerPanelGUI", {})
--------------------------------------------------------------------------------
function TPlayerPanelGUI:CreateNewObjectByDesc(name, desc, owner)
    local widget = TWidget:CreateNewObjectByDesc(name, desc, owner)
    return setmetatable({
            Bar = widget:GetChildByName("PlayerInfoBar"),
            Name = widget:GetChildByName("PlayerInfoTextViewName").Widget,
            Value = widget:GetChildByName("PlayerInfoTextViewStats").Widget,
            Percent = widget:GetChildByName("PlayerInfoTextViewPercentage").Widget,
        }, { __index = widget })
end
--------------------------------------------------------------------------------
-- Type TSpellPanelGUI
Global("TSpellPanelGUI", {})
--------------------------------------------------------------------------------
function TSpellPanelGUI:CreateNewObjectByDesc(name, desc, owner)
    local widget = TWidget:CreateNewObjectByDesc(name, desc, owner)
    return setmetatable({
            Bar = widget:GetChildByName("SpellBar"),
            Name = widget:GetChildByName("SpellTextViewName").Widget,
            Damage = widget:GetChildByName("SpellTextViewStats").Widget,
            Percent = widget:GetChildByName("SpellTextViewPercentage").Widget,
        }, { __index = widget })
end
--------------------------------------------------------------------------------
-- Type TSpellDetailsPanelGUI
Global("TSpellDetailsPanelGUI", {})
--------------------------------------------------------------------------------
function TSpellDetailsPanelGUI:CreateNewObjectByDesc(name, desc, owner)
    local widget = TWidget:CreateNewObjectByDesc(name, desc, owner)
    return setmetatable({
            Bar = widget:GetChildByName("SpellDetailBar"),
            Name = widget:GetChildByName("SpellDetailTextViewName").Widget,
            Count = widget:GetChildByName("SpellDetailTextViewCount").Widget,
            Damage = widget:GetChildByName("SpellDetailTextViewStats").Widget,
            Percent = widget:GetChildByName("SpellDetailTextViewPercentage").Widget,
        }, { __index = widget })
end

--------------------------------------------------------------------------------
-- Type TUMeter
Global("TUMeterGUI", {})
--------------------------------------------------------------------------------
function TUMeterGUI:CreateNewObject(dpsMeter)
    return setmetatable({
            DPSMeter = dpsMeter,   -- the data object
            
            ActiveFight = dpsMeter.Fight.Current,   -- Active fight
            ActiveMode = enumMode.Dps,          -- Active mode
            SelectedCombatant = nil,            -- Selected combatant
            SelectedCombatantName = "",         -- Selected combatant name
            
            ShowHideBtn = nil,      -- "D" button to show/hide the addon
            MainPanel = nil,        -- Main panel with player list
            DetailsPanel = nil,     -- Details panel with spell list / details
            
            InitialSize = {},       -- Keep trace of original sizes and location of widgets
            MainPanelWidth = 294,   -- MainPanel width, must be >= 294
            BarWidth = 230,         -- Bar width, adjusted automatically
        }, { __index = self })
end
--------------------------------------------------------------------------------
function TUMeterGUI:GetActiveFight()
    return self.DPSMeter.FightsList[self.ActiveFight]
end
--------------------------------------------------------------------------------
function TUMeterGUI:GetActiveFightData()
    return self.DPSMeter.FightsList[self.ActiveFight].Data[self.ActiveMode]
end
--------------------------------------------------------------------------------
function TUMeterGUI:GetCurrentCombatant()
    if self.SelectedCombatant then return self.SelectedCombatant end
end
--------------------------------------------------------------------------------
function TUMeterGUI:SetSelectedCombatantByName(name)
    self.SelectedCombatant = nil
    self.SelectedCombatantName = name
end
--==============================================================================
--================= PLAYER LIST ================================================
--==============================================================================

--------------------------------------------------------------------------------
-- Hide player panel in the player list
--------------------------------------------------------------------------------
function TUMeterGUI:HideAllPlayerPanel()
    self.MainPanel.TotalPanel:Hide()
    for i = 1, Settings["MaxCombatants"] do
        self.MainPanel.PlayerList[i]:Hide()
    end
end

--------------------------------------------------------------------------------
-- Update the panel "Total" in the player list
--------------------------------------------------------------------------------
function TUMeterGUI:DisplayTotal()
    local totalPanel = self.MainPanel.TotalPanel
    totalPanel:Show()
    
    local minute, second = 0, 0
    local fightTime = self:GetActiveFight().Timer:GetElapsedTime()
    if fightTime > 0 then
        minute, second = GetMinSec(fightTime)
    end
    
    totalPanel.Bar:SetWidth(self.BarWidth)
    totalPanel.Bar:SetColor(self.DPSMeter.bCollectData and TotalColorInFight or TotalColor)
    
    -- Total(0:00)
    local total = "Total ("  .. minute .. ":" .. userMods.FromWString(common.FormatInt(second , "%02d")) .. ")"
    totalPanel.Name:SetVal("Name", userMods.ToWString(total))
    totalPanel.Value:SetVal("DamageDone", common.FormatFloat(self:GetActiveFightData().Amount, "%f3K5"))
    totalPanel.Value:SetVal("DPS", common.FormatFloat(self:GetActiveFightData().AmountPerSec, "%f3K5"))
end
--------------------------------------------------------------------------------
-- Update values for a combatant in the player list
--------------------------------------------------------------------------------
function TUMeterGUI:DisplayPlayer(playerIndex)
    local combatant = self:GetActiveFight():GetCombatantByIndex(playerIndex)
    local playerPanel = self.MainPanel.PlayerList[playerIndex]
    if not combatant or not playerPanel then return end
    
    playerPanel:Show()
    
    playerPanel.Bar:SetWidth(self.BarWidth * (combatant.Data[self.ActiveMode].LeaderPercentage / 100))
    playerPanel.Bar:SetColor(ClassColors[combatant.Class])
    
    playerPanel.Name:SetVal("Index", common.FormatInt(playerIndex , "%d"))
    playerPanel.Name:SetVal("Name", combatant.Name)
    playerPanel.Value:SetVal("DamageDone", common.FormatFloat(combatant.Data[self.ActiveMode].Amount, "%f3K5"))
    playerPanel.Value:SetVal("DPS", common.FormatFloat(combatant.Data[self.ActiveMode].AmountPerSec, "%f3K5"))
    playerPanel.Percent:SetVal("Percentage", common.FormatInt(combatant.Data[self.ActiveMode].Percentage, "%d"))
end
--------------------------------------------------------------------------------
-- Update the whole player list
--------------------------------------------------------------------------------
function TUMeterGUI:UpdatePlayerList()
    local currentFight = self:GetActiveFight()
    if not self.MainPanel.Widget:IsVisible() or not currentFight then return end
    currentFight:RecalculateCombatantsData(self.ActiveMode) -- Important
    
    local combatantCount = math.min(currentFight:GetCombatantCount(), Settings["MaxCombatants"])
    
    self:HideAllPlayerPanel()
    self.MainPanel:SetHeight(47 + (combatantCount + 1) * 24)
    
    self:DisplayTotal()
    for playerIndex = 1, combatantCount do
        self:DisplayPlayer(playerIndex)
    end
end

--==============================================================================
--================= SPELL PANEL - Spell list ===================================
--==============================================================================

--------------------------------------------------------------------------------
-- Update a spell line in the spell panel
--------------------------------------------------------------------------------
function TUMeterGUI:DisplaySpell(spellIndex)
    if not self.SelectedCombatant then return end
    
    local spellData = self.SelectedCombatant:GetSpellByIndex(spellIndex, self.ActiveMode)
    local spellPanel = self.DetailsPanel.SpellList[spellIndex]
    
    if spellData and spellPanel then
        self.DetailsPanel.SpellList[spellIndex]:Show()
        
        spellPanel.Bar:SetColor(DamageTypeColors[spellData.Type])
        spellPanel.Bar:SetWidth(356 * (spellData.Percentage / 100))
        
        spellPanel.Name:SetVal("Index", common.FormatInt(spellIndex , "%d"))
        spellPanel.Name:SetVal("Name", spellData.Name)
        spellPanel.Damage:SetVal("DamageDone", common.FormatFloat(spellData.Amount , "%f3K5"))
        spellPanel.Damage:SetVal("DPS", common.FormatFloat(spellData.AmountPerSec , "%f3K5"))
        spellPanel.Damage:SetVal("DamageBlock", common.FormatFloat(spellData.ResistPercentage , "%d"))
        spellPanel.Percent:SetVal("Percentage", common.FormatInt(spellData.Percentage , "%d"))
    end
end
--------------------------------------------------------------------------------
-- Fill the spell panel
--------------------------------------------------------------------------------
function TUMeterGUI:UpdateSpellList()
    local detailsPanel = self.DetailsPanel
    if not detailsPanel:IsVisible() or not self:GetActiveFight() then return end
    
    detailsPanel:HideAllChild()
    
    detailsPanel.CloseButton:Show()
    
    if self.SelectedCombatant then
        self.SelectedCombatant:CalculateSpell(self:GetActiveFight().Timer:GetElapsedTime(), self.ActiveMode) -- Important.
        
        detailsPanel.PlayerNameText:SetVal("Name", self.SelectedCombatant.Name)
        detailsPanel.PlayerNameText:Show(true)
        
        if self.SelectedCombatant.Amount ~= 0 then
            detailsPanel.SpellHeaderPanel:Show()
        end
        for spellIndex = 1, MAXSPELLS do
            self:DisplaySpell(spellIndex)
        end
    end
end

--------------------------------------------------------------------------------
-- Update values in both player list & spell panel
--------------------------------------------------------------------------------
function TUMeterGUI:UpdateValues()
    if not self.SelectedCombatant then
        self.SelectedCombatant = self:GetActiveFight():GetCombatantByName(self.SelectedCombatantName)
        if not self.SelectedCombatant then
            self:SetSelectedCombatantByName(object.GetName(avatar.GetId()))
            self.SelectedCombatant = self:GetActiveFight():GetCombatantByName(object.GetName(avatar.GetId()))
        end
    end
    self:UpdatePlayerList()
    self:UpdateSpellList()
end

--==============================================================================
--================= SPELL PANEL - details list + details resist list ===========
--==============================================================================

--------------------------------------------------------------------------------
-- Display a spell details line in the spell info panel
--------------------------------------------------------------------------------
function TUMeterGUI:DisplaySpellDetails(spellInfoData, spellInfoPanel, title)
    if spellInfoData and spellInfoData.Count > 0 then
        spellInfoPanel:Show()
        
        spellInfoPanel.Bar:SetColor(HitTypeColors[spellInfoData.ID])
        spellInfoPanel.Bar:SetWidth(356 * (spellInfoData.Percentage / 100))
        
        spellInfoPanel.Name:SetVal("Name", title[spellInfoData.ID])
        spellInfoPanel.Count:SetVal("Count", common.FormatInt(spellInfoData.Count , "%d"))
        spellInfoPanel.Damage:SetVal("Min", common.FormatFloat(spellInfoData.Min , "%f3K5"))
        spellInfoPanel.Damage:SetVal("Avg", common.FormatFloat(spellInfoData.Avg , "%f3K5"))
        spellInfoPanel.Damage:SetVal("Max", common.FormatFloat(spellInfoData.Max , "%f3K5"))
        spellInfoPanel.Percent:SetVal("Percentage", common.FormatInt(spellInfoData.Percentage , "%d"))
    end
end
--------------------------------------------------------------------------------
-- Fill the spell panel
--------------------------------------------------------------------------------
function TUMeterGUI:UpdateSpellDetailsList(spellIndex)
    if (self.SelectedCombatant) then
        local index
        local spellData = self.SelectedCombatant:GetSpellByIndex(spellIndex, self.ActiveMode)
        
        if spellData then
            index = 1
            for dmgtype in SortSpellDetailsByCount(spellData.DetailsList) do
                self:DisplaySpellDetails(spellData.DetailsList[dmgtype], self.DetailsPanel.SpellInfoList[index], TitleDmgType)
                index = index + 1
            end
            
            index = 1
            for misstype in SortSpellDetailsByCount(spellData.MissList) do
                self:DisplaySpellDetails(spellData.MissList[misstype], self.DetailsPanel.SpellMissList[index], TitleMissType)
                index = index + 1
            end
            
            index = 1
            for blockdmgtype in SortSpellDetailsByAmount(spellData.ResistDetailsList) do
                self:DisplaySpellDetails(spellData.ResistDetailsList[blockdmgtype], self.DetailsPanel.SpellBlockList[index], self.ActiveMode == enumMode.Hps and TitleHealResistType or TitleHitBlockType)
                index = index + 1
            end
            
            index = 1
            for extrainfotype in SortSpellDetailsByAmount(spellData.ExtraInfoList) do
                self:DisplaySpellDetails(spellData.ExtraInfoList[extrainfotype], self.DetailsPanel.SpellExtraList[index], TitleExtraInfoType)
                index = index + 1
            end
        end
    end
end
--------------------------------------------------------------------------------
-- Hide all spell info line
--------------------------------------------------------------------------------
function TUMeterGUI:HideAllSpellDetailsPanel()
    for i = 1, DMGTYPES do
        self.DetailsPanel.SpellInfoList[i]:Hide()
    end
    for i = 1, MISSTYPES do
        self.DetailsPanel.SpellMissList[i]:Hide()
    end
    for i = 1, BLOCKDMGTYPES do
        self.DetailsPanel.SpellBlockList[i]:Hide()
    end
    for i = 1, EXTRATYPES do
        self.DetailsPanel.SpellExtraList[i]:Hide()
    end
end

--==============================================================================
--================= Event implementation =======================================
--==============================================================================

--------------------------------------------------------------------------------
-- Swap to the next fight
--------------------------------------------------------------------------------
function TUMeterGUI:SwapFight()
    local rotateFight = {
        [self.DPSMeter.Fight.Total] = self.DPSMeter.Fight.Current,
        [self.DPSMeter.Fight.Current] = self.DPSMeter.Fight.Previous,
        [self.DPSMeter.Fight.Previous] = self.DPSMeter.Fight.Total
    }
    self.ActiveFight = rotateFight[self.ActiveFight]
    self.SelectedCombatant = nil -- invalidate the current combatant
    
    -- DPSMeter.Fight.x indices can change during the execution
    local title = {
        [self.DPSMeter.Fight.Previous] = TitleFight[enumFight.Previous],
        [self.DPSMeter.Fight.Current] = TitleFight[enumFight.Current],
        [self.DPSMeter.Fight.Total] = TitleFight[enumFight.Total]
    }
    
    -- Update the fight in the fight panel (at the top of the player list)
    self.MainPanel.FightText:SetVal("Name", title[self.ActiveFight])
    
    -- Update the fight in the title of the spell panel
    self.DetailsPanel.PlayerNameText:SetVal("Fight", title[self.ActiveFight])
    
    self:UpdateValues()
end

--------------------------------------------------------------------------------
-- Swap to the next mode
--------------------------------------------------------------------------------
function TUMeterGUI:SwapMode()
    local rotateMode = {
        [enumMode.Dps] = Settings["ModeHPS"] and enumMode.Hps or Settings["ModeDEF"] and enumMode.Def or enumMode.Dps,
        [enumMode.Hps] = Settings["ModeDEF"] and enumMode.Def or Settings["ModeDPS"] and enumMode.Dps or enumMode.Hps,
        [enumMode.Def] = Settings["ModeDPS"] and enumMode.Dps or Settings["ModeHPS"] and enumMode.Hps or enumMode.Def
    }
    self.ActiveMode = rotateMode[self.ActiveMode]
    self.SelectedCombatant = nil -- invalidate the current combatant
    
    -- Update the mode in the fight panel (at the top of the player list)
    self.MainPanel.ModeText:SetVal("Name", TitleMode[self.ActiveMode])
    
    -- Update the mode in the title of the spell panel
    self.DetailsPanel.PlayerNameText:SetVal("Mode", TitleMode[self.ActiveMode])
    
    -- Update the mode in the header of the spell panel
    self.DetailsPanel.SpellHeaderStatsText:SetVal("DPS", TitleMode[self.ActiveMode])
    
    self:UpdateValues()
end

--==============================================================================
--================= INIT =======================================================
--==============================================================================
function TUMeterGUI:Init()
    -- The Data
    self.DPSMeter = TUMeter:CreateNewObject()
    
    -- Default mode
    self.ActiveMode = Settings["DefaultMode"]
    self.ActiveFight = self.DPSMeter.Fight.Current
    
    -- The "D" button to show or hide the main panels
    self.ShowHideBtn = TWidget:CreateNewObject("ShowHideBtn")
    self.ShowHideBtn:DragNDrop(true, 687, nil, true)
    
    -- Main panel with player list
    self.MainPanel = TMainPanelGUI:CreateNewObject("MainPanel")
    self.MainPanel:DragNDrop(true, 686, nil, true, false)
    
    -- Retrieve initial sizes of the main panel and its children in order to tweak the width
    local posX, posY
    self.InitialSize["MainPanelWidth"] = self.MainPanel:GetWidth()
    self.InitialSize["FightBtnWidth"] = self.MainPanel.FightBtn:GetWidth()
    self.InitialSize["ModeBtnWidth"] = self.MainPanel.ModeBtn:GetWidth()
    posX, posY = self.MainPanel.FightBtn:GetPosition()
    self.InitialSize["FightBtnPosX"] = posX
    
    -- Secondary panel with spell list / details
    self.DetailsPanel = TDetailsPanelGUI:CreateNewObject("SpellInfoPanel")
    self.DetailsPanel:DragNDrop(true, 688, nil, true)
    
    -- Total panel
    local totalPanelDesc = self.MainPanel.TotalPanel:GetDesc()
    self.MainPanel.TotalPanel:Destroy()
    self.MainPanel.TotalPanel = TTotalPanelGUI:CreateNewObjectByDesc("TotalPanel", totalPanelDesc, self.MainPanel)
    self.MainPanel.TotalPanel:SetPosition(20, 47)
    self.InitialSize["TotalPanelWidth"] = self.MainPanel.TotalPanel:GetWidth()
    
    -- Player list
    local playerPanelDesc = self.MainPanel:GetChildByName("PlayerInfoPanel"):GetDesc()
    self.MainPanel:GetChildByName("PlayerInfoPanel"):Destroy()
    for playerIndex = 1, Settings["MaxCombatants"] do
        local wtName = "PlayerPanel" .. playerIndex
        self.MainPanel.PlayerList[playerIndex] = TPlayerPanelGUI:CreateNewObjectByDesc(wtName, playerPanelDesc, self.MainPanel)
        self.MainPanel.PlayerList[playerIndex]:SetPosition(20, 47 + playerIndex * 24)
    end
    
    -- Spell list
    local spellPanelDesc = self.DetailsPanel:GetChildByName("SpellPanel"):GetDesc()
    self.DetailsPanel:GetChildByName("SpellPanel"):Destroy()
    local spellListOffset = 54
    for spellIndex = 1, MAXSPELLS do
        local wtName = "SpellPanel" .. spellIndex
        self.DetailsPanel.SpellList[spellIndex] = TSpellPanelGUI:CreateNewObjectByDesc(wtName, spellPanelDesc, self.DetailsPanel)
        self.DetailsPanel.SpellList[spellIndex]:SetPosition(22, spellListOffset + (spellIndex-1) * 18)
    end
    
    -- Spell info
    local spellInfoPanelDesc = self.DetailsPanel:GetChildByName("SpellDetailPanel"):GetDesc()
    self.DetailsPanel:GetChildByName("SpellDetailPanel"):Destroy()
    local damageOffset = spellListOffset + MAXSPELLS * 18 + 35
    for infoIndex = 1, DMGTYPES do
        local wtName = "SpellInfoPanel" .. infoIndex
        self.DetailsPanel.SpellInfoList[infoIndex] = TSpellDetailsPanelGUI:CreateNewObjectByDesc(wtName, spellInfoPanelDesc, self.DetailsPanel)
        self.DetailsPanel.SpellInfoList[infoIndex]:SetPosition(22, damageOffset + (infoIndex-1) * 18)
    end
    
    -- Spell miss
    local missOffset = damageOffset + DMGTYPES * 18 + 5
    for missIndex = 1, MISSTYPES do
        local wtName = "SpellMissPanel" .. missIndex
        self.DetailsPanel.SpellMissList[missIndex] = TSpellDetailsPanelGUI:CreateNewObjectByDesc(wtName, spellInfoPanelDesc, self.DetailsPanel)
        self.DetailsPanel.SpellMissList[missIndex]:SetPosition(22, missOffset + (missIndex-1) * 18)
    end
    
    -- Spell block
    local blockDamageOffset = missOffset + MISSTYPES * 18 + 5
    for blockIndex = 1, BLOCKDMGTYPES do
        local wtName = "SpellBlckPanel" .. blockIndex
        self.DetailsPanel.SpellBlockList[blockIndex] = TSpellDetailsPanelGUI:CreateNewObjectByDesc(wtName, spellInfoPanelDesc, self.DetailsPanel)
        self.DetailsPanel.SpellBlockList[blockIndex]:SetPosition(22, blockDamageOffset + (blockIndex-1) * 18)
    end
    
    -- Spell extra info
    local extraInfoOffset = blockDamageOffset + BLOCKDMGTYPES * 18 + 5
    for extraIndex = 1, EXTRATYPES do
        local wtName = "ExtraInfoPanel" .. extraIndex
        self.DetailsPanel.SpellExtraList[extraIndex] = TSpellDetailsPanelGUI:CreateNewObjectByDesc(wtName, spellInfoPanelDesc, self.DetailsPanel)
        self.DetailsPanel.SpellExtraList[extraIndex]:SetPosition(22, extraInfoOffset + (extraIndex-1) * 18)
    end
    
    self.DPSMeter:ResetAllFights()
    self.ActiveFight = self.DPSMeter.Fight.Current
    
    -- Backward compatibility with pre-1.1.04 versions:
    if not social.GetFriendInfo then
        mainForm:SetPriority(1000)
    end
    
    if avatar.IsExist() then
        onEvent["EVENT_AVATAR_CREATED"]()
        onEvent["EVENT_GROUP_CHANGED"]()
    end
end
