--神心一刀流·樱繁风光
function c66914001.initial_effect(c)
	--Activate
	local e1=Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_ACTIVATE)
	e1:SetCode(EVENT_FREE_CHAIN)
	e1:SetTarget(c66914001.thtg)
	e1:SetOperation(c66914001.thop)
	c:RegisterEffect(e1)  
end
function c66914001.filter(c)
	return c:IsSetCard(0x373) and c:IsAbleToHand() and not c:IsCode(66914001)
end
function c66914001.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 (count~=1 or Duel.IsExistingMatchingCard(c66914001.filter,tp,LOCATION_GRAVE,0,1,nil)) and (count~=2 or Duel.IsExistingMatchingCard(c66914001.filter,tp,LOCATION_DECK,0,1,nil))
		else
			return (count~=1 or Duel.IsExistingMatchingCard(c66914001.filter,tp,LOCATION_GRAVE,0,1,nil)) and Duel.IsExistingMatchingCard(c66914001.filter,tp,LOCATION_DECK,0,1,nil)
		end
	end
	local cat=0
	if count==3 or reverse>0 then
		cat=cat|CATEGORY_TOHAND
		cat=cat|CATEGORY_SEARCH
		Duel.SetOperationInfo(0,CATEGORY_TOHAND,nil,1,tp,LOCATION_DECK)
	end
	if count==2 then
		cat=cat|CATEGORY_TOHAND
		Duel.SetOperationInfo(0,CATEGORY_TOHAND,nil,1,tp,LOCATION_GRAVE)
	end
	e:SetCategory(cat)
	count=count+1
	e:SetLabel(count)
end
function c66914001.thop(e,tp,eg,ep,ev,re,r,rp)
	local reverse=Duel.GetFlagEffect(tp,66914009)
	local count=e:GetLabel()
	if (reverse>0 and count==3) or (reverse==0) then
		local e1=Effect.CreateEffect(e:GetHandler())
		e1:SetType(EFFECT_TYPE_FIELD)
		e1:SetCode(EFFECT_UPDATE_ATTACK)
		e1:SetTargetRange(LOCATION_MZONE,0)
		e1:SetValue(500)
		e1:SetReset(RESET_PHASE+PHASE_END)
		Duel.RegisterEffect(e1,tp)
	end
	if count==2 then
		Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_ATOHAND)
		local g=Duel.SelectMatchingCard(tp,c66914001.filter,tp,LOCATION_GRAVE,0,1,1,nil)
		if g:GetCount()>0 then
			Duel.SendtoHand(g,nil,REASON_EFFECT)
			Duel.ConfirmCards(1-tp,g)
		end
	end
	if count==3 or reverse>0 then
		Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_ATOHAND)
		local g=Duel.SelectMatchingCard(tp,c66914001.filter,tp,LOCATION_DECK,0,1,1,nil)
		if g:GetCount()>0 then
			Duel.SendtoHand(g,nil,REASON_EFFECT)
			Duel.ConfirmCards(1-tp,g)
		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