local s,id,o=GetID()
function s.initial_effect(c)
	--fusion material
	c:EnableReviveLimit()
	aux.AddFusionProcFunRep(c,aux.AtkEqualsDef,3,true)
	aux.AddContactFusionProcedure(c,s.ffilter,LOCATION_ONFIELD,LOCATION_ONFIELD,Duel.Release,REASON_COST+REASON_MATERIAL)
    --cannot fusion material
	local e1=Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_SINGLE)
	e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_UNCOPYABLE)
	e1:SetCode(EFFECT_CANNOT_BE_FUSION_MATERIAL)
	e1:SetValue(1)
	c:RegisterEffect(e1)
    local e2=Effect.CreateEffect(c)
	e2:SetType(EFFECT_TYPE_FIELD)
	e2:SetCode(EFFECT_DISABLE)
	e2:SetRange(LOCATION_MZONE)
	e2:SetTargetRange(LOCATION_MZONE,LOCATION_MZONE)
    e2:SetCondition(s.condition)
	e2:SetTarget(s.distg)
	c:RegisterEffect(e2)
    --atk
	local e3=Effect.CreateEffect(c)
	e3:SetType(EFFECT_TYPE_FIELD)
	e3:SetCode(EFFECT_UPDATE_ATTACK)
	e3:SetRange(LOCATION_MZONE)
	e3:SetTargetRange(LOCATION_MZONE,0)
	e3:SetTarget(aux.TargetBoolFunction(aux.AtkEqualsDef))
	e3:SetValue(500)
	c:RegisterEffect(e3)
    --addc
	local e4=Effect.CreateEffect(c)
	e4:SetDescription(aux.Stringid(id,1))
	e4:SetType(EFFECT_TYPE_QUICK_O)
	e4:SetCode(EVENT_FREE_CHAIN)
	e4:SetRange(LOCATION_MZONE)
	e4:SetCost(s.addccost)
    e4:SetTarget(s.addctg)
	e4:SetOperation(s.addc)
	c:RegisterEffect(e4)
	--def
	local e5=e3:Clone()
	e5:SetCode(EFFECT_UPDATE_DEFENSE)
	c:RegisterEffect(e5)
end
function s.ffilter(c)
	return c:IsReleasable() and c:IsFaceup()
end
function s.cfilter(c)
	return c:IsFaceup() and c:IsCode(24793135)
end
function s.condition(e)
	local tp=e:GetHandlerPlayer()
	return Duel.IsExistingMatchingCard(s.cfilter,tp,LOCATION_ONFIELD,0,1,nil)
end
function s.distg(e,c)
	return c:IsType(TYPE_LINK)
end
function s.ctfilter(c)
	return c:IsFaceup() and c:IsCode(24793135) and c:IsCanAddCounter(0x5d,1)
end
function s.rfilter(c)
	return c:IsFaceup() and aux.AtkEqualsDef(c)
end
function s.atkfilter(c)
	return c:IsFaceup() and c:GetAttack()>0
end
function s.addccost(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return Duel.CheckReleaseGroup(tp,s.rfilter,1,REASON_SPSUMMON,nil) end
	local g=Duel.SelectReleaseGroup(tp,s.rfilter,1,1,REASON_SPSUMMON,nil)
	Duel.Release(g,REASON_SPSUMMON)
end
function s.addctg(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return Duel.IsExistingMatchingCard(s.ctfilter,tp,LOCATION_FZONE,0,1,nil) end
    Duel.SetOperationInfo(0,CATEGORY_COUNTER,nil,1,0,0x5d)
end
function s.addc(e,tp,eg,ep,ev,re,r,rp)
	local g=Duel.GetMatchingGroup(s.ctfilter,tp,LOCATION_FZONE,0,nil)
	local tc=g:GetFirst()
    local c=e:GetHandler()
    tc:AddCounter(0x5d,1)
    if tc~=0 and Duel.IsExistingMatchingCard(s.atkfilter,tp,0,LOCATION_MZONE,1,nil) and Duel.SelectYesNo(tp,aux.Stringid(id,0)) then
        local g=Duel.SelectMatchingCard(tp,s.atkfilter,tp,0,LOCATION_MZONE,1,1,nil)
		Duel.HintSelection(g)
		local tc=g:GetFirst()
		if tc then
			local e1=Effect.CreateEffect(c)
			e1:SetType(EFFECT_TYPE_SINGLE)
			e1:SetCode(EFFECT_SET_ATTACK_FINAL)
			e1:SetValue(0)
			e1:SetReset(RESET_EVENT+RESETS_STANDARD)
			tc:RegisterEffect(e1)
		end
	end
end