--神心一刀流·樱落刀舞
function c66914004.initial_effect(c)
	--Activate
	local e1=Effect.CreateEffect(c)
	e1:SetCategory(CATEGORY_TODECK)
	e1:SetType(EFFECT_TYPE_ACTIVATE)
	e1:SetCode(EVENT_FREE_CHAIN)
	e1:SetTarget(c66914004.thtg)
	e1:SetOperation(c66914004.thop)
	c:RegisterEffect(e1)	 
end
function c66914004.thtg(e,tp,eg,ep,ev,re,r,rp,chk,chkc)
	local count=0
	local chain=Duel.GetCurrentChain()
	for i=1,chain do
		local te,tgp=Duel.GetChainInfo(i,CHAININFO_TRIGGERING_EFFECT,CHAININFO_TRIGGERING_PLAYER)
		if tgp==tp and te:GetHandler():IsSetCard(0x373) and te:GetHandler()~=e:GetHandler() then
			count=count+1
		end
	end
	local reverse=Duel.GetFlagEffect(tp,66914009)
	if chk==0 then 
		if reverse==0 then
			return Duel.IsExistingMatchingCard(Card.IsAbleToDeck,tp,0,LOCATION_GRAVE,1,nil) and (count~=2 or Duel.IsExistingMatchingCard(Card.IsCanTurnSet,tp,0,LOCATION_MZONE,1,nil)) and (count~=4 or Duel.IsExistingMatchingCard(Card.IsAbleToRemove,tp,0,LOCATION_GRAVE,1,nil))
		else
			return Duel.IsExistingMatchingCard(Card.IsAbleToRemove,tp,0,LOCATION_GRAVE,1,nil) and (count~=2 or Duel.IsExistingMatchingCard(Card.IsCanTurnSet,tp,0,LOCATION_MZONE,1,nil)) and (count~=4 or Duel.IsExistingMatchingCard(Card.IsAbleToDeck,tp,0,LOCATION_GRAVE,1,nil))
		end
	end
	local cat=0
	if count==5 or reverse>0 then
		cat=cat|CATEGORY_REMOVE
		Duel.SetOperationInfo(0,CATEGORY_REMOVE,nil,1,1-tp,LOCATION_GRAVE)
	end
	if count==3 then
		cat=cat|CATEGORY_POSITION
	end
	if reverse==0 or (count==5 and reverse>0) then
		cat=cat|CATEGORY_TODECK
		Duel.SetOperationInfo(0,CATEGORY_TODECK,nil,0,1-tp,LOCATION_GRAVE)
	end
	e:SetCategory(cat)
	count=count+1
	e:SetLabel(count)
end
function c66914004.setfilter(c)
	return c:IsCanTurnSet() and (not c:IsLocation(LOCATION_PZONE)) and not c:IsStatus(STATUS_CHAINING)
end
function c66914004.thop(e,tp,eg,ep,ev,re,r,rp)
	local count=e:GetLabel()
	local reverse=Duel.GetFlagEffect(tp,66914009)
	if reverse==0 or (reverse>0 and count==5) then
		Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_TODECK)
		local g=Duel.SelectMatchingCard(tp,Card.IsAbleToDeck,tp,0,LOCATION_GRAVE,1,1,nil)
		if g:GetCount()>0 then
			Duel.HintSelection(g)
			Duel.SendtoDeck(g,nil,2,REASON_EFFECT)
		end
	end
	if count==3 then
		Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_FACEUP)
		local g=Duel.SelectMatchingCard(tp,Card.IsCanTurnSet,tp,0,LOCATION_MZONE,1,1,nil)
		Duel.ChangePosition(g,POS_FACEDOWN_DEFENSE)
	end
	if reverse>0 or count==5 then
		Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_REMOVE)
		local g=Duel.SelectMatchingCard(tp,Card.IsAbleToRemove,tp,0,LOCATION_GRAVE,1,1,nil)
		if g:GetCount()>0 then
			Duel.HintSelection(g)
			Duel.Remove(g,POS_FACEUP,REASON_EFFECT)
		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 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