--------------------------------------------------------------------------------
-- GLOBALS
--------------------------------------------------------------------------------

Global( "RELEASE", 15 )

Global( "wtMainPanel", nil )
Global( "wtTextView", nil )
Global( "wtTextStrg", nil )
Global( "wtFooterText", nil )
Global( "wtNumView", nil )
Global( "wtTextViewBlock", {} )
Global( "wtNumViewBlock", {} )
Global( "wtTextString", {} )
Global( "wtTextHeader", nil )
Global( "wtButtonExecute", nil )
Global( "wtButtonClean", nil )
Global( "wtButtonCleanAuto", nil )
Global( "wtButtonMasterPet", nil )
Global( "wtButtonStack", nil )
Global( "wtButtonLang", nil )
Global( "wtPanelInfo", nil )
Global( "wtPanelTitle", nil )

Global("PanelTitles", {})
Global("PanelFlag", false)
Global("CleanAuto", false)
Global( "StringLeft", 0 )
Global( "StringRight", 0 )
Global( "PetExist", false )
Global( "PetId", nil )
Global( "DamageWhom", "Master" )

Global( "Language", "eng" )
Global( "wsBarrierLocalized", userMods.ToWString( "Holy Barrier" ) )

Global( "DamageMaster", {
	["DamageCount"] = 0,
	["DamageAmount"] = 0,
	["DamageMimo"] = 0,
	["FightTime"] = 0,
	["Dodge"] = 0,
	["Miss"] = 0,
	["Parry"] = 0,
	["Block"] = 0,
	["DamageBlocked"] = 0,
	["DamageParry"] = 0,
	["DamageResist"] = 0,
	["ResistElement"] = 0,
	["ResistDivine"] = 0,
	["ResistNature"] = 0,
	["DamageAbsorb"] = 0,
	["DamageBarrierIn"] = 0,
	["DamageBarrierOut"] = 0,
	["DamagePhysic"] = 0,
	["DamageMagic"] = 0,
	["DamageElement"] = 0,
	["DamageFire"] = 0,
	["DamageCold"] = 0,
	["DamageLightning"] = 0,
	["DamageDivine"] = 0,
	["DamageHoly"] = 0,
	["DamageShadow"] = 0,
	["DamageAstral"] = 0,
	["DamageNature"] = 0,
	["DamagePoison"] = 0,
	["DamageDisease"] = 0,
	["DamageAcid"] = 0,
	["DamageCrit"] = 0,
	["NumberCrit"] = 0,
	["DamageGlanc"] = 0,
	["NumberGlanc"] = 0,
	["DmgMaxPhysic"] = {},
	["DmgMaxFire"] = {},
	["DmgMaxCold"] = {},
	["DmgMaxLightning"] = {},
	["DmgMaxHoly"] = {},
	["DmgMaxShadow"] = {},
	["DmgMaxAstral"] = {},
	["DmgMaxPoison"] = {},
	["DmgMaxDisease"] = {},
	["DmgMaxAcid"] = {},
	["bCollectData"] = false
} )
Global( "DamagePet", {
	["DamageCount"] = 0,
	["DamageAmount"] = 0,
	["DamageMimo"] = 0,
	["FightTime"] = 0,
	["Dodge"] = 0,
	["Miss"] = 0,
	["Parry"] = 0,
	["Block"] = 0,
	["DamageBlocked"] = 0,
	["DamageParry"] = 0,
	["DamageResist"] = 0,
	["ResistElement"] = 0,
	["ResistDivine"] = 0,
	["ResistNature"] = 0,
	["DamageAbsorb"] = 0,
	["DamageBarrierIn"] = 0,
	["DamageBarrierOut"] = 0,
	["DamagePhysic"] = 0,
	["DamageMagic"] = 0,
	["DamageElement"] = 0,
	["DamageFire"] = 0,
	["DamageCold"] = 0,
	["DamageLightning"] = 0,
	["DamageDivine"] = 0,
	["DamageHoly"] = 0,
	["DamageShadow"] = 0,
	["DamageAstral"] = 0,
	["DamageNature"] = 0,
	["DamagePoison"] = 0,
	["DamageDisease"] = 0,
	["DamageAcid"] = 0,
	["DamageCrit"] = 0,
	["NumberCrit"] = 0,
	["DamageGlanc"] = 0,
	["NumberGlanc"] = 0,
	["DmgMaxPhysic"] = {},
	["DmgMaxFire"] = {},
	["DmgMaxCold"] = {},
	["DmgMaxLightning"] = {},
	["DmgMaxHoly"] = {},
	["DmgMaxShadow"] = {},
	["DmgMaxAstral"] = {},
	["DmgMaxPoison"] = {},
	["DmgMaxDisease"] = {},
	["DmgMaxAcid"] = {},
	["bCollectData"] = false
} )
Global( "DamageMaster2", {
	["DamageCount"] = 0,
	["DamageAmount"] = 0,
	["DamageMimo"] = 0,
	["FightTime"] = 0,
	["Dodge"] = 0,
	["Miss"] = 0,
	["Parry"] = 0,
	["Block"] = 0,
	["DamageBlocked"] = 0,
	["DamageParry"] = 0,
	["DamageResist"] = 0,
	["ResistElement"] = 0,
	["ResistDivine"] = 0,
	["ResistNature"] = 0,
	["DamageAbsorb"] = 0,
	["DamageBarrierIn"] = 0,
	["DamageBarrierOut"] = 0,
	["DamagePhysic"] = 0,
	["DamageMagic"] = 0,
	["DamageElement"] = 0,
	["DamageFire"] = 0,
	["DamageCold"] = 0,
	["DamageLightning"] = 0,
	["DamageDivine"] = 0,
	["DamageHoly"] = 0,
	["DamageShadow"] = 0,
	["DamageAstral"] = 0,
	["DamageNature"] = 0,
	["DamagePoison"] = 0,
	["DamageDisease"] = 0,
	["DamageAcid"] = 0,
	["DamageCrit"] = 0,
	["NumberCrit"] = 0,
	["DamageGlanc"] = 0,
	["NumberGlanc"] = 0,
	["DmgMaxPhysic"] = {},
	["DmgMaxFire"] = {},
	["DmgMaxCold"] = {},
	["DmgMaxLightning"] = {},
	["DmgMaxHoly"] = {},
	["DmgMaxShadow"] = {},
	["DmgMaxAstral"] = {},
	["DmgMaxPoison"] = {},
	["DmgMaxDisease"] = {},
	["DmgMaxAcid"] = {},
	["bCollectData"] = false
} )
Global( "DamagePet2", {
	["DamageCount"] = 0,
	["DamageAmount"] = 0,
	["DamageMimo"] = 0,
	["FightTime"] = 0,
	["Dodge"] = 0,
	["Miss"] = 0,
	["Parry"] = 0,
	["Block"] = 0,
	["DamageBlocked"] = 0,
	["DamageParry"] = 0,
	["DamageResist"] = 0,
	["ResistElement"] = 0,
	["ResistDivine"] = 0,
	["ResistNature"] = 0,
	["DamageAbsorb"] = 0,
	["DamageBarrierIn"] = 0,
	["DamageBarrierOut"] = 0,
	["DamagePhysic"] = 0,
	["DamageMagic"] = 0,
	["DamageElement"] = 0,
	["DamageFire"] = 0,
	["DamageCold"] = 0,
	["DamageLightning"] = 0,
	["DamageDivine"] = 0,
	["DamageHoly"] = 0,
	["DamageShadow"] = 0,
	["DamageAstral"] = 0,
	["DamageNature"] = 0,
	["DamagePoison"] = 0,
	["DamageDisease"] = 0,
	["DamageAcid"] = 0,
	["DamageCrit"] = 0,
	["NumberCrit"] = 0,
	["DamageGlanc"] = 0,
	["NumberGlanc"] = 0,
	["DmgMaxPhysic"] = {},
	["DmgMaxFire"] = {},
	["DmgMaxCold"] = {},
	["DmgMaxLightning"] = {},
	["DmgMaxHoly"] = {},
	["DmgMaxShadow"] = {},
	["DmgMaxAstral"] = {},
	["DmgMaxPoison"] = {},
	["DmgMaxDisease"] = {},
	["DmgMaxAcid"] = {},
	["bCollectData"] = false
} )
DamageMaster.DmgMaxPhysic.amount = 0
DamageMaster.DmgMaxFire.amount = 0
DamageMaster.DmgMaxCold.amount = 0
DamageMaster.DmgMaxLightning.amount = 0
DamageMaster.DmgMaxHoly.amount = 0
DamageMaster.DmgMaxShadow.amount = 0
DamageMaster.DmgMaxAstral.amount = 0
DamageMaster.DmgMaxPoison.amount = 0
DamageMaster.DmgMaxDisease.amount = 0
DamageMaster.DmgMaxAcid.amount = 0

