--無限泡影
local s,id,o=GetID()
function s.initial_effect(c)
	--Activate
	local e1=Effect.CreateEffect(c)
	e1:SetCategory(CATEGORY_DESTROY+CATEGORY_SPECIAL_SUMMON)
	e1:SetType(EFFECT_TYPE_ACTIVATE)
	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
	e1:SetCode(EVENT_FREE_CHAIN)
	e1:SetHintTiming(0,TIMINGS_CHECK_MONSTER)
	e1:SetCountLimit(1,EFFECT_COUNT_CODE_OATH)
	e1:SetTarget(s.target)
	e1:SetOperation(s.activate)
	c:RegisterEffect(e1)
	--act in hand
	local e2=Effect.CreateEffect(c)
	e2:SetType(EFFECT_TYPE_SINGLE)
	e2:SetCode(EFFECT_TRAP_ACT_IN_HAND)
	e2:SetCondition(s.handcon)
	c:RegisterEffect(e2)
end
function s.filter(c,e,tp)
	return c:IsSetCard(0xa) and c:IsCanBeSpecialSummoned(e,0,tp,false,false) and c:IsAttribute(ATTRIBUTE_DARK)
end
function s.xyzsumfilter(c)
	return c:IsType(TYPE_XYZ) and c:IsSetCard(0xa) and c:IsXyzSummonable(nil)
end
function s.asumfilter(c)
	return (c:IsSummonable(true,nil,1) or c:IsMSetable(true,nil,1)) and c:IsAttribute(ATTRIBUTE_DARK)
end
function s.tgfilter(c)
	return not c:IsAttribute(ATTRIBUTE_DARK)
end
function s.target(e,tp,eg,ep,ev,re,r,rp,chk,chkc)
	if chkc then return chkc:IsControler(1-tp) and chkc:IsOnField() end
	if chk==0 then return Duel.IsExistingTarget(aux.True,tp,0,LOCATION_ONFIELD,1,nil) and Duel.GetLocationCount(tp,LOCATION_MZONE)>1 and Duel.IsExistingMatchingCard(s.filter,tp,LOCATION_DECK,0,1,nil,e,tp) and Duel.IsExistingMatchingCard(s.filter,tp,LOCATION_HAND,0,1,nil,e,tp) and not Duel.IsPlayerAffectedByEffect(tp,59822133) end
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_DESTROY)
	local g=Duel.SelectTarget(tp,aux.True,tp,0,LOCATION_ONFIELD,1,1,nil)
	Duel.SetOperationInfo(0,CATEGORY_DESTROY,g,1,0,0)
    Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,nil,2,tp,LOCATION_DECK+LOCATION_HAND)
end
function s.activate(e,tp,eg,ep,ev,re,r,rp)
	local tc=Duel.GetFirstTarget()
	if tc:IsRelateToEffect(e) and Duel.Destroy(tc,REASON_EFFECT)~=0 then
		if Duel.GetLocationCount(tp,LOCATION_MZONE)>=2 and not Duel.IsPlayerAffectedByEffect(tp,59822133) then
			Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SPSUMMON)
			local g1=Duel.SelectMatchingCard(tp,s.filter,tp,LOCATION_DECK,0,1,1,nil,e,tp)
			local g2=Duel.SelectMatchingCard(tp,s.filter,tp,LOCATION_HAND,0,1,1,nil,e,tp)
			g1:Merge(g2)
			if g1:GetCount()>1 then
				local res=Duel.SpecialSummon(g1,0,tp,tp,false,false,POS_FACEUP)
				Duel.RaiseEvent(e:GetHandler(),EVENT_ADJUST,nil,0,PLAYER_NONE,PLAYER_NONE,0)
				local b1=Duel.IsExistingMatchingCard(s.asumfilter,tp,LOCATION_HAND,0,1,nil)
				local b2=Duel.IsExistingMatchingCard(s.xyzsumfilter,tp,LOCATION_EXTRA,0,1,nil)
				if res~=0 and (b1 or b2) then
					local off=1
					local ops,opval={},{}
					if b1 then
						ops[off]=aux.Stringid(id,0)
						opval[off]=0
						off=off+1
					end
					if b2 then
						ops[off]=aux.Stringid(id,1)
						opval[off]=1
						off=off+1
					end
					ops[off]=aux.Stringid(id,2)
					opval[off]=2
					local op=Duel.SelectOption(tp,table.unpack(ops))+1
					local sel=opval[op]
					if sel==0 then
						Duel.BreakEffect()
						Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SUMMON)
						local g=Duel.SelectMatchingCard(tp,s.asumfilter,tp,LOCATION_HAND,0,1,1,nil)
						local tc=g:GetFirst()
						if tc then
							local s1=tc:IsSummonable(true,nil,1)
							local s2=tc:IsMSetable(true,nil,1)
							if (s1 and s2 and Duel.SelectPosition(tp,tc,POS_FACEUP_ATTACK+POS_FACEDOWN_DEFENSE)==POS_FACEUP_ATTACK) or not s2 then
								Duel.Summon(tp,tc,true,nil,1)
							else
								Duel.MSet(tp,tc,true,nil,1)
							end
						end
					end
					if sel==1 then
						Duel.BreakEffect()
						Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SPSUMMON)
						local sg2=Duel.SelectMatchingCard(tp,s.xyzsumfilter,tp,LOCATION_EXTRA,0,1,1,nil)
						Duel.XyzSummon(tp,sg2:GetFirst(),nil)
					end
				end
			end
		end
    end
end
function s.handfilter(c)
	return c:IsLevelAbove(5) and c:IsFaceup()
end
function s.handcon(e)
	return Duel.IsExistingMatchingCard(s.handfilter,e:GetHandlerPlayer(),0,LOCATION_MZONE,1,nil)
end