--神心一刀流·樱环妖现
function c66914010.initial_effect(c)
	--Activate
	local e1=Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_ACTIVATE)
	e1:SetCode(EVENT_FREE_CHAIN)
	e1:SetTarget(c66914010.thtg)
	e1:SetOperation(c66914010.thop)
	c:RegisterEffect(e1)	  
end
function c66914010.cfilter(c)
	return (c:IsCode(66914011) or c:IsCode(66914000))
end
function c66914010.filter(c)
	return (c:IsCode(66914011) or c:IsCode(66914000)) and c:IsAbleToHand()
end
function c66914010.spfilter(c,e,tp)
	return c:IsCode(66914000) and c:IsCanBeSpecialSummoned(e,0,tp,false,false)
end
function c66914010.eqfilter(c,tc,tp)
	return c:IsType(TYPE_EQUIP) and c:IsCode(66914011) and c:CheckEquipTarget(tc) and c:CheckUniqueOnField(tp)
end
function c66914010.filter1(c,g,e,tp)
	return (c:IsCode(66914011) or c:IsCode(66914000)) and c:IsAbleToHand() and g:IsExists(c66914010.spfilter1,1,c,g,e,tp)
end
function c66914010.spfilter1(c,g,e,tp)
	return c:IsCode(66914000) and c:IsCanBeSpecialSummoned(e,0,tp,false,false) and g:IsExists(c66914010.eqfilter1,1,c,g,c,tp)
end
function c66914010.eqfilter1(c,g,tc,tp)
	return c:IsType(TYPE_EQUIP) and c:IsCode(66914011) and c:CheckEquipTarget(tc) and c:CheckUniqueOnField(tp)
end
function c66914010.filter3(c)
	return c:IsCode(66914011) and c:IsFaceup()
end
function c66914010.fselect(g,count,reverse,e,tp)
	if reverse==0 then
		if count==4 then
			return g:IsExists(c66914010.filter1,1,nil,g,e,tp)
		else
			return g:IsExists(c66914010.filter,1,nil)
		end
	else
		if count==4 then
			return g:IsExists(c66914010.filter1,1,nil,g,e,tp)
		else
			return g:IsExists(c66914010.spfilter1,1,nil,g,e,tp)
		end
	end
end
function c66914010.thtg(e,tp,eg,ep,ev,re,r,rp,chk,chkc)
	local count=0
	local chain=Duel.GetCurrentChain()
	local g=Duel.GetMatchingGroup(c66914010.cfilter,tp,LOCATION_DECK+LOCATION_GRAVE,0,nil)
	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 return g:CheckSubGroup(c66914010.fselect,1,3,count,reverse,e,tp) end
	local cat=0
	if reverse==0 or (reverse>0 and count==3) then
		cat=cat|CATEGORY_SEARCH 
		cat=cat|CATEGORY_TOHAND
	end
	if count==3 or reverse>0 then
		cat=cat|CATEGORY_SPECIAL_SUMMON
		cat=cat|CATEGORY_EQUIP
		Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,nil,1,tp,LOCATION_DECK)
	end
	count=count+1
	e:SetCategory(cat)
	e:SetLabel(count)
end
function c66914010.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==4) then
		Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_ATOHAND)
		local g=Duel.SelectMatchingCard(tp,c66914010.filter,tp,LOCATION_DECK+LOCATION_GRAVE,0,1,1,nil)
		if g:GetCount()>0  then
			Duel.SendtoHand(g,tp,REASON_EFFECT)
		end
	end
	if count==2 then
		if Duel.IsExistingMatchingCard(c66914010.filter3,tp,LOCATION_SZONE,0,1,nil) and Duel.SelectYesNo(tp,aux.Stringid(66914010,1)) then
			Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_COUNTER)
			local tc=Duel.SelectMatchingCard(tp,c66914010.filter3,tp,LOCATION_SZONE,0,1,1,nil):GetFirst()
			local opt=Duel.SelectOption(tp,aux.Stringid(66914010,2),aux.Stringid(66914010,3),aux.Stringid(66914010,4))+1
			if opt==1 then
				tc:AddCounter(0x1371,1)
			elseif opt==2 then
				tc:AddCounter(0x1372,1)
			else
				tc:AddCounter(0x1373,1)
			end
		end
	end
	if count==5 or reverse>0 then
		if Duel.GetLocationCount(tp,LOCATION_MZONE)<=0 then return end
		Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SPSUMMON)
		local g=Duel.SelectMatchingCard(tp,c66914010.spfilter,tp,LOCATION_DECK+LOCATION_GRAVE,0,1,1,nil,e,tp)
		local tc=g:GetFirst()
		if not tc or Duel.SpecialSummon(tc,0,tp,tp,false,false,POS_FACEUP)==0 then return end
		local tg=Duel.GetMatchingGroup(c66914010.eqfilter,tp,LOCATION_DECK+LOCATION_GRAVE,0,nil,tc,tp)
		if tg:GetCount()>0 and Duel.GetLocationCount(tp,LOCATION_SZONE)>0 then
			Duel.BreakEffect()
			Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_EQUIP)
			local sg=tg:Select(tp,1,1,nil)
			Duel.Equip(tp,sg:GetFirst(),tc)
		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