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_TOHAND+CATEGORY_TOGRAVE+CATEGORY_REMOVE)
	e1:SetProperty(EFFECT_FLAG_DELAY)
	e1:SetCode(EVENT_TO_HAND)
	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.f(c,tp)
	return c:IsControler(1-tp) and c:IsPreviousLocation(LOCATION_DECK)
end
function cm.con(e,tp,eg,ep,ev,re,r,rp)
	return Duel.GetCurrentPhase()~=PHASE_DRAW and eg:IsExists(cm.f,1,nil,tp)
end
function cm.ff(c,tp)
	return c:IsCode(41418852) and not c:IsForbidden() and c:CheckUniqueOnField(tp)
end
function cm.f1(c)
    return c:IsSetCard(0x14a) and c:IsAbleToHand()
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 , c = Duel.IsExistingMatchingCard(cm.ff,tp,LOCATION_DECK,0,1,nil,tp) , Duel.IsExistingMatchingCard(cm.f1,tp,LOCATION_DECK,0,1,nil) , Duel.IsExistingMatchingCard(cm.f2,tp,LOCATION_FZONE,0,1,nil) and eg:FilterCount(Card.IsAbleToGrave,nil)>0 and Duel.GetFieldGroupCount(tp,0,LOCATION_DECK)>0
    if chk ==0 then return a or b or c end
    local op = cm.SE(tp,m,{a,2},{b,3},{c,4})
    e:SetLabel(op)
    if op==1 then
        Duel.SetOperationInfo(0,CATEGORY_TOHAND,nil,1,0,LOCATION_DECK)
    end
	if op==2 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
    elseif op == 1 then
        Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_ATOHAND)
    	local g=Duel.SelectMatchingCard(tp,cm.f1,tp,LOCATION_DECK,0,1,1,nil)
    	if g:GetCount()>0 then
    		Duel.SendtoHand(g,nil,REASON_EFFECT)
    		Duel.ConfirmCards(1-tp,g)
    	end
    else
        Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_CODE)
    	getmetatable(e:GetHandler()).announce_filter={TYPE_FUSION+TYPE_SYNCHRO+TYPE_XYZ+TYPE_LINK,OPCODE_ISTYPE,OPCODE_NOT}
    	local ac=Duel.AnnounceCard(tp,table.unpack(getmetatable(e:GetHandler()).announce_filter))
    	if Duel.IsExistingMatchingCard(Card.IsCode,tp,0,LOCATION_DECK,1,nil,ac) then
    	    Duel.SendtoGrave(eg,REASON_EFFECT)
    	    Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_ATOHAND)
    	    local g=Duel.SelectMatchingCard(tp,function(c,cod) return c:IsCode(cod) and c:IsAbleToHand() end,tp,0,LOCATION_DECK,1,1,nil,ac)
    	    if g:GetCount()>0 then
        		Duel.SendtoHand(g,1-tp,REASON_EFFECT)
        		Duel.ConfirmCards(tp,g)
        	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