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_DESTROY+CATEGORY_DISABLE)
	e1:SetCode(EVENT_FREE_CHAIN)
	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.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.cfilter(c)
	return c:IsFaceup() and c:IsType(TYPE_CONTINUOUS+TYPE_FIELD)
end
function cm.dfilter(c)
	return c:IsType(TYPE_SPELL+TYPE_TRAP) and c:IsFaceup()
end
function cm.con(e,tp,eg,ep,ev,re,r,rp,chk)
	return Duel.IsExistingMatchingCard(cm.cfilter,tp,0,LOCATION_ONFIELD,1,nil)
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_CONTINUOUS+TYPE_FIELD,OPCODE_ISTYPE}
    	local ac=Duel.AnnounceCard(tp,table.unpack(getmetatable(e:GetHandler()).announce_filter))
    	if Duel.IsExistingMatchingCard(function(c,cod,tp) return c:IsCode(cod) and not c:IsForbidden() and c:CheckUniqueOnField(tp) end,tp,0,LOCATION_DECK,1,nil,ac,1-tp) and Duel.IsExistingMatchingCard(cm.dfilter,tp,0,LOCATION_ONFIELD,1,nil) then
			Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_DESTROY)
			local tc=Duel.SelectMatchingCard(tp,cm.dfilter,tp,0,LOCATION_ONFIELD,1,1,nil)
			if Duel.Destroy(tc,REASON_EFFECT)~=0 then
				Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_OPERATECARD)
				local g=Duel.SelectMatchingCard(tp,function(c,cod,tp) return c:IsCode(cod) and not c:IsForbidden() and c:CheckUniqueOnField(tp) end,tp,0,LOCATION_DECK,1,1,nil,ac,1-tp):GetFirst()
				if g and g:IsType(TYPE_FIELD) then
					local fc=Duel.GetFieldCard(tp,0,LOCATION_FZONE)
					if fc then
						Duel.SendtoGrave(fc,REASON_RULE)
						Duel.BreakEffect()
					end
					Duel.MoveToField(g,tp,1-tp,LOCATION_FZONE,POS_FACEUP,true)
				else
					Duel.MoveToField(g,tp,1-tp,LOCATION_SZONE,POS_FACEUP,true)
				end
			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