--神心樱舞·厄科
function c66914013.initial_effect(c)
	--xyz summon
	aux.AddXyzProcedure(c,nil,4,4,c66914013.ovfilter,aux.Stringid(66914013,0),4,c66914013.xyzop)
	c:EnableReviveLimit()  
	--activate from hand
	local e1=Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_FIELD)
	e1:SetCode(EFFECT_QP_ACT_IN_NTPHAND)
	e1:SetRange(LOCATION_MZONE)
	e1:SetTarget(aux.TargetBoolFunction(Card.IsSetCard,0x373))
	e1:SetTargetRange(LOCATION_HAND,0)
	c:RegisterEffect(e1)
	local e2=e1:Clone()
	e2:SetCode(EFFECT_TRAP_ACT_IN_HAND)
	c:RegisterEffect(e2)	
	--a lot of effect
	local e3=Effect.CreateEffect(c)
	e3:SetDescription(aux.Stringid(66914013,2))
	e3:SetType(EFFECT_TYPE_QUICK_O)
	e3:SetRange(LOCATION_MZONE)
	e3:SetCode(EVENT_FREE_CHAIN)
	e3:SetCountLimit(1)
	e3:SetTarget(c66914013.eqtg)
	e3:SetOperation(c66914013.eqop)
	c:RegisterEffect(e3)
	--copy effect
	local e21=Effect.CreateEffect(c)
	e21:SetDescription(aux.Stringid(66914013,1))
	e21:SetType(EFFECT_TYPE_QUICK_O)
	e21:SetRange(LOCATION_MZONE)
	e21:SetCode(EVENT_FREE_CHAIN)
	e21:SetCountLimit(1)
	e21:SetCondition(c66914013.discon)
	e21:SetCost(c66914013.cpcost)
	e21:SetTarget(c66914013.cptg)
	e21:SetOperation(c66914013.cpop)
	c:RegisterEffect(e21)
	--
	Duel.AddCustomActivityCounter(66914013,ACTIVITY_CHAIN,c66914013.chainfilter)
end
function c66914013.chainfilter(re,tp,cid)
	return not (Duel.GetCurrentChain()>3)
end
function c66914013.ovfilter(c)
	return c:IsFaceup() and c:IsCode(66914000)
end
function c66914013.xyzop(e,tp,chk)
	if chk==0 then return Duel.GetFlagEffect(tp,66914010)==0
		and (Duel.GetCustomActivityCount(66914013,tp,ACTIVITY_CHAIN)>0
			or Duel.GetCustomActivityCount(66914013,1-tp,ACTIVITY_CHAIN)>0) end
	Duel.RegisterFlagEffect(tp,66914010,RESET_PHASE+PHASE_END,EFFECT_FLAG_OATH,1)
end
function c66914013.cpfilter(c)
	return (c:IsType(TYPE_QUICKPLAY)) and c:IsSetCard(0x373) and c:CheckActivateEffect(true,true,false)~=nil
end
function c66914013.cpcost(e,tp,eg,ep,ev,re,r,rp,chk)
	e:SetLabel(1)
	if chk==0 then return true end
end
function c66914013.discon(e,tp,eg,ep,ev,re,r,rp)
	return not e:GetHandler():IsStatus(STATUS_BATTLE_DESTROYED)
		and e:GetHandler():GetOverlayGroup():IsExists(Card.IsCode,1,nil,66914000)
end
function c66914013.cptg(e,tp,eg,ep,ev,re,r,rp,chk)
	local c=e:GetHandler()
	local og=c:GetOverlayGroup()
	if chk==0 then
		if e:GetLabel()==0 then return false end
		e:SetLabel(0)
		return og:IsExists(c66914013.cpfilter,1,nil) and c:CheckRemoveOverlayCard(tp,1,REASON_COST) 
	end
	e:SetLabel(0)
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_TOGRAVE)
	local g=og:FilterSelect(tp,c66914013.cpfilter,1,1,nil)
	local te,ceg,cep,cev,cre,cr,crp=g:GetFirst():CheckActivateEffect(true,true,true)
	Duel.SendtoGrave(g,REASON_COST)
	e:SetProperty(te:GetProperty())
	local tg=te:GetTarget()
	if tg then tg(e,tp,ceg,cep,cev,cre,cr,crp,1) end
	te:SetLabelObject(e:GetLabelObject())
	e:SetLabelObject(te)
	Duel.ClearOperationInfo(0)
end
function c66914013.cpop(e,tp,eg,ep,ev,re,r,rp)
	local te=e:GetLabelObject()
	if te then
		e:SetLabelObject(te:GetLabelObject())
		local op=te:GetOperation()
		if op then op(e,tp,eg,ep,ev,re,r,rp) end
	end
end
function c66914013.ovfilter2(c,count)
	return c:IsCanOverlay() and c:IsSetCard(0x373) and (count>=4 or not c:IsLocation(LOCATION_DECK))
end
function c66914013.eqtg(e,tp,eg,ep,ev,re,r,rp,chk)
	local count=0
	local chain=Duel.GetCurrentChain()
	for i=1,chain+1 do
		count=count+1
	end
	local g=e:GetHandler():GetOverlayGroup()
	local check1=math.floor(count/2)
	local check2=math.floor(count/4)
	if chk==0 then return count>=2 and (check1<1 or Duel.IsExistingMatchingCard(c66914013.ovfilter2,tp,LOCATION_DECK+LOCATION_GRAVE+LOCATION_ONFIELD,0,1,nil,count)) and (check2<1 or g:IsExists(Card.IsAbleToHand,1,nil)) end
	if check2 then
		e:SetCategory(CATEGORY_TOHAND)
		Duel.SetOperationInfo(0,CATEGORY_TOHAND,nil,1,tp,LOCATION_OVERLAY)
	end
	e:SetLabel(count-1)
end
function c66914013.filter3(c)
	return c:IsCode(66914011) and c:IsFaceup()
end
function c66914013.fselect(g,count)
	return g:FilterCount(Card.IsLocation,nil,LOCATION_DECK)<=1
end
function c66914013.eqop(e,tp,eg,ep,ev,re,r,rp)
	local count=e:GetLabel()
	local check1=math.floor(count/2)
	local check3=math.floor(count/3)
	local check2=math.floor(count/4)
	local c=e:GetHandler()
	if not (c:IsRelateToChain() and c:IsType(TYPE_XYZ)) then return end
	local g=Duel.GetMatchingGroup(c66914013.ovfilter2,tp,LOCATION_DECK+LOCATION_GRAVE+LOCATION_ONFIELD,0,e:GetHandler(),count)
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_XMATERIAL)
	local mg=g:SelectSubGroup(tp,c66914013.fselect,false,1,check1,count)
	if #mg>0 then
		local tc=mg:GetFirst()
		while tc do
			if tc:IsOnField() and not (tc:IsType(TYPE_CONTINUOUS) or tc:IsType(TYPE_MONSTER)) then
				local fe=tc:GetActivateEffect()
				if tc:IsRelateToEffect(fe) then
					tc:CancelToGrave()
				end
			end
			tc=mg:GetNext()
		end
		Duel.Overlay(c,mg)
	end
	while check3>0 and Duel.IsExistingMatchingCard(c66914013.filter3,tp,LOCATION_SZONE,0,1,nil) and Duel.SelectYesNo(tp,aux.Stringid(66914013,3)) do
		Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_COUNTER)
		local tc=Duel.SelectMatchingCard(tp,c66914013.filter3,tp,LOCATION_SZONE,0,1,1,nil):GetFirst()
		local opt=Duel.SelectOption(tp,aux.Stringid(66914013,4),aux.Stringid(66914013,5),aux.Stringid(66914013,6))+1
		if opt==1 then
			tc:AddCounter(0x1371,1)
		elseif opt==2 then
			tc:AddCounter(0x1372,1)
		else
			tc:AddCounter(0x1373,1)
		end
		check3=check3-1
	end
	if c:IsRelateToEffect(e) then
		local g=c:GetOverlayGroup()
		Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_ATOHAND)
		local tg=g:FilterSelect(tp,Card.IsAbleToHand,1,check2,nil)
		if tg:GetCount()>0 then
			Duel.SendtoHand(tg,nil,REASON_EFFECT)
		end
	end
end