--灵摆女巫
local cm,m,o=GetID()
function cm.initial_effect(c)
	--pendulum summon
	aux.EnablePendulumAttribute(c)
	--Destroy P
	local e1=Effect.CreateEffect(c)
	e1:SetCategory(CATEGORY_DESTROY+CATEGORY_TOHAND+CATEGORY_SEARCH)
	e1:SetType(EFFECT_TYPE_SINGLE+EFFECT_TYPE_TRIGGER_O)
	e1:SetCode(EVENT_SUMMON_SUCCESS)
	e1:SetProperty(EFFECT_FLAG_CARD_TARGET+EFFECT_FLAG_DELAY)
	e1:SetCountLimit(1,m)
	e1:SetTarget(cm.destg)
	e1:SetOperation(cm.desop)
	c:RegisterEffect(e1)
	local e4=e1:Clone()
	e4:SetCode(EVENT_SPSUMMON_SUCCESS)
	e4:SetCondition(cm.condition)
	c:RegisterEffect(e4)
	--pendulum
	local e2=Effect.CreateEffect(c)
	e2:SetType(EFFECT_TYPE_SINGLE+EFFECT_TYPE_TRIGGER_O)
	e2:SetCode(EVENT_DESTROYED)
	e2:SetProperty(EFFECT_FLAG_DELAY)
	e2:SetCondition(cm.pencon)
	e2:SetTarget(cm.pentg)
	e2:SetOperation(cm.penop)
	c:RegisterEffect(e2)
    --to EX
    local e3=Effect.CreateEffect(c)
    e3:SetCategory(CATEGORY_TOEXTRA)
	e3:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_TRIGGER_O)
	e3:SetCode(EVENT_DESTROYED)
	e3:SetProperty(EFFECT_FLAG_DELAY)
    e3:SetRange(LOCATION_PZONE)
    e3:SetCountLimit(1,m+o)
    e3:SetCondition(cm.tecon)
	e3:SetTarget(cm.tetg)
	e3:SetOperation(cm.teop)
	c:RegisterEffect(e3)
end
function cm.desfilter(c)
	return c:IsType(TYPE_PENDULUM) and c:IsFaceup()
end
function cm.thfilter(c)
	return c:IsLevelBelow(4) and c:IsType(TYPE_PENDULUM) and c:IsAbleToHand()
end
function cm.destg(e,tp,eg,ep,ev,re,r,rp,chk)
	if chkc then return chkc:IsLocation(LOCATION_PZONE) and cm.desfilter(chkc) and chkc~=e:GetHandler() end
	if chk==0 then return Duel.IsExistingTarget(cm.desfilter,tp,LOCATION_PZONE,LOCATION_PZONE,1,e:GetHandler())
		and Duel.IsExistingMatchingCard(cm.thfilter,tp,LOCATION_DECK,0,1,nil) end
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_DESTROY)
	local g=Duel.SelectTarget(tp,cm.desfilter,tp,LOCATION_PZONE,LOCATION_PZONE,1,1,e:GetHandler())
	g:AddCard(e:GetHandler())
	Duel.SetOperationInfo(0,CATEGORY_DESTROY,g,2,0,0)
	Duel.SetOperationInfo(0,CATEGORY_TOHAND,nil,1,tp,LOCATION_DECK)
end
function cm.desop(e,tp,eg,ep,ev,re,r,rp)
	local tc=Duel.GetFirstTarget()
	if tc:IsRelateToEffect(e) and e:GetHandler():IsRelateToEffect(e) and Duel.Destroy(Group.FromCards(tc,e:GetHandler()),REASON_EFFECT)==2 then
		Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_ATOHAND)
		local g=Duel.SelectMatchingCard(tp,cm.thfilter,tp,LOCATION_DECK,0,1,1,nil)
		if g:GetCount()>0 then
			Duel.SendtoHand(g,nil,REASON_EFFECT)
			Duel.ConfirmCards(1-tp,g)
		end
	end
end
function cm.condition(e,tp,eg,ep,ev,re,r,rp)
	return e:GetHandler():IsSummonType(SUMMON_TYPE_PENDULUM)
end
function cm.pencon(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	return c:IsPreviousLocation(LOCATION_MZONE) and c:IsFaceup()
end
function cm.pentg(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return Duel.CheckLocation(tp,LOCATION_PZONE,0) or Duel.CheckLocation(tp,LOCATION_PZONE,1) end
end
function cm.penop(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	if c:IsRelateToEffect(e) then
		Duel.MoveToField(c,tp,tp,LOCATION_PZONE,POS_FACEUP,true)
	end
end
function cm.cfilter(c,tp)
    return c:IsPreviousLocation(LOCATION_MZONE) and c:IsPreviousPosition(POS_FACEUP) and c:IsPreviousControler(tp)
        and c:IsType(TYPE_XYZ+TYPE_SYNCHRO+TYPE_FUSION) and c:IsReason(REASON_DESTROY)
        and (c:IsReason(REASON_BATTLE) or c:GetReasonPlayer()==1-tp)
end
function cm.tefilter(c,race)
    return c:IsType(TYPE_PENDULUM) and c:GetOriginalRace()&race~=0
end
function cm.tecon(e,tp,eg,ep,ev,re,r,rp)
    return eg:IsExists(cm.cfilter,1,nil,tp)
end
function cm.tetg(e,tp,eg,ep,ev,re,r,rp,chk)
    local g=eg:Filter(cm.cfilter,nil,tp)
    local tc=g:GetFirst()
    local sg=Group.CreateGroup()
    while tc do
        local rg=Duel.GetMatchingGroup(cm.tefilter,tp,LOCATION_DECK,0,nil,tc:GetOriginalRace())
        sg:Merge(rg)
        tc=g:GetNext()
    end
    if chk==0 then return sg:GetCount()>0 end
end
function cm.teop(e,tp,eg,ep,ev,re,r,rp)
    local g=eg:Filter(cm.cfilter,nil,tp)
    local tc=g:GetFirst()
    local sg=Group.CreateGroup()
    while tc do
        local rg=Duel.GetMatchingGroup(cm.tefilter,tp,LOCATION_DECK,0,nil,tc:GetOriginalRace())
        sg:Merge(rg)
        tc=g:GetNext()
    end
    if sg:GetCount()>0 then
        Duel.Hint(HINT_SELECTMSG,tp,aux.Stringid(84521924,2))
        local exg=sg:Select(tp,1,1,nil)
        Duel.SendtoExtraP(exg,nil,REASON_EFFECT)
    end
end