

-- EVENT_UNIT_BUFFS_ELEMENT_CHANGED
function OnEventBuffChanged_OLD( params ) -- AO 1.0.00-2.0.05
  if params.units then
     local unitID = params.units.key      --- New\old API
     for i in params.units do
      	if i then 
		local unitID = i
        	for x in params.units[i] do
			local index = x
			CheckBuff_OLD({ u = unitID, inx = index})
		end
      	end 
     end
  else
     local unitID = params.unitId
     local index = params.index
     CheckBuff_OLD({ u = unitID, inx = index})
  end
end



function CheckBuff_OLD(params)
  
  local Buff = unit.GetBuff(params.u, params.inx )
  
  if Status[params.u] == 1 then
     local ab = userMods.FromWString( Buff.name )
     if Buff.producer.casterId == avatar.GetId() and ( ab == abIcyGrave[LOC] or ab ==  abPlague[LOC] or ab ==  abPotion[LOC] or ab ==  abBlock[LOC]) then
 	BuffID[params.u] = params.inx
	Time[params.u]=math.ceil(Buff.remainingMs/1000)	

	if Time[params.u] <= 1 then Time[params.u] = LastTime[params.u] end	

	if Time[params.u] > 16 then 
		Wid[params.u] = G[params.u]
		W[params.u]:Show(false)
		Rd[params.u]:Show(false)
		G[params.u]:Show(true)
		Y[params.u]:Show(false)
	else 
		if Time[params.u] > 6 then
			Wid[params.u] = Y[params.u]
			W[params.u]:Show(false)
			Rd[params.u]:Show(false)
			Y[params.u]:Show(true)
			G[params.u]:Show(false)
		else
			Wid[params.u] = Rd[params.u]
			W[params.u]:Show(false)
			G[params.u]:Show(false)
			Rd[params.u]:Show(true)
			Y[params.u]:Show(false)
		end
	end
	
	if ab == abIcyGrave[LOC] then Wid[params.u]:SetVal( "time", userMods.ToWString( (Ct[params.u]+1) .. stIce[LOC] .. Time[params.u] .. stSec[LOC])) end
	if ab == abPlague[LOC] then Wid[params.u]:SetVal( "time", userMods.ToWString( (Ct[params.u]+1) .. stPlague[LOC] .. Time[params.u] .. stSec[LOC])) end
 	if ab == abPotion[LOC] then Wid[params.u]:SetVal( "time", userMods.ToWString( (Ct[params.u]+1) .. stLethargy[LOC] .. Time[params.u] .. stSec[LOC])) end
 	
	if ab == abBlock[LOC] then		
 		local BuffsC = unit.GetBuffCount(params.u)
 		local findLoc = 0
		if BuffsC > 0 then
  			for i = 0, BuffsC-1 do
   				Buff = unit.GetBuff(params.u, i )
 				if Buff.producer.casterId == avatar.GetId() and userMods.FromWString( Buff.name ) == abSuppression[LOC] then
 					Wid[params.u]:SetVal( "time", userMods.ToWString( (Ct[params.u]+1) .. stHypnosis[LOC] .. Time[params.u] .. "|" .. math.ceil(Buff.remainingMs/1000)-1 .. stSec[LOC]))
					findLoc = 1
				end						
 			end
 		end
		if findLoc == 0 then
			Wid[params.u]:SetVal( "time", userMods.ToWString( (Ct[params.u]+1) .. stHypnosis[LOC] .. Time[params.u] .. stSec[LOC]))
		end				
 	end
     end
  end
end



-- EVENT_UNIT_BUFF_ADDED
function OnEventBuffAdded_OLD(par)
	local Buff = unit.GetBuff( par.unitId, par.index )		                                    
     	local ab = userMods.FromWString( Buff.name )
     	if Buff.producer.casterId == avatar.GetId() and ( ab == abIcyGrave[LOC] or ab ==  abPlague[LOC] or ab ==  abPotion[LOC] or ab ==  abBlock[LOC]) then
	    if Status[par.unitId] == 2 then
		Status[par.unitId] = 1
		Wid[par.unitId]:SetVal( "time", userMods.ToWString( stWait[LOC] ))
		BuffID[par.unitId] = par.index
		Time[par.unitId] = 30
	    else
	      if Status[par.unitId] == nil then
		
	        if ab ==  abPlague[LOC]  then MP[par.unitId] = 1 end
		if ab ==  abBlock[LOC] then GIPNOZ[par.unitId] = 1 end
		UnitID[par.unitId] = par.unitId			
			
			local desc = wtTextView:GetWidgetDesc()           	
			W[par.unitId] = mainForm:CreateWidgetByDesc( desc )
			W[par.unitId]:SetName( "w" .. par.unitId )
			mainForm:AddChild( W[par.unitId] )
			
			local desc = wtRText:GetWidgetDesc()           	
			Rd[par.unitId] = mainForm:CreateWidgetByDesc( desc )
			Rd[par.unitId]:SetName( "r" .. par.unitId )
			mainForm:AddChild( Rd[par.unitId] )
			
			local desc = wtGText:GetWidgetDesc()           	
			G[par.unitId] = mainForm:CreateWidgetByDesc( desc )
			G[par.unitId]:SetName( "g" .. par.unitId )
			mainForm:AddChild( G[par.unitId] )
			
			local desc = wtYText:GetWidgetDesc()           	
			Y[par.unitId] = mainForm:CreateWidgetByDesc( desc )
			Y[par.unitId]:SetName( "y" .. par.unitId )
			mainForm:AddChild( Y[par.unitId] )
			
			Wid[par.unitId] = W[par.unitId]			
			
			Wid[par.unitId]:SetVal( "time", userMods.ToWString( stWait[LOC] ))
			
			local placement = wtTextView:GetPlacementPlain()
				
           		placement.posY = placement.posY + (GlobalCt * 30 ) + 30
			Ct[par.unitId] = GlobalCt			
			GlobalCt = GlobalCt + 1
			
			
			Status[par.unitId] = 1
			BuffID[par.unitId] = par.index
			
			Select:SetPlacementPlain(placement)
			Select:Show(true)
			
			desc = Reactor:GetWidgetDesc()
			R[par.unitId] = mainForm:CreateWidgetByDesc( desc )
			R[par.unitId]:SetName( "" .. par.unitId )
			mainForm:AddChild( R[par.unitId] )
			R[par.unitId]:SetPlacementPlain(placement)
			R[par.unitId]:Show(true)
			
			placement.posY = placement.posY + 1
			
			W[par.unitId]:SetPlacementPlain(placement)
			Rd[par.unitId]:SetPlacementPlain(placement)
			G[par.unitId]:SetPlacementPlain(placement)
			Y[par.unitId]:SetPlacementPlain(placement)
			
			Wid[par.unitId]:Show(true)
	     end	
				
	     if mainForm:IsVisible() == false then
		mainForm:Show(true)	
		DNDReg()
	     end			   
	  end		
	end
end