DamageMaster2.DmgMaxPhysic.amount = 0
DamageMaster2.DmgMaxFire.amount = 0
DamageMaster2.DmgMaxCold.amount = 0
DamageMaster2.DmgMaxLightning.amount = 0
DamageMaster2.DmgMaxHoly.amount = 0
DamageMaster2.DmgMaxShadow.amount = 0
DamageMaster2.DmgMaxAstral.amount = 0
DamageMaster2.DmgMaxPoison.amount = 0
DamageMaster2.DmgMaxDisease.amount = 0
DamageMaster2.DmgMaxAcid.amount = 0

DamagePet.DmgMaxPhysic.amount = 0
DamagePet.DmgMaxFire.amount = 0
DamagePet.DmgMaxCold.amount = 0
DamagePet.DmgMaxLightning.amount = 0
DamagePet.DmgMaxHoly.amount = 0
DamagePet.DmgMaxShadow.amount = 0
DamagePet.DmgMaxAstral.amount = 0
DamagePet.DmgMaxPoison.amount = 0
DamagePet.DmgMaxDisease.amount = 0
DamagePet.DmgMaxAcid.amount = 0

DamagePet2.DmgMaxPhysic.amount = 0
DamagePet2.DmgMaxFire.amount = 0
DamagePet2.DmgMaxCold.amount = 0
DamagePet2.DmgMaxLightning.amount = 0
DamagePet2.DmgMaxHoly.amount = 0
DamagePet2.DmgMaxShadow.amount = 0
DamagePet2.DmgMaxAstral.amount = 0
DamagePet2.DmgMaxPoison.amount = 0
DamagePet2.DmgMaxDisease.amount = 0
DamagePet2.DmgMaxAcid.amount = 0

Global( "Stack", 1 )

Global( "DamageMax", {} )
Global( "DamageMax2", {} )
DamageMax.amount = 0
DamageMax2.amount = 0
Global ("TypeDamage", {} )

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function SetMapMarkerPos( marker, charX, charY )
	local mapPlace = marker:GetPlacementPlain()
	mapPlace.posX = charX;
	mapPlace.posY = charY;
	marker:SetPlacementPlain( mapPlace )
end

function SetParamsString (block, row, align, damage, color)
	local PosX = 20
	local PosY = 60
	local proc = nil
	local sdvig = 0
	local DamageData = MasterPetAmount(DamageWhom)
	if not color then color = "tip_yellow" end
	if align == "Damage" then
		PosX = 20
		PosY = PosY + 20
	elseif align == "center" then
		PosX = 160
	elseif align == "Mimo" then
		PosX = 260
		PosY = PosY + 20
	elseif align == "DPS" then
		PosX = 20
		PosY = PosY + 40 + 20 * StringLeft
	elseif align == "STAT" then
		PosX = 260
		PosY = PosY + 40 + 20 * StringRight
	elseif align == "MAX" then
		PosX = 260
		PosY = PosY + 40 + 20 * StringRight
	end
	if row > 0 and align ~= "STAT" then
		sdvig = sdvig + 5
		if (block > 3 and block < 18)  or (block > 26 and block < 30) then
			sdvig = sdvig + 5
		end
		if (block > 8 and block < 18) then
			sdvig = sdvig + 5
		end
	end
	SetMapMarkerPos( wtTextViewBlock[block], PosX + sdvig, PosY + (row*20) )
	SetMapMarkerPos( wtNumViewBlock[block], PosX + 135, PosY + (row*20) )
	
	wtTextViewBlock[block]:SetVal( "TEXT", GetTextLocalized( align .. block ) )
	PanelTitles["Slot_"..block] = GetTextLocalized( align .. block )
	wtTextViewBlock[block]:SetClassVal( "CLASS", color )
	if type(damage) == "userdata" then
		wtNumViewBlock[block]:SetVal( "NUM", damage )
	else
		wtNumViewBlock[block]:SetVal( "NUM", common.FormatFloat( damage , "%f3K5" ) )
		if (align == "Damage" or align == "Mimo") and row == 0 then
			proc = damage / (DamageData.DamageAmount + DamageData.DamageMimo) * 100
		elseif align == "Damage" and row > 0 then
			proc = damage / DamageData.DamageAmount * 100
		elseif align == "Mimo" and row > 0 then
			proc = damage / DamageData.DamageMimo * 100
		elseif align == "DPS" and damage < DamageData.DamageCount and damage > 0 then
			proc = damage / DamageData.DamageCount * 100
		end
	end
	if proc then
		wtNumViewBlock[block]:SetVal( "PROC1", GetTextLocalized( "proc1" ) )
		wtNumViewBlock[block]:SetVal( "PROC", common.FormatFloat( proc , "%.1f" ) )
		wtNumViewBlock[block]:SetVal( "PROC2", GetTextLocalized( "proc2" ) )
	end
	wtTextViewBlock[block]:Show(true)
	wtNumViewBlock[block]:Show(true)
end

function SetDPSMeterValues( DamageData )

	StringLeft = 0
	StringRight = 0
	SetParamsString (0, 0, "center", DamageData.DamageAmount + DamageData.DamageMimo)
	local rowLeft = 0
	local rowRight = 0
-- блок полученного урона
	if DamageData.DamageAmount > 0 then					-- весь полученный урон
		StringLeft = StringLeft + 1
		SetParamsString (1, 0, "Damage", DamageData.DamageAmount)
		rowLeft = 1
		if DamageData.DamagePhysic > 0 then				-- тип урона физический
			StringLeft = StringLeft + 1
			SetParamsString (2, rowLeft, "Damage", DamageData.DamagePhysic, "Epic")
			rowLeft = rowLeft + 1
		end
		if DamageData.DamageBarrierOut > 0 then				-- тип урона из барьера
			StringLeft = StringLeft + 1
			SetParamsString (4, rowLeft, "Damage", DamageData.DamageBarrierOut, "tip_purple")
			rowLeft = rowLeft + 1
		end
		if DamageData.DamageMagic > 0 then				-- тип урона магический
			StringLeft = StringLeft + 1
			SetParamsString (3, rowLeft, "Damage", DamageData.DamageMagic, "tip_blue")
			rowLeft = rowLeft + 1
		end
		
		if DamageData.DamageElement > 0 then				-- тип урона стихийный (холод, огонь, эл-во)
			StringLeft = StringLeft + 1
			SetParamsString (6, rowLeft, "Damage", DamageData.DamageElement, "tip_red")
			rowLeft = rowLeft + 1
		end
		if DamageData.DamageCold > 0 then				-- тип урона холод
			StringLeft = StringLeft + 1
			SetParamsString (9, rowLeft, "Damage", DamageData.DamageCold, "tip_red")
			rowLeft = rowLeft + 1
		end
		if DamageData.DamageLightning > 0 then				-- тип урона электричество
			StringLeft = StringLeft + 1
			SetParamsString (10, rowLeft, "Damage", DamageData.DamageLightning, "tip_red")
			rowLeft = rowLeft+ 1
		end
		if DamageData.DamageFire > 0 then				-- тип урона огонь
			StringLeft = StringLeft + 1
			SetParamsString (11, rowLeft, "Damage", DamageData.DamageFire, "tip_red")
			rowLeft = rowLeft + 1
		end
		if DamageData.DamageDivine > 0 then				-- тип урона божественный (свет, тень, астрал)
			StringLeft = StringLeft + 1
			SetParamsString (7, rowLeft, "Damage", DamageData.DamageDivine, "tip_golden")
			rowLeft = rowLeft + 1
		end
		if DamageData.DamageHoly > 0 then				-- тип урона свет
			StringLeft = StringLeft + 1
			SetParamsString (12, rowLeft, "Damage", DamageData.DamageHoly, "tip_golden")
			rowLeft = rowLeft + 1
		end
		if DamageData.DamageShadow > 0 then				-- тип урона тень
			StringLeft = StringLeft + 1
			SetParamsString (13, rowLeft, "Damage", DamageData.DamageShadow, "tip_golden")
			rowLeft = rowLeft + 1
		end
		if DamageData.DamageAstral > 0 then				-- тип урона астрал
			StringLeft = StringLeft + 1
			SetParamsString (14, rowLeft, "Damage", DamageData.DamageAstral, "tip_golden")
			rowLeft = rowLeft + 1
		end
		if DamageData.DamageNature > 0 then				-- тип урона природный (яд, болезнь, кислота)
			StringLeft = StringLeft + 1
			SetParamsString (8, rowLeft, "Damage", DamageData.DamageNature, "tip_green")
			rowLeft = rowLeft + 1
		end
		if DamageData.DamagePoison > 0 then				-- тип урона яд
			StringLeft = StringLeft + 1
			SetParamsString (17, rowLeft, "Damage", DamageData.DamagePoison, "tip_green")
			rowLeft = rowLeft + 1
		end
		if DamageData.DamageDisease > 0 then				-- тип урона болезнь
			StringLeft = StringLeft + 1
			SetParamsString (16, rowLeft, "Damage", DamageData.DamageDisease, "tip_green")
			rowLeft = rowLeft + 1
		end
		if DamageData.DamageAcid > 0 then				-- тип урона кислота
			StringLeft = StringLeft + 1
			SetParamsString (15, rowLeft, "Damage", DamageData.DamageAcid, "tip_green")
			rowLeft = rowLeft + 1
		end
	end

-- блок неполученного урона
	if DamageData.DamageMimo > 0 then					-- весь неполученный урон
		StringRight = StringRight + 1
		SetParamsString (20, 0, "Mimo", DamageData.DamageMimo)
		rowRight = 1
		if DamageData.DamageBlocked > 0 then				-- тип блока - блок
			StringRight = StringRight + 1
			SetParamsString (21, rowRight, "Mimo", DamageData.DamageBlocked, "Epic")
			rowRight = rowRight + 1
		end
		if DamageData.DamageParry > 0 then				-- тип блока - парирование
			StringRight = StringRight + 1
			SetParamsString (22, rowRight, "Mimo", DamageData.DamageParry, "Epic")
			rowRight = rowRight + 1
		end
		if DamageData.DamageAbsorb > 0 then				-- тип блока - поглощение
			StringRight = StringRight + 1
			SetParamsString (23, rowRight, "Mimo", DamageData.DamageAbsorb, "Rare")
			rowRight = rowRight + 1
		end
		if DamageData.DamageBarrierIn > 0 then				-- тип блока - поглощение барьером
			StringRight = StringRight + 1
			SetParamsString (24, rowRight, "Mimo", DamageData.DamageBarrierIn, "tip_purple")
			rowRight = rowRight + 1
		end
		if DamageData.Dodge > 0 then					-- тип блока - в результате уворота (теория)
			StringRight = StringRight + 1
			local dodge = math.ceil (DamageData.DamageAmount / DamageData.DamageCount * DamageData.Dodge)
			SetParamsString (25, rowRight, "Mimo", dodge, "tip_grey")
			rowRight = rowRight + 1
		end
		if DamageData.DamageResist > 0 then				-- тип блока - сопротивление
			StringRight = StringRight + 1
			SetParamsString (26, rowRight, "Mimo", DamageData.DamageResist, "tip_blue")
			rowRight = rowRight + 1
		end
		if DamageData.ResistElement > 0 then				-- тип блока - сопротивление стихии
			StringRight = StringRight + 1
			SetParamsString (27, rowRight, "Mimo", DamageData.ResistElement, "tip_red")
			rowRight = rowRight + 1
		end
		if DamageData.ResistDivine > 0 then				-- тип блока - сопротивление божеств.
			StringRight = StringRight + 1
			SetParamsString (28, rowRight, "Mimo", DamageData.ResistDivine, "tip_golden")
			rowRight = rowRight + 1
		end
		if DamageData.ResistNature > 0 then				-- тип блока - сопротивление природе
			StringRight = StringRight + 1
			SetParamsString (29, rowRight, "Mimo", DamageData.ResistNature, "tip_green")
			rowRight = rowRight + 1
		end
	end

										-- блок ДПС
	if DamageData.DamageCount > 0 then
		SetParamsString (30, 0, "DPS", DamageData.DamageCount)
		rowLeft = 1
										-- кол-во критических ударов
		if DamageData.NumberCrit > 0 then
			SetParamsString (33, rowLeft, "DPS", DamageData.NumberCrit)
			rowLeft = rowLeft + 1
		end
										-- кол-во неудачных ударов
		if DamageData.NumberGlanc > 0 then
			SetParamsString (34, rowLeft, "DPS", DamageData.NumberGlanc)
			rowLeft = rowLeft + 1
		end
										-- кол-во уворотов
		if DamageData.Dodge > 0 then
			SetParamsString (35, rowLeft, "DPS", DamageData.Dodge)
			rowLeft = rowLeft + 1
		end
										-- кол-во промахов
		if DamageData.Miss > 0 then
			SetParamsString (36, rowLeft, "DPS", DamageData.Miss)
			rowLeft = rowLeft + 1
		end
										-- кол-во парирований
		if DamageData.Parry > 0 then
			SetParamsString (37, rowLeft, "DPS", DamageData.Parry, "Epic")
			rowLeft = rowLeft + 1
		end
										-- кол-во блоков
		if DamageData.Block > 0 then
			SetParamsString (38, rowLeft, "DPS", DamageData.Block, "Epic")
			rowLeft = rowLeft + 1
		end
	end

	rowRight = 0	
	if DamageData.FightTime > 0 then					-- статистика "STAT"

		local time = DamageData.FightTime				-- время
		local Minute = 0
		local Second = 0
		if time > 59 then
			Minute = math.floor (time/60)		
			Second = time - Minute * 60
		else
			Second = DamageData["FightTime"]
		end
		if Second < 10 then
			Second = "0" .. Second
		end
		if Minute < 10 then
			Minute = "0" .. Minute
		end
		if avatar.GetEmotes then
			local LocMin = userMods.FromWString (GetTextLocalized( "Time_minute" ))
			local LocSec = userMods.FromWString (GetTextLocalized( "Time_second" ))
			time = userMods.ToWString( Minute .. LocMin .. Second .. LocSec)
		end
		SetParamsString (40, rowRight, "STAT", time)
		rowRight = rowRight + 1
	end
	if DamageData.DamageCount > 0 then							-- средний урон
		local middle = math.floor (DamageData.DamageAmount/DamageData.DamageCount)
		SetParamsString (41, rowRight, "STAT", middle)
		rowRight = rowRight + 1
	end
	if DamageMax.amount > 0 then								-- "MAX"
		rowRight = rowRight + 1
		SetParamsString (50, rowRight, "MAX", DamageMax.amount)				-- максимальный урон
		rowRight = rowRight + 1
		SetParamsString (51, rowRight, "MAX", DamageMax.sourceName)			-- имя, нанесшего макс. урон
		rowRight = rowRight + 1
		SetParamsString (52, rowRight, "MAX", TypeDamage[DamageMax.sysSubElement])	-- тип урона
		if DamageMax.isCritical then							-- тип удара (крит некрит)
			rowRight = rowRight + 1
			SetParamsString (53, rowRight, "MAX", GetTextLocalized( "Crit" ))
		elseif DamageMax.isGlancing then
			rowRight = rowRight + 1
			SetParamsString (53, rowRight, "MAX", GetTextLocalized( "Clanc" ))
		else
			wtTextViewBlock[53]:Show(false)
			wtNumViewBlock[53]:Show(false)
		end
		rowRight = rowRight + 1
		if DamageMax.ability then
		--LogInfo (DamageMax.sysActionSourceType, "-::-", DamageMax.ability, "<")
			SetParamsString (54, rowRight, "MAX", DamageMax.ability)		-- чем нанесен урон
		--	if userMods then 
		--		SetParamsString (54, rowRight, "MAX", userMods.ToWString(DamageMax.sysActionSourceType))
		--	end
			rowRight = rowRight + 1
		end
	end
	local maxY = 0
	if StringLeft + rowLeft < StringRight + rowRight then
		maxY = StringRight + rowRight
	else
		maxY = StringLeft + rowLeft
	end
--	LogInfo (StringRight, "--", rowRight)
	SetMapMarkerPos( wtFooterText, 0, maxY * 20 + 105 )
	local placement = wtMainPanel:GetPlacementPlain()
	placement.sizeY = maxY * 20 + 130
	wtMainPanel:SetPlacementPlain( placement )
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- REACTION HANDLERS
--------------------------------------------------------------------------------
 -- "EVENT_SECOND_TIMER"
function OnEventSecondTimer( params )
	if DamageMaster["bCollectData"] and unit.IsInCombat( avatar.GetId() ) then
		DamageMaster["FightTime"] = DamageMaster["FightTime"] + 1
	end
end
--------------------------------------------------------------------------------
function MasterPetAmount ( Whom )
	local DamageData = {}
	if Whom == "Master" then
		DamageData = DamageMaster
	elseif Whom == "Pet" then
		DamageData = DamagePet
	elseif Whom == "Amount" then
		DamageData = {
			["DamageCount"] = DamageMaster.DamageCount + DamagePet.DamageCount,
			["DamageAmount"] = DamageMaster.DamageAmount + DamagePet.DamageAmount,
			["DamageMimo"] = DamageMaster.DamageMimo + DamagePet.DamageMimo,
			["FightTime"] = DamageMaster.FightTime,
			["Dodge"] = DamageMaster.Dodge + DamagePet.Dodge,
			["Miss"] = DamageMaster.Miss + DamagePet.Miss,
			["Block"] = DamageMaster.Block + DamagePet.Block,
			["Parry"] = DamageMaster.Parry + DamagePet.Parry,
			["DamageBlocked"] = DamageMaster.DamageBlocked + DamagePet.DamageBlocked, 
			["DamageParry"] = DamageMaster.DamageParry + DamagePet.DamageParry,
			["DamageResist"] = DamageMaster.DamageResist + DamagePet.DamageResist,
			["ResistElement"] = DamageMaster.ResistElement + DamagePet.ResistElement,
			["ResistDivine"] = DamageMaster.ResistDivine + DamagePet.ResistDivine,
			["ResistNature"] = DamageMaster.ResistNature + DamagePet.ResistNature,
			["DamageAbsorb"] = DamageMaster.DamageAbsorb + DamagePet.DamageAbsorb,
			["DamageBarrierIn"] = DamageMaster.DamageBarrierIn + DamagePet.DamageBarrierIn,
			["DamageBarrierOut"] = DamageMaster.DamageBarrierOut + DamagePet.DamageBarrierOut,
			["DamagePhysic"] = DamageMaster.DamagePhysic + DamagePet.DamagePhysic,
			["DamageMagic"] = DamageMaster.DamageMagic + DamagePet.DamageMagic,
			["DamageElement"] = DamageMaster.DamageElement + DamagePet.DamageElement,
			["DamageFire"] = DamageMaster.DamageFire + DamagePet.DamageFire,
			["DamageCold"] = DamageMaster.DamageCold + DamagePet.DamageCold,
			["DamageLightning"] = DamageMaster.DamageLightning + DamagePet.DamageLightning,
			["DamageDivine"] = DamageMaster.DamageDivine + DamagePet.DamageDivine,
			["DamageHoly"] = DamageMaster.DamageHoly + DamagePet.DamageHoly,
			["DamageShadow"] = DamageMaster.DamageShadow + DamagePet.DamageShadow,
			["DamageAstral"] = DamageMaster.DamageAstral + DamagePet.DamageAstral,
			["DamageNature"] = DamageMaster.DamageNature + DamagePet.DamageNature,
			["DamagePoison"] = DamageMaster.DamagePoison + DamagePet.DamagePoison,
			["DamageDisease"] = DamageMaster.DamageDisease + DamagePet.DamageDisease,
			["DamageAcid"] = DamageMaster.DamageAcid + DamagePet.DamageAcid,
			["DamageCrit"] = DamageMaster.DamageCrit + DamagePet.DamageCrit,
			["NumberCrit"] = DamageMaster.NumberCrit + DamagePet.NumberCrit,
			["DamageGlanc"] = DamageMaster.DamageGlanc + DamagePet.DamageGlanc,
			["NumberGlanc"] = DamageMaster.NumberGlanc + DamagePet.NumberGlanc
		}
		if DamageMaster.DmgMaxPhysic.amount >= DamagePet.DmgMaxPhysic.amount then
			DamageData.DmgMaxPhysic = DamageMaster.DmgMaxPhysic
		else
			DamageData.DmgMaxPhysic = DamagePet.DmgMaxPhysic
		end
		if DamageMaster.DmgMaxFire.amount >= DamagePet.DmgMaxFire.amount then
			DamageData.DmgMaxFire = DamageMaster.DmgMaxFire
		else
			DamageData.DmgMaxFire = DamagePet.DmgMaxFire
		end
		if DamageMaster.DmgMaxCold.amount >= DamagePet.DmgMaxCold.amount then
			DamageData.DmgMaxCold = DamageMaster.DmgMaxCold
		else
			DamageData.DmgMaxCold = DamagePet.DmgMaxCold
		end
		if DamageMaster.DmgMaxLightning.amount >= DamagePet.DmgMaxLightning.amount then
			DamageData.DmgMaxLightning = DamageMaster.DmgMaxLightning
		else
			DamageData.DmgMaxLightning = DamagePet.DmgMaxLightning
		end
		if DamageMaster.DmgMaxHoly.amount >= DamagePet.DmgMaxHoly.amount then
			DamageData.DmgMaxHoly = DamageMaster.DmgMaxHoly
		else
			DamageData.DmgMaxHoly = DamagePet.DmgMaxHoly
		end
		if DamageMaster.DmgMaxShadow.amount >= DamagePet.DmgMaxShadow.amount then
			DamageData.DmgMaxShadow = DamageMaster.DmgMaxShadow
		else
			DamageData.DmgMaxShadow = DamagePet.DmgMaxShadow
		end
		if DamageMaster.DmgMaxAstral.amount >= DamagePet.DmgMaxAstral.amount then
			DamageData.DmgMaxAstral = DamageMaster.DmgMaxAstral
		else
			DamageData.DmgMaxAstral = DamagePet.DmgMaxAstral
		end
		if DamageMaster.DmgMaxPoison.amount >= DamagePet.DmgMaxPoison.amount then
			DamageData.DmgMaxPoison = DamageMaster.DmgMaxPoison
		else
			DamageData.DmgMaxPoison = DamagePet.DmgMaxPoison
		end
		if DamageMaster.DmgMaxDisease.amount >= DamagePet.DmgMaxDisease.amount then
			DamageData.DmgMaxDisease = DamageMaster.DmgMaxDisease
		else
			DamageData.DmgMaxDisease = DamagePet.DmgMaxDisease
		end
		if DamageMaster.DmgMaxAcid.amount >= DamagePet.DmgMaxAcid.amount then
			DamageData.DmgMaxAcid = DamageMaster.DmgMaxAcid
		else
			DamageData.DmgMaxAcid = DamagePet.DmgMaxAcid
		end

	end
	return DamageData
end
function onAvatarCombatStatusChanged( params )
	if params.inCombat and CleanAuto then
		DamageClean(DamageMaster)
		DamageClean(DamagePet)
		DamageMaster["bCollectData"] = true
	else
		DamageMaster["bCollectData"] = false
	end
	if params.inCombat and not CleanAuto then
		DamageMaster["bCollectData"] = true
	end
	SetDPSMeterValues(MasterPetAmount(DamageWhom))
end

function DamageClean(DamageData)

	for i = 1, 60 do
		wtTextViewBlock[i]:Show(false)
		wtNumViewBlock[i]:Show(false)
	end
	DamageMax.amount = 0

	DamageData["DamageCount"] = 0
	DamageData["DamageAmount"] = 0
	DamageData["DamageMimo"] = 0
	DamageData["FightTime"] = 0  
	DamageData["Dodge"] = 0
	DamageData["Miss"] = 0
	DamageData["Block"] = 0
	DamageData["Parry"] = 0
	DamageData["DamageBlocked"] = 0 
	DamageData["DamageParry"] = 0
	DamageData["DamageResist"] = 0
	DamageData["ResistElement"] = 0
	DamageData["ResistDivine"] = 0
	DamageData["ResistNature"] = 0
	DamageData["DamageAbsorb"] = 0
	DamageData["DamageBarrierIn"] = 0
	DamageData["DamageBarrierOut"] = 0
	DamageData["DamagePhysic"] = 0
	DamageData["DamageMagic"] = 0
	DamageData["DamageElement"] = 0
	DamageData["DamageFire"] = 0
	DamageData["DamageCold"] = 0
	DamageData["DamageLightning"] = 0
	DamageData["DamageDivine"] = 0
	DamageData["DamageHoly"] = 0
	DamageData["DamageShadow"] = 0
	DamageData["DamageAstral"] = 0
	DamageData["DamageNature"] = 0
	DamageData["DamagePoison"] = 0
	DamageData["DamageDisease"] = 0
	DamageData["DamageAcid"] = 0
	DamageData["DamageCrit"] = 0
	DamageData["NumberCrit"] = 0
	DamageData["DamageGlanc"] = 0
	DamageData["NumberGlanc"] = 0
	
	DamageData.DmgMaxPhysic.amount = 0
	DamageData.DmgMaxFire.amount = 0
	DamageData.DmgMaxCold.amount = 0
	DamageData.DmgMaxLightning.amount = 0
	DamageData.DmgMaxHoly.amount = 0
	DamageData.DmgMaxShadow.amount = 0
	DamageData.DmgMaxAstral.amount = 0
	DamageData.DmgMaxPoison.amount = 0
	DamageData.DmgMaxDisease.amount = 0
	DamageData.DmgMaxAcid.amount = 0
end

function onDamageReceived( params )
local db = 0
	if params then
		if params.target == avatar.GetId() then
			DamageMaster = DamageRecord(DamageMaster, params)
		--	LogInfo ("урон хозяину")
			SetDPSMeterValues(MasterPetAmount(DamageWhom))
		elseif PetExist and params.target == PetId then
			DamagePet = DamageRecord(DamagePet, params)
		--	LogInfo ("урон пету")
			SetDPSMeterValues(MasterPetAmount(DamageWhom))
		end
	end

end

function DamageRecord (DamageData, params)
	DamageData["DamageCount"] = DamageData["DamageCount"] + 1
	if params.amount then 
	--	LogInfo(params.sysActionSourceType)
	--	LogInfo(params.ability)
		DamageData["DamageAmount"] = DamageData["DamageAmount"] + params.amount
		DamageData["DamageBlocked"] = DamageData["DamageBlocked"] + params.shieldBlock
		DamageData["DamageParry"] = DamageData["DamageParry"] + params.parry
		DamageData["DamageResist"] = DamageData["DamageResist"] + params.resist
		DamageData["DamageAbsorb"] = DamageData["DamageAbsorb"] + params.absorb
		DamageData["DamageBarrierIn"] = DamageData["DamageBarrierIn"] + params.barrier
		DamageData.DamageMimo = DamageData.DamageMimo + params.shieldBlock + params.parry + params.resist + params.absorb + params.barrier
		if params.amount > DamageMax.amount then
			DamageMax = params
		end
		if common.CompareWString( params.ability, wsBarrierLocalized ) == 0 or params.sysActionSourceType == "Barrier" then
			DamageData.DamageMimo = DamageData.DamageMimo - params.amount
			DamageData["DamageBarrierIn"] = DamageData["DamageBarrierIn"] - params.amount
			DamageData["DamageBarrierOut"] = DamageData["DamageBarrierOut"] + params.amount
		end
	--------------------
	--- Увороты
	--------------------	
		if params.isDodge == true then
			DamageData["Dodge"] = DamageData["Dodge"] +  1
		end
	--------------------
	--- Промахи
	--------------------	
		if params.isMiss == true then
			DamageData["Miss"] = DamageData["Miss"] + 1
		end
	--------------------
	--- Кол-во паррирований
	--------------------	
		if params.parry > 0 then
			DamageData["Parry"] = DamageData["Parry"] + 1
		end
	--------------------
	--- Кол-во блоков
	--------------------	
		if params.shieldBlock > 0 then
			DamageData["Block"] = DamageData["Block"] + 1
		end
	--------------------
	--- "Критические повреждения"
	--------------------	
		if params.isCritical == true then
			DamageData["DamageCrit"] = DamageData["DamageCrit"] +  params.amount
			DamageData["NumberCrit"] = DamageData["NumberCrit"] + 1
		end  
	--------------------
	--- "Неудачный удары"
	--------------------
		if params.isGlancing == true then
			DamageData["DamageGlanc"] = DamageData["DamageGlanc"] +  params.amount
			DamageData["NumberGlanc"] = DamageData["NumberGlanc"] + 1
		end  
	---------------------
	-- "ENUM_SubElement_PHYSICAL"
	---------------------
		if params.sysSubElement == "ENUM_SubElement_PHYSICAL" then
			DamageData["DamagePhysic"] = DamageData["DamagePhysic"] +  params.amount
			if DamageData.DmgMaxPhysic.amount < params.amount then
				DamageData.DmgMaxPhysic = params
			end
		else
			DamageData["DamageMagic"] = DamageData["DamageMagic"] + params.amount
		end
	----------------------------    
	-- "ENUM_SubElement_FIRE"
	----------------------------
		if params.sysSubElement == "ENUM_SubElement_FIRE" then
			DamageData["DamageFire"] = DamageData["DamageFire"] +  params.amount
			DamageData["DamageElement"] = DamageData["DamageElement"] + params.amount
			DamageData["ResistElement"] = DamageData["ResistElement"] + params.resist
			if DamageData.DmgMaxFire.amount < params.amount then
				DamageData.DmgMaxFire = params
			end
		end
	----------------------------    
	-- "ENUM_SubElement_COLD"
	----------------------------
		if params.sysSubElement == "ENUM_SubElement_COLD" then
			DamageData["DamageCold"] = DamageData["DamageCold"] +  params.amount
			DamageData["DamageElement"] = DamageData["DamageElement"] + params.amount
			DamageData["ResistElement"] = DamageData["ResistElement"] + params.resist
			if DamageData.DmgMaxCold.amount < params.amount then
				DamageData.DmgMaxCold = params
			end
		end
	----------------------------    
	-- "ENUM_SubElement_LIGHTNING"
	----------------------------
		if params.sysSubElement == "ENUM_SubElement_LIGHTNING" then
			DamageData["DamageLightning"] = DamageData["DamageLightning"] + params.amount
			DamageData["DamageElement"] = DamageData["DamageElement"] + params.amount
			DamageData["ResistElement"] = DamageData["ResistElement"] + params.resist
			if DamageData.DmgMaxLightning.amount < params.amount then
				DamageData.DmgMaxLightning = params
			end
		end
	----------------------------
	-- "ENUM_SubElement_HOLY"
	----------------------------
		if params.sysSubElement == "ENUM_SubElement_HOLY" then
			DamageData["DamageHoly"] = DamageData["DamageHoly"] + params.amount
			DamageData["DamageDivine"] = DamageData["DamageDivine"] + params.amount
			DamageData["ResistDivine"] = DamageData["ResistDivine"] + params.resist
			if DamageData.DmgMaxHoly.amount < params.amount then
				DamageData.DmgMaxHoly = params
			end
		end
	----------------------------
	-- "ENUM_SubElement_SHADOW"
	----------------------------
		if params.sysSubElement == "ENUM_SubElement_SHADOW" then
			DamageData["DamageShadow"] = DamageData["DamageShadow"] + params.amount
			DamageData["DamageDivine"] = DamageData["DamageDivine"] + params.amount
			DamageData["ResistDivine"] = DamageData["ResistDivine"] + params.resist
			if DamageData.DmgMaxShadow.amount < params.amount then
				DamageData.DmgMaxShadow = params
			end
		end
	----------------------------    
	-- "ENUM_SubElement_ASTRAL"
	----------------------------
		if params.sysSubElement == "ENUM_SubElement_ASTRAL" then
			DamageData["DamageAstral"] = DamageData["DamageAstral"] + params.amount
			DamageData["DamageDivine"] = DamageData["DamageDivine"] + params.amount
			DamageData["ResistDivine"] = DamageData["ResistDivine"] + params.resist
			if DamageData.DmgMaxShadow.amount < params.amount then
				DamageData.DmgMaxShadow = params
			end
		end  
	----------------------------    
	-- "ENUM_SubElement_POISON"
	----------------------------
		if params.sysSubElement == "ENUM_SubElement_POISON" then
			DamageData["DamagePoison"] = DamageData["DamagePoison"] + params.amount
			DamageData["DamageNature"] = DamageData["DamageNature"] + params.amount
			DamageData["ResistNature"] = DamageData["ResistNature"] + params.resist
			if DamageData.DmgMaxPoison.amount < params.amount then
				DamageData.DmgMaxPoison = params
			end
		end  
	----------------------------    
	-- "ENUM_SubElement_DISEASE"
	----------------------------
		if params.sysSubElement == "ENUM_SubElement_DISEASE" then
			DamageData["DamageDisease"] = DamageData["DamageDisease"] + params.amount
			DamageData["DamageNature"] = DamageData["DamageNature"] + params.amount
			DamageData["ResistNature"] = DamageData["ResistNature"] + params.resist
			if DamageData.DmgMaxDisease.amount < params.amount then
				DamageData.DmgMaxDisease = params
			end
		end
	----------------------------
	-- "ENUM_SubElement_ACID"
	----------------------------
		if params.sysSubElement == "ENUM_SubElement_ACID" then
			DamageData["DamageAcid"] = DamageData["DamageAcid"] +  params.amount
			DamageData["DamageNature"] = DamageData["DamageNature"] + params.amount
			DamageData["ResistNature"] = DamageData["ResistNature"] + params.resist
			if DamageData.DmgMaxAcid.amount < params.amount then
				DamageData.DmgMaxAcid = params
			end
		end
	end
	return DamageData
end


-- "execute"
function OnReactionExecute( params )
	if DnD:IsDragging() then return end
	PanelFlag = not PanelFlag
	wtMainPanel:Show( PanelFlag )
end

function OnReactionClean( params )
	DamageClean(DamageMaster)
	DamageClean(DamagePet)
	onPetChanged()
end

function OnReactionCleanAuto( params )
	if CleanAuto then
		wtButtonCleanAuto:SetVal( "button_label", GetTextLocalized( "OFF" ) )
		CleanAuto = false
	else
		wtButtonCleanAuto:SetVal( "button_label", GetTextLocalized( "ON" ) )
		CleanAuto = true
	end
end

function onDamageBarrier (params )
--	DamageData["DamageBarrierOut"] = DamageData["DamageBarrierOut"] + params.damage
--	LogInfo (DamageData["DamageBarrierOut"])
end
function onDamageBarrierABSORBED (params )
--	dmgbar = dmgbar + params.damage
--	LogInfo ("Absorbed (поглощение): ", dmgbar)
end



-- "EVENT_AVATAR_CREATED"
function onAvatarCreated()
	local GameLanguage = GetGameLocalization()
	if common.GetAddonRelatedTextGroup( "Barrier" ):HasText( GameLanguage ) then
		wsBarrierLocalized = common.GetAddonRelatedTextGroup( "Barrier" ):GetText( GameLanguage )
	else
		wsBarrierLocalized = common.GetAddonRelatedTextGroup( "Barrier" ):GetText( "eng" )
	end
	
	Language = common.GetAddonRelatedTextGroup( GameLanguage ) and GameLanguage or Language
	SignChange()
	SetDPSMeterValues(MasterPetAmount(DamageWhom))
	
	onPetChanged()
end



function onPetChanged()
	if avatar.GetId() ~= nil then
		PetId = unit.GetActivePet( avatar.GetId() )
		if PetId ~= nil then
			if unit.IsPet( PetId ) then
				PetExist = true
			end
		else
			PetExist = false
			DamageWhom = "Master"
		end
		if (DamagePet.DamageAmount == 0 and DamagePet2.DamageAmount == 0) or PetExist then
			wtButtonMasterPet:Show(PetExist)
		end
	end
	for i = 1, 60 do
		wtTextViewBlock[i]:Show(false)
		wtNumViewBlock[i]:Show(false)
	end
	wtButtonMasterPet:SetVal( "button_label", GetTextLocalized( DamageWhom ) )
	SetDPSMeterValues(MasterPetAmount(DamageWhom))
end
function OnReactionMasterPet ()
	for i = 1, 60 do
		wtTextViewBlock[i]:Show(false)
		wtNumViewBlock[i]:Show(false)
	end
	if DamageWhom == "Master" then
		DamageWhom = "Pet"
	elseif DamageWhom == "Pet" then
		DamageWhom = "Amount"
	elseif DamageWhom == "Amount" then
		DamageWhom = "Master"
	end
	wtButtonMasterPet:SetVal( "button_label", GetTextLocalized( DamageWhom ) )
	SetDPSMeterValues(MasterPetAmount(DamageWhom))
end
function OnReactionSetChange ()
	for i = 1, 60 do
		wtTextViewBlock[i]:Show(false)
		wtNumViewBlock[i]:Show(false)
	end
	if Stack == 1 then
		Stack = 2
	else
		Stack = 1
	end
	wtButtonStack:SetVal( "button_label", GetTextLocalized( "Stack" .. Stack ) )
	local DamageSetMaster = DamageMaster
	local DamageSetPet = DamagePet
	local DamageSetMax = DamageMax
	DamageMaster = DamageMaster2
	DamagePet = DamagePet2
	DamageMax = DamageMax2
	DamageMaster2 = DamageSetMaster
	DamagePet2 = DamageSetPet
	DamageMax2 = DamageSetMax
	SetDPSMeterValues(MasterPetAmount(DamageWhom))
end

function OnReactionPointing( params )
	if params.active then
		local Place1 = wtMainPanel:GetPlacementPlain()
		local Place2 = wtPanelInfo:GetPlacementPlain()
		if Place1.posX < 190 then
			Place2.posX = Place1.posX + Place1.sizeX - 10
		else
			Place2.posX = Place1.posX - 190
		end
		Place2.posY = Place1.posY
		wtPanelInfo:SetPlacementPlain(Place2)
		wtPanelTitle:SetVal( "INFO", PanelTitles[params.sender] )
		wtPanelInfo:Show( panelCreate (params.sender) )
	else
		wtPanelInfo:Show(false)
	end
end

function panelCreate (title)
	for i = 1, 5 do
		wtTextString[i]:Show(false)
	end
	local NumStr = 0
	local mapPlace = nil
	local DamageData = MasterPetAmount(DamageWhom)
	local proc = nil
	local showStr = false
	if title == "Slot_2" then
		proc = 100 * DamageData.DamagePhysic / (DamageData.DamageAmount + DamageData.DamageMimo)
		NumStr = SetStringMaxInfo (DamageData, DamageData.DmgMaxPhysic)
	end
	if title == "Slot_4" then
		proc = 100 * DamageData.DamageBarrierOut / (DamageData.DamageAmount + DamageData.DamageMimo)
		NumStr = NumStr + 1
	end
	if title == "Slot_3" then
		proc = 100 * DamageData.DamageMagic / (DamageData.DamageAmount + DamageData.DamageMimo)
		NumStr = NumStr + 1
	end
	if title == "Slot_6" then
		proc = 100 * DamageData.DamageElement / (DamageData.DamageAmount + DamageData.DamageMimo)
		NumStr = NumStr + 1
	end
	if title == "Slot_7" then
		proc = 100 * DamageData.DamageDivine / (DamageData.DamageAmount + DamageData.DamageMimo)
		NumStr = NumStr + 1
	end
	if title == "Slot_8" then
		proc = 100 * DamageData.DamageNature / (DamageData.DamageAmount + DamageData.DamageMimo)
		NumStr = NumStr + 1
	end
	if title == "Slot_9" then
		proc = 100 * DamageData.DamageCold / (DamageData.DamageAmount + DamageData.DamageMimo)
		NumStr = SetStringMaxInfo (DamageData, DamageData.DmgMaxCold)
	end
	if title == "Slot_10" then
		proc = 100 * DamageData.DamageLightning / (DamageData.DamageAmount + DamageData.DamageMimo)
		NumStr = SetStringMaxInfo (DamageData, DamageData.DmgMaxLightning)
	end
	if title == "Slot_11" then
		proc = 100 * DamageData.DamageFire / (DamageData.DamageAmount + DamageData.DamageMimo)
		NumStr = SetStringMaxInfo (DamageData, DamageData.DmgMaxFire)
	end
	if title == "Slot_12" then
		proc = 100 * DamageData.DamageHoly / (DamageData.DamageAmount + DamageData.DamageMimo)
		NumStr = SetStringMaxInfo (DamageData, DamageData.DmgMaxHoly)
	end
	if title == "Slot_13" then
		proc = 100 * DamageData.DamageShadow / (DamageData.DamageAmount + DamageData.DamageMimo)
		NumStr = SetStringMaxInfo (DamageData, DamageData.DmgMaxShadow)
	end
	if title == "Slot_14" then
		proc = 100 * DamageData.DamageAstral / (DamageData.DamageAmount + DamageData.DamageMimo)
		NumStr = SetStringMaxInfo (DamageData, DamageData.DmgMaxAstral)
	end
	if title == "Slot_15" then
		proc = 100 * DamageData.DamageAcid / (DamageData.DamageAmount + DamageData.DamageMimo)
		NumStr = SetStringMaxInfo (DamageData, DamageData.DmgMaxAcid)
	end
	if title == "Slot_16" then
		proc = 100 * DamageData.DamageDisease / (DamageData.DamageAmount + DamageData.DamageMimo)
		NumStr = SetStringMaxInfo (DamageData, DamageData.DmgMaxDisease)
	end
	if title == "Slot_17" then
		proc = 100 * DamageData.DamagePoison / (DamageData.DamageAmount + DamageData.DamageMimo)
		NumStr = SetStringMaxInfo (DamageData, DamageData.DmgMaxPoison)
	end

	if title == "Slot_21" then
		proc = 100 * DamageData.DamageBlocked / (DamageData.DamageAmount + DamageData.DamageMimo)
		NumStr = NumStr + 1
	end
	if title == "Slot_22" then
		proc = 100 * DamageData.DamageParry / (DamageData.DamageAmount + DamageData.DamageMimo)
		NumStr = NumStr + 1
	end
	if title == "Slot_23" then
		proc = 100 * DamageData.DamageAbsorb / (DamageData.DamageAmount + DamageData.DamageMimo)
		NumStr = NumStr + 1
	end
	if title == "Slot_24" then
		proc = 100 * DamageData.DamageBarrierIn / (DamageData.DamageAmount + DamageData.DamageMimo)
		NumStr = NumStr + 1
	end
	if title == "Slot_25" then
		proc = 100 * math.ceil (DamageData.DamageAmount / DamageData.DamageCount * DamageData.Dodge) / (DamageData.DamageAmount + DamageData.DamageMimo)
		NumStr = NumStr + 1
	end
	if title == "Slot_26" then
		proc = 100 * DamageData.DamageResist / (DamageData.DamageAmount + DamageData.DamageMimo)
		NumStr = NumStr + 1
	end
	if title == "Slot_27" then
		proc = 100 * DamageData.ResistElement / (DamageData.DamageAmount + DamageData.DamageMimo)
		NumStr = NumStr + 1
	end
	if title == "Slot_28" then
		proc = 100 * DamageData.ResistDivine / (DamageData.DamageAmount + DamageData.DamageMimo)
		NumStr = NumStr + 1
	end
	if title == "Slot_29" then
		proc = 100 * DamageData.ResistNature / (DamageData.DamageAmount + DamageData.DamageMimo)
		NumStr = NumStr + 1
	end
	if proc then
		wtTextString[1]:SetVal( "NAME", GetTextLocalized( "String1" ) )
		wtTextString[1]:SetVal( "VALUE", common.FormatFloat( proc , "%.1f" ) )
		mapPlace = wtTextString[1]:GetPlacementPlain()
		mapPlace.posX = 20
		mapPlace.posY = 25
		wtTextString[1]:SetPlacementPlain( mapPlace )
		wtTextString[1]:Show(true)
		showStr = true
	end
	mapPlace = wtPanelInfo:GetPlacementPlain()
	mapPlace.sizeY = 35 + NumStr * 20
	wtPanelInfo:SetPlacementPlain( mapPlace )	
	return showStr
end
function SetStringPanelInfo (number, name, value)
	local mapPlace = nil
	wtTextString[number]:SetVal( "NAME", GetTextLocalized( name ) )
	if type(value) == "userdata" then
		wtTextString[number]:SetVal( "VALUE", value )
	else
		wtTextString[number]:SetVal( "VALUE", common.FormatFloat( value , "%f3K5" ) )
	end
	mapPlace = wtTextString[number]:GetPlacementPlain()
	mapPlace.posX = 20
	mapPlace.posY = 25 + (number - 1) * 20
	wtTextString[number]:SetPlacementPlain( mapPlace )
	wtTextString[number]:Show(true)
end
function SetStringMaxInfo (DamageData, damageType)
	SetStringPanelInfo (2, "MAX50", damageType.amount)			-- макс. физ. урон
	SetStringPanelInfo (3, "MAX51", damageType.sourceName)			-- имя, нанесшего макс. урон
	SetStringPanelInfo (4, "MAX54", damageType.ability )			-- чем нанесен
	local NumStr = 4
	if damageType.isCritical then						-- тип удара (крит/некрит)
		SetStringPanelInfo (5, "MAX53", GetTextLocalized( "Crit" ))
		NumStr = 5
	elseif damageType.isGlancing then
		NumStr = 5
		SetStringPanelInfo (5, "MAX53", GetTextLocalized( "Clanc" ))
	end
	return NumStr
end

function OnReactionLanguage()
	if Language == "rus" then
		Language = "eng"
	elseif Language == "eng" then
		Language = "ger"
	elseif Language == "ger" then
		Language = "fra"
	elseif Language == "fra" then
		Language = "rus"
	end
	SignChange()
	SetDPSMeterValues(MasterPetAmount(DamageWhom))
end

function SignChange()
	
	wtButtonLang:SetVal( "button_label", GetTextLocalized( "Lang_Ico" ) )
	wtTextHeader:SetVal ( "TITLE", GetTextLocalized( "TitleText" ) )
	wtButtonClean:SetVal( "button_label", GetTextLocalized( "Clean" ) )
	if CleanAuto then
		wtButtonCleanAuto:SetVal( "button_label", GetTextLocalized( "ON" ) )
	else
		wtButtonCleanAuto:SetVal( "button_label", GetTextLocalized( "OFF" ) )
	end
	wtButtonStack:SetVal( "button_label", GetTextLocalized( "Stack" .. Stack ) )
	wtButtonMasterPet:SetVal( "button_label", GetTextLocalized( DamageWhom ) )
	
	TypeDamage["ENUM_SubElement_PHYSICAL"] = GetTextLocalized( "Damage2" )
	TypeDamage["ENUM_SubElement_FIRE"] = GetTextLocalized( "Damage11" )
	TypeDamage["ENUM_SubElement_COLD"] = GetTextLocalized( "Damage9" )
	TypeDamage["ENUM_SubElement_LIGHTNING"] = GetTextLocalized( "Damage10" )
	TypeDamage["ENUM_SubElement_HOLY"] = GetTextLocalized( "Damage12" )
	TypeDamage["ENUM_SubElement_SHADOW"] = GetTextLocalized( "Damage13" )
	TypeDamage["ENUM_SubElement_ASTRAL"] = GetTextLocalized( "Damage14" )
	TypeDamage["ENUM_SubElement_POISON"] = GetTextLocalized( "Damage17" )
	TypeDamage["ENUM_SubElement_DISEASE"] = GetTextLocalized( "Damage16" )
	TypeDamage["ENUM_SubElement_ACID"] = GetTextLocalized( "Damage15" )
	
	OnEventAMAddonInfoRequest( { target = common.GetAddonName() } ) -- Refreshing description.
end

-- "SCRIPT_ADDON_INFO_REQUEST" -- "Addon Manager", Sending addon info.
function OnEventAMAddonInfoRequest( params )
	if params.target == common.GetAddonName() then
		userMods.SendEvent( "SCRIPT_ADDON_INFO_RESPONSE", {
			sender = params.target,
			desc = userMods.FromWString( GetTextLocalized( "Description" ) ),
			showDNDButton = true,
			showHideButton = true,
			showSettingsButton = true,
		} )
	end
end

-- "SCRIPT_ADDON_MEM_REQUEST" -- "Addon Manager", Sending memory usage info.
function OnEventAMAddonMemoryRequest( params )
	if params.target == common.GetAddonName() then
		userMods.SendEvent( "SCRIPT_ADDON_MEM_RESPONSE", { sender = params.target, memUsage = gcinfo() } )
	end
end

-- "SCRIPT_SHOW_SETTINGS" -- "Addon Manager", Showing the Main window by request.
function OnEventAMShowSettings( params )
	if params.target == common.GetAddonName() then
		if not wtMainPanel:IsVisible() then
			OnReactionExecute()
		end
	end
end

-- "SCRIPT_TOGGLE_VISIBILITY" -- "Addon Manager", Showing/hiding the show/hide button.
function OnEventAMToggleVisibility( params )
	if params.target == common.GetAddonName() then
		wtShowHideBtn:Show( params.state and true or false )
	end
end

-- "SCRIPT_TOGGLE_DND" -- "Addon Manager", Locking/unlocking the show/hide button.
function OnEventAMToggleDnD( params )
	if params.target == common.GetAddonName() then
		DnD:Enable( wtButtonExecute, params.state )
	end
end

-- "SCRIPT_TOGGLE_UI" -- Alt+Z / ESC hiding.
function OnEventToggleUI( params )
	mainForm:Show( params.visible )
end

-- AO game Localization detection by SLA. Version 2011-02-10.
function GetGameLocalization()
	local B = cartographer.GetMapBlocks()
	local T = { rus="\203\232\227\224", eng="Holy Land", ger="Heiliges Land",
	fra="Terre Sacr\233e", br="Terra Sagrada", jpn="\131\74\131\106\131\65" }
	for b in B do for l,t in T do
	if userMods.FromWString( cartographer.GetMapBlockInfo(B[b]).name ) == t
	then return l end end end return "eng"
end

function GetTextLocalized( strTextName )
	return common.GetAddonRelatedTextGroup( Language ):GetText( strTextName )
end


--------------------------------------------------------------------------------
-- INITIALIZATION
--------------------------------------------------------------------------------
function Init()

	common.RegisterReactionHandler( OnReactionExecute, "execute" )
	common.RegisterReactionHandler( OnReactionClean, "clean" )
	common.RegisterReactionHandler( OnReactionCleanAuto, "cleanauto" )
	common.RegisterReactionHandler( OnReactionMasterPet, "masterpet" )
	common.RegisterReactionHandler( OnReactionSetChange, "setchange" )
	common.RegisterReactionHandler( OnReactionLanguage, "langSelect" )
	common.RegisterEventHandler( onAvatarCreated, "EVENT_AVATAR_CREATED" )
	common.RegisterEventHandler( OnEventSecondTimer, "EVENT_SECOND_TIMER" )
	common.RegisterEventHandler( onDamageReceived, "EVENT_UNIT_DAMAGE_RECEIVED" )
	common.RegisterEventHandler( onPetChanged, "EVENT_ACTIVE_PET_CHANGED" )
--	common.RegisterEventHandler( onDamageBarrier, "EVENT_ENDURE_BARRIER_DAMAGE" )
--	common.RegisterEventHandler( onDamageBarrier, "EVENT_RELIEF_BARRIER_DAMAGE" )
--	common.RegisterEventHandler( onDamageBarrier, "EVENT_PURGE_BARRIER_DAMAGE" )
--	common.RegisterEventHandler( onDamageBarrierABSORBED, "EVENT_DAMAGE_ABSORBED_BY_BARRIER" )
	common.RegisterEventHandler( onAvatarCombatStatusChanged, "EVENT_AVATAR_COMBAT_STATUS_CHANGED" )
	common.RegisterReactionHandler( OnReactionPointing, "detail" )
	
	common.RegisterEventHandler( OnEventAMAddonInfoRequest, "SCRIPT_ADDON_INFO_REQUEST" )
	common.RegisterEventHandler( OnEventAMAddonMemoryRequest, "SCRIPT_ADDON_MEM_REQUEST" )
	common.RegisterEventHandler( OnEventAMShowSettings, "SCRIPT_SHOW_SETTINGS" )
	common.RegisterEventHandler( OnEventAMToggleVisibility, "SCRIPT_TOGGLE_VISIBILITY" )
	common.RegisterEventHandler( OnEventAMToggleDnD, "SCRIPT_TOGGLE_DND" )
	common.RegisterEventHandler( OnEventToggleUI, "SCRIPT_TOGGLE_UI" )
	
	wtMainPanel = mainForm:GetChildChecked( "MainPanel", false )
	wtButtonExecute = mainForm:GetChildChecked( "Execute", false )
	wtPanelInfo = mainForm:GetChildChecked( "PanelInfo", false )
	
	wtTextHeader = wtMainPanel:GetChildChecked( "TextHeader", false )
	wtTextHeader:SetAlignY ( 1 )
	
	wtFooterText		=	wtMainPanel:GetChildChecked( "FooterText", false )
	wtButtonClean		=	wtMainPanel:GetChildChecked( "ButtonClean", false )
	wtButtonCleanAuto	=	wtMainPanel:GetChildChecked( "ButtonCleanAuto", false )
	wtButtonMasterPet	=	wtMainPanel:GetChildChecked( "ButtonMasterPet", false )
	wtButtonMasterPet:Show(PetExist)
	wtButtonStack		=	wtMainPanel:GetChildChecked( "ButtonStack", false )
	wtButtonLang 		= 	wtMainPanel:GetChildChecked( "ButtonLang", false )

	wtTextView = wtMainPanel:GetChildChecked( "TextView", false )
	wtTextView:Show(false)
	wtNumView = wtMainPanel:GetChildChecked( "NumView", false )
	wtNumView:Show(false)

	for i = 0, 60 do
		wtTextViewBlock[i] = mainForm:CreateWidgetByDesc( wtTextView:GetWidgetDesc() ) 
		wtMainPanel:AddChild( wtTextViewBlock[i] )
		wtTextViewBlock[i]:Show(false)

		wtNumViewBlock[i] = mainForm:CreateWidgetByDesc( wtNumView:GetWidgetDesc() ) 
		wtNumViewBlock[i]:SetName( "Slot_" .. i )
		wtMainPanel:AddChild( wtNumViewBlock[i] )
		wtNumViewBlock[i]:Show(false)
	---------------------------------
	end

	wtPanelTitle = wtPanelInfo:GetChildChecked( "TextTELL", false )
	wtTextStrg = wtPanelInfo:GetChildChecked( "TextString", false )
	wtTextStrg:Show(false)
	for i = 1, 5 do
		wtTextString[i] = mainForm:CreateWidgetByDesc( wtTextStrg:GetWidgetDesc() ) 
		wtPanelInfo:AddChild( wtTextString[i] )
		wtTextString[i]:Show(false)
		wtTextString[i]:SetName( "String_" .. i )
	end

	DnD:Init( 670, wtTextHeader, wtMainPanel, true, true, { 0, -8, -8, -8 } )
	DnD:Init( 671, wtButtonExecute, wtButtonExecute, true, true )

	-- Backward compatibility with pre-1.1.04 versions:
	if not social.GetFriendInfo then
		mainForm:SetPriority( 1000 )
	end

	if avatar.IsExist() then
		onAvatarCreated()
	end

	wtFooterText:SetVal( "RELEASE", common.FormatInt( RELEASE, "%d" ) )

end
--------------------------------------------------------------------------------
Init()
--------------------------------------------------------------------------------
