local cm,m = GetID()
function cm.initial_effect(c)
    local e1=Effect.CreateEffect(c)
	e1:SetDescription(aux.Stringid(m,0))
	e1:SetType(EFFECT_TYPE_ACTIVATE)
	e1:SetCategory(CATEGORY_TOGRAVE)
	e1:SetCode(EVENT_CHAINING)
	e1:SetCondition(cm.con)
	e1:SetTarget(cm.tg)
	e1:SetOperation(cm.op)
	c:RegisterEffect(e1)
	local e2=Effect.CreateEffect(c)
	e2:SetDescription(aux.Stringid(m,1))
	e2:SetType(EFFECT_TYPE_SINGLE)
	e2:SetCode(EFFECT_TRAP_ACT_IN_HAND)
	c:RegisterEffect(e2)
end
function cm.con(e,tp,eg,ep,ev,re,r,rp)
	return (Duel.IsExistingMatchingCard(cm.f2,tp,LOCATION_FZONE,0,1,nil) or ep==1-tp) and re:IsActiveType(TYPE_SPELL)
end
function cm.ff(c,tp)
	return c:IsCode(41418852) and not c:IsForbidden() and c:CheckUniqueOnField(tp)
end
function cm.f2(c)
    return c:IsCode(41418852) and c:IsFaceup()
end
function cm.tg(e,tp,eg,ep,ev,re,r,rp,chk)
    local a , b= Duel.IsExistingMatchingCard(cm.ff,tp,LOCATION_DECK,0,1,nil,tp) , Duel.IsExistingMatchingCard(cm.f2,tp,LOCATION_FZONE,0,1,nil) and Duel.GetFieldGroupCount(tp,0,LOCATION_DECK)>0
    if chk ==0 then return a or b end
    local op = cm.SE(tp,m,{a,2},{b,3})
	e:SetLabel(op)
	if op==1 then
        Duel.SetOperationInfo(0,CATEGORY_ANNOUNCE,nil,0,tp,0)
    end
end
function cm.op(e,tp,eg,ep,ev,re,r,rp)
    local op = e:GetLabel()
    if op == 0 then
        Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_TOFIELD)
    	local tc=Duel.SelectMatchingCard(tp,cm.ff,tp,LOCATION_DECK,0,1,1,nil,tp):GetFirst()
    	if tc then
    		local fc=Duel.GetFieldCard(tp,LOCATION_FZONE,0)
    		if fc then
    			Duel.SendtoGrave(fc,REASON_RULE)
    			Duel.BreakEffect()
    		end
    		Duel.MoveToField(tc,tp,tp,LOCATION_FZONE,POS_FACEUP,true)
    	end
    else
        Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_CODE)
    	getmetatable(e:GetHandler()).announce_filter={TYPE_SPELL,OPCODE_ISTYPE,TYPE_CONTINUOUS+TYPE_FIELD+TYPE_EQUIP+TYPE_RITUAL,OPCODE_ISTYPE,OPCODE_NOT,OPCODE_AND}
    	local ac=Duel.AnnounceCard(tp,table.unpack(getmetatable(e:GetHandler()).announce_filter))
    	if Duel.IsExistingMatchingCard(function(c,cod) return c:IsCode(cod) and c:GetActivateEffect():IsActivatable(1-tp,true) end,tp,0,LOCATION_DECK,1,nil,ac) then
    	    Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_TOGRAVE)
    	    local g=Duel.SelectMatchingCard(tp,function(c,cod) return c:IsCode(cod) and c:GetActivateEffect():IsActivatable(1-tp,true) end,tp,0,LOCATION_DECK,1,1,nil,ac)
    	    if g:GetCount()>0 then
        		Duel.SendtoGrave(g,REASON_EFFECT)
				local ee=g:GetFirst():GetActivateEffect()
        		local tg,op = ee:GetTarget(),ee:GetOperation()
        		oop = function(e,tp,eg,ep,ev,re,r,rp)
        		    tg(e,tp,eg,ep,ev,re,r,rp)
        		    op(e,tp,eg,ep,ev,re,r,rp)
        		end
        		Duel.ChangeChainOperation(ev,oop)
        	end
    	end
    end
end
function cm.SE(tp,id,...)
    local v = {...}
	local off=1
	local ops={}
	local opval={}
	for k,i in ipairs(v) do
	    local a,b = i[1],i[2]
    	if a then
    		ops[off]=aux.Stringid(id,b)
    		opval[off]=k-1
    		off=off+1
    	end
    end
	local op=Duel.SelectOption(tp,table.unpack(ops))+1
	local sel=opval[op]
	return sel
end