--神心一刀流·樱散回返
function c66914006.initial_effect(c)
	--spsummon
	local e2=Effect.CreateEffect(c)
	e2:SetType(EFFECT_TYPE_ACTIVATE)
	e2:SetCode(EVENT_CHAINING)
	e2:SetCountLimit(1,66914006)
	e2:SetCondition(c66914006.spcon)
	e2:SetTarget(c66914006.cptg)
	e2:SetOperation(c66914006.cpop)
	c:RegisterEffect(e2)   
	--set
	local e3=Effect.CreateEffect(c)
	e3:SetCategory(CATEGORY_TODECK+CATEGORY_DRAW)
	e3:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_TRIGGER_O)
	e3:SetCode(EVENT_CHAINING)
	e3:SetProperty(EFFECT_FLAG_DELAY)
	e3:SetRange(LOCATION_GRAVE)
	e3:SetCountLimit(1,66914006)
	e3:SetCondition(c66914006.condition)
	e3:SetTarget(c66914006.settg)
	e3:SetOperation(c66914006.setop)
	c:RegisterEffect(e3) 
end
function c66914006.spcon(e,tp,eg,ep,ev,re,r,rp)
	return re:GetHandler():IsSetCard(0x373) and re:IsHasType(EFFECT_TYPE_ACTIVATE) and re:GetHandler():IsType(TYPE_QUICKPLAY)
end
function c66914006.cptg(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return re:GetHandler():CheckActivateEffect(true,true,false)~=nil end
	local te,ceg,cep,cev,cre,cr,crp=re:GetHandler():CheckActivateEffect(false,true,true)
	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 c66914006.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
	if (Duel.GetFlagEffect(tp,66914007)>0 or Duel.GetFlagEffect(tp,66914006)>0) and Duel.IsExistingMatchingCard(aux.TRUE,tp,0,LOCATION_ONFIELD,1,nil) and Duel.IsPlayerCanDraw(tp,1)  and not re:GetHandler():IsCode(66914007) and Duel.SelectYesNo(tp,aux.Stringid(66914007,0)) then
		local g=Duel.SelectMatchingCard(tp,aux.TRUE,tp,0,LOCATION_ONFIELD,1,1,nil):GetFirst()
		Duel.Destroy(g,REASON_EFFECT)
		Duel.Draw(tp,1,REASON_EFFECT)
		if Duel.GetFlagEffect(tp,66914006)~=0 then
			Duel.ResetFlagEffect(tp,66914006)
		else
			Duel.ResetFlagEffect(tp,66914007)
		end
	end
end
function c66914006.condition(e,tp,eg,ep,ev,re,r,rp)
	return Duel.GetTurnPlayer()~=tp and rp==tp and re:GetHandler():IsSetCard(0x373)
end
function c66914006.tgfilter(c)
	return c:IsSetCard(0x373) and c:IsAbleToDeck()
end
function c66914006.settg(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then
		local g=Duel.GetMatchingGroup(c66914006.tgfilter,tp,LOCATION_GRAVE,0,e:GetHandler())
		return g:GetClassCount(Card.GetCode)>1 and Duel.IsPlayerCanDraw(tp,1) and e:GetHandler():IsAbleToDeck()
	end
	Duel.SetOperationInfo(0,CATEGORY_TODECK,nil,2,tp,LOCATION_GRAVE)
	Duel.SetOperationInfo(0,CATEGORY_DRAW,nil,0,tp,1)
end
function c66914006.setop(e,tp,eg,ep,ev,re,r,rp)
	local g=Duel.GetMatchingGroup(c66914006.tgfilter,tp,LOCATION_GRAVE,0,e:GetHandler())
	if g:GetClassCount(Card.GetCode)<2 then return end
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_TODECK)
	local tg1=g:SelectSubGroup(tp,aux.dncheck,false,2,2)
	tg1:AddCard(e:GetHandler())
	if Duel.SendtoDeck(tg1,nil,2,REASON_EFFECT)~=0 and tg1:IsExists(Card.IsLocation,3,nil,LOCATION_DECK) then
		Duel.Draw(tp,1,REASON_EFFECT)
	end
end