--神心一刀流·樱风刃雨
function c66914002.initial_effect(c)
	--Activate
	local e1=Effect.CreateEffect(c)
	e1:SetCategory(CATEGORY_DESTROY)
	e1:SetType(EFFECT_TYPE_ACTIVATE)
	e1:SetCode(EVENT_FREE_CHAIN)
	e1:SetTarget(c66914002.thtg)
	e1:SetOperation(c66914002.thop)
	c:RegisterEffect(e1)	  
end
function c66914002.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(aux.TRUE,tp,0,LOCATION_MZONE,1,nil) and (count~=3 or Duel.IsExistingMatchingCard(aux.TRUE,tp,0,LOCATION_SZONE,1,nil))
		else
			return Duel.IsExistingMatchingCard(aux.TRUE,tp,0,LOCATION_SZONE,1,nil) and (count~=3 or Duel.IsExistingMatchingCard(aux.TRUE,tp,0,LOCATION_MZONE,1,nil))
		end
	end
	local g=Duel.GetMatchingGroup(aux.TRUE,tp,0,LOCATION_ONFIELD,nil)
	Duel.SetOperationInfo(0,CATEGORY_DESTROY,g,1,0,0)
	count=count+1
	e:SetLabel(count)
end
function c66914002.thop(e,tp,eg,ep,ev,re,r,rp)
	local count=e:GetLabel()
	local ct1=0
	local reverse=Duel.GetFlagEffect(tp,66914009)
	if count==3 then
		ct1=1
	end
	if reverse==0 or (reverse>0 and count==4) then
		Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_DESTROY)
		local g=Duel.SelectMatchingCard(tp,aux.TRUE,tp,0,LOCATION_MZONE,1,1,nil)
		if g:GetCount()>0 then
			Duel.HintSelection(g)
			if Duel.Destroy(g,REASON_EFFECT)~=0 then
				if ct1==1 then
					local tc=g:GetFirst()
					if not tc then return end
					local e12=Effect.CreateEffect(c)
					e12:SetType(EFFECT_TYPE_SINGLE)
					e12:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
					e12:SetCode(EFFECT_DISABLE)
					e12:SetReset(RESET_EVENT+RESETS_STANDARD+RESET_PHASE+PHASE_END)
					tc:RegisterEffect(e12)
					local e22=Effect.CreateEffect(c)
					e22:SetType(EFFECT_TYPE_SINGLE)
					e22:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
					e22:SetCode(EFFECT_DISABLE_EFFECT)
					e22:SetValue(RESET_TURN_SET)
					e22:SetReset(RESET_EVENT+RESETS_STANDARD+RESET_PHASE+PHASE_END)
					tc:RegisterEffect(e22)
				end
			end
		end
	end
	if count==4 or reverse>0 then
		Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_DESTROY)
		local g=Duel.SelectMatchingCard(tp,aux.TRUE,tp,0,LOCATION_SZONE,1,1,nil)
		if g:GetCount()>0 then
			Duel.HintSelection(g)
			if Duel.Destroy(g,REASON_EFFECT)~=0 then
				if ct1==1 then
					local tc=g:GetFirst()
					if not tc then return end
					local e12=Effect.CreateEffect(c)
					e12:SetType(EFFECT_TYPE_SINGLE)
					e12:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
					e12:SetCode(EFFECT_DISABLE)
					e12:SetReset(RESET_EVENT+RESETS_STANDARD+RESET_PHASE+PHASE_END)
					tc:RegisterEffect(e12)
					local e22=Effect.CreateEffect(c)
					e22:SetType(EFFECT_TYPE_SINGLE)
					e22:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
					e22:SetCode(EFFECT_DISABLE_EFFECT)
					e22:SetValue(RESET_TURN_SET)
					e22:SetReset(RESET_EVENT+RESETS_STANDARD+RESET_PHASE+PHASE_END)
					tc:RegisterEffect(e22)
				end
			end
		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.BreakEffect()
		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