--神心一刀流·樱袭冲天
function c66914014.initial_effect(c)
	--Activate
	local e1=Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_ACTIVATE)
	e1:SetCode(EVENT_FREE_CHAIN)
	e1:SetTarget(c66914014.thtg)
	e1:SetOperation(c66914014.thop)
	c:RegisterEffect(e1)	  
end
function c66914014.filter(c,e,tp)
	return c:IsSetCard(0x373) and c:IsCanBeSpecialSummoned(e,0,tp,false,false,POS_FACEUP)
end
function c66914014.exfilter(c,e,tp,mc)
	return c:IsCode(66914013) and c:IsType(TYPE_XYZ)
		and mc:IsCanBeXyzMaterial(c)
		and c:IsCanBeSpecialSummoned(e,SUMMON_TYPE_XYZ,tp,false,false)
		and Duel.GetLocationCountFromEx(tp,tp,mc,c)>0
end
function c66914014.ovfilter(c,e,tp)
	return aux.MustMaterialCheck(c,tp,EFFECT_MUST_BE_XMATERIAL)
		and Duel.IsExistingMatchingCard(c66914014.exfilter,tp,LOCATION_EXTRA,0,1,nil,e,tp,c) and c:IsCode(66914000)
end
function c66914014.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.GetLocationCount(tp,LOCATION_MZONE)>0 and Duel.IsExistingMatchingCard(c66914014.filter,tp,LOCATION_GRAVE,0,1,nil,e,tp) and (count~=3 or Duel.IsExistingMatchingCard(c66914014.ovfilter,tp,LOCATION_MZONE,0,1,nil,e,tp))
		else
			return Duel.IsExistingMatchingCard(c66914014.ovfilter,tp,LOCATION_MZONE,0,1,nil,e,tp) and (count~=3 or (Duel.GetLocationCount(tp,LOCATION_MZONE)>0 and Duel.IsExistingMatchingCard(c66914014.filter,tp,LOCATION_GRAVE,0,1,nil,e,tp)))
		end
	end
	local cat=0
	if count==3 or reverse>0 then
		cat=cat|CATEGORY_SPECIAL_SUMMON
		Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,nil,1,tp,LOCATION_EXTRA)
	end
	if (reverse==0) or count==3 then
		cat=cat|CATEGORY_SPECIAL_SUMMON
		Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,nil,1,tp,LOCATION_GRAVE)
	end
	e:SetCategory(cat)
	count=count+1
	e:SetLabel(count)
end
function c66914014.ovfilter2(c)
	return c:IsCanOverlay() and c:IsSetCard(0x373)
end
function c66914014.thop(e,tp,eg,ep,ev,re,r,rp)
	local reverse=Duel.GetFlagEffect(tp,66914009)
	local count=e:GetLabel()
	local check=0
	if count==3 then
		check=1
	end
	if (reverse>0 and count==4) or (reverse==0) then
		if Duel.GetLocationCount(tp,LOCATION_MZONE)>0 then
			Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SPSUMMON)
			local g=Duel.SelectMatchingCard(tp,c66914014.filter,tp,LOCATION_GRAVE,0,1,1,nil,e,tp)
			if g:GetCount()>0  then
				if Duel.SpecialSummon(g,0,tp,tp,false,false,POS_FACEUP)~=0 then
					if g:GetFirst():IsType(TYPE_XYZ) and check==1 then
						local g1=Duel.GetMatchingGroup(c66914014.ovfilter2,tp,LOCATION_GRAVE,0,c)
						if g1:GetCount()>0 then
							Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_XMATERIAL)
							local tg=g1:Select(tp,1,1,nil)
							Duel.HintSelection(tg)
							local tc=tg:GetFirst()
							if not tc:IsImmuneToEffect(e) then
								Duel.Overlay(g:GetFirst(),tg)
							end
						end
					end			  
				end
			end
		end
	end
	if count==4 or reverse>0 then
		Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_XMATERIAL)
		local g=Duel.SelectMatchingCard(tp,c66914014.ovfilter,tp,LOCATION_MZONE,0,1,1,nil,e,tp)
		if g:GetCount()>0 then
			Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SPSUMMON)
			local tc=Duel.SelectMatchingCard(tp,c66914014.exfilter,tp,LOCATION_EXTRA,0,1,1,nil,e,tp,g:GetFirst()):GetFirst()
			local mg=g:GetFirst():GetOverlayGroup()
			if mg:GetCount()>0 then
				Duel.Overlay(tc,mg)
			end
			tc:SetMaterial(Group.FromCards(g:GetFirst()))
			Duel.Overlay(tc,Group.FromCards(g:GetFirst()))
			if Duel.SpecialSummon(tc,SUMMON_TYPE_XYZ,tp,tp,false,false,POS_FACEUP)~=0 then
				tc:CompleteProcedure()
				if check==1 then
					local g=Duel.GetMatchingGroup(c66914014.ovfilter2,tp,LOCATION_GRAVE,0,c)
					if g:GetCount()>0 then
						Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_XMATERIAL)
						local tg=g:Select(tp,1,1,nil)
						Duel.HintSelection(tg)
						local tc=tg:GetFirst()
						if not tc:IsImmuneToEffect(e) then
							Duel.Overlay(g:GetFirst(),tg)
						end
					end
				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.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