Generate a YGOPro Lua script for the following Yu-Gi-Oh card effect:
2 Level 5 Spellcaster monsters Cannot be destroyed by battle or card effects while it has material. Your opponent takes any effect damage you would have taken, a number of times each turn up to the number of materials attached to this card. If this card has "Performage Trapeze Magician" as material, it gains this effect. ● You can detach 1 material from this card; this card can make up to 3 attacks during each Battle Phase this turn.
--Ｅｍトラピーズ・ハイ・マジシャン
--Performage Trapeze High Magician
--scripted by Naim
local s,id=GetID()
function s.initial_effect(c)
	c:EnableReviveLimit()
	--Xyz Summon procedure: 2 Level 5 Spellcaster monsters
	Xyz.AddProcedure(c,aux.FilterBoolFunctionEx(Card.IsRace,RACE_SPELLCASTER),5,2)
	--Cannot be destroyed by battle or card effects while it has material
	local e1=Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_SINGLE)
	e1:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
	e1:SetCode(EFFECT_INDESTRUCTABLE_BATTLE)
	e1:SetRange(LOCATION_MZONE)
	e1:SetCondition(function(e) return e:GetHandler():GetOverlayCount()>0 end)
	e1:SetValue(1)
	c:RegisterEffect(e1)
	local e2=e1:Clone()
	e2:SetCode(EFFECT_INDESTRUCTABLE_EFFECT)
	c:RegisterEffect(e2)
	--Your opponent takes any effect damage you would have taken instead
	local e3=Effect.CreateEffect(c)
	e3:SetType(EFFECT_TYPE_FIELD)
	e3:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
	e3:SetCode(EFFECT_REFLECT_DAMAGE)
	e3:SetRange(LOCATION_MZONE)
	e3:SetTargetRange(1,0)
	e3:SetCondition(s.reflectcond)
	e3:SetValue(s.reflectvalue)
	c:RegisterEffect(e3)
	--Can make up to 3 attacks this turn
	local e4=Effect.CreateEffect(c)
	e4:SetDescription(aux.Stringid(id,0))
	e4:SetType(EFFECT_TYPE_IGNITION)
	e4:SetRange(LOCATION_MZONE)
	e4:SetCondition(s.atkcon)
	e4:SetCost(Cost.DetachFromSelf(1,1,nil))
	e4:SetOperation(s.atkop)
	c:RegisterEffect(e4)
end
s.listed_names={17016362} --"Performage Trapeze Magician"
function s.reflectcond(e)
	local c=e:GetHandler()
	return c:GetOverlayCount()>c:GetFlagEffect(id)
end
function s.reflectvalue(e,re,val,r,rp,rc)
	if (r&REASON_EFFECT)>0 then
		Duel.Hint(HINT_CARD,rp,id)
		e:GetHandler():RegisterFlagEffect(id,RESETS_STANDARD_PHASE_END,0,1)
		return true
	end
	return false
end
function s.atkcon(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	return Duel.IsAbleToEnterBP() and c:CanAttack() and c:GetEffectCount(EFFECT_EXTRA_ATTACK)<2
		and c:GetOverlayGroup():IsExists(Card.IsCode,1,nil,17016362)
end
function s.atkop(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	if c:IsRelateToEffect(e) then
		--Can make up to 3 attacks during each Battle Phase this turn
		local e1=Effect.CreateEffect(c)
		e1:SetType(EFFECT_TYPE_SINGLE)
		e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
		e1:SetCode(EFFECT_EXTRA_ATTACK)
		e1:SetValue(2)
		e1:SetReset(RESETS_STANDARD_DISABLE_PHASE_END)
		c:RegisterEffect(e1)
	end
end