local cm,m = GetID()
cm.name = "交给我吧！军贯全席"
function cm.initial_effect(c)
    aux.AddCodeList(c,24639891)
    local e1 = Effect.CreateEffect(c)
    e1:SetCategory(CATEGORY_SPECIAL_SUMMON+CATEGORY_ATKCHANGE)
    e1:SetType(EFFECT_TYPE_ACTIVATE)
    e1:SetCode(EVENT_FREE_CHAIN)
    e1:SetCondition(cm.con)
    e1:SetTarget(cm.tg)
    e1:SetOperation(cm.op)
    c:RegisterEffect(e1)
end
function cm.con(e,tp,eg,ep,ev,re,r,rp)
    return Duel.IsExistingMatchingCard(function(c) return not c:IsAttack(0) end,tp,0,LOCATION_MZONE,1,nil)
end
function cm.tg(e,tp,eg,ep,ev,re,r,rp,chk)
    local g = Duel.GetMatchingGroup(function(c,e,tp) return c:IsSetCard(0x166) and c:IsCanBeSpecialSummoned(e,0,tp,false,false) end,tp,LOCATION_DECK,0,nil,e,tp)
    if chk==0 then return g:CheckSubGroup(cm.f,2,2,tp) and Duel.GetLocationCount(tp,LOCATION_MZONE)>1 and not Duel.IsPlayerAffectedByEffect(tp,59822133) end
    Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,nil,2,tp,LOCATION_DECK)
end
function cm.f(g,tp)
    return g:FilterCount(function(c) return c:IsCode(24639891) end,nil)>0 and g:GetClassCount(Card.GetCode)>1 and Duel.IsExistingMatchingCard(function(c,g) return c:IsXyzSummonable(g) end,tp,LOCATION_EXTRA,0,1,nil,g)
end
function cm.op(e,tp,eg,ep,ev,re,r,rp)
    local g = Duel.GetMatchingGroup(function(c,e,tp) return c:IsSetCard(0x166) and c:IsCanBeSpecialSummoned(e,0,tp,false,false) end,tp,LOCATION_DECK,0,nil,e,tp)
    if not g:CheckSubGroup(cm.f,2,2,tp) or Duel.GetLocationCount(tp,LOCATION_MZONE)<1 or Duel.IsPlayerAffectedByEffect(tp,59822133) then return false end
    ::start::
    Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SPSUMMON)
    local mg = g:SelectSubGroup(tp,cm.f,true,2,2,tp)
    if not mg then goto start end
    Duel.SpecialSummon(mg,0,tp,tp,false,false,POS_FACEUP)
    Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SPSUMMON)
    ::xyz::
    local xc = Duel.SelectMatchingCard(tp,function(c,g) return c:IsXyzSummonable(g) end,tp,LOCATION_EXTRA,0,1,1,nil,mg):GetFirst()
    if not xc then goto xyz end
    Duel.Overlay(xc,mg)
    xc:SetMaterial(mg)
    if Duel.SpecialSummon(xc,SUMMON_TYPE_XYZ,tp,tp,false,false,POS_FACEUP) then
        xc:CompleteProcedure()
    end
    local atk = xc:GetAttack()
    local def = xc:GetDefense()
    if atk~=0 then
        for c in aux.Next(Duel.GetFieldGroup(tp,0,LOCATION_MZONE,nil)) do
            local e1=Effect.CreateEffect(e:GetHandler())
        	e1:SetType(EFFECT_TYPE_SINGLE)
            e1:SetCode(EFFECT_UPDATE_ATTACK)
        	e1:SetReset(RESET_EVENT+RESETS_STANDARD)
        	e1:SetRange(LOCATION_MZONE)
        	e1:SetValue(-atk)
        	c:RegisterEffect(e1)
        end
    end
    if def~=0 then
        Duel.SetLP(1-tp,Duel.GetLP(1-tp)-def)
    end
    g = Duel.GetMatchingGroup(function(c,e,tp) return c:IsSetCard(0x166) and c:IsCanBeSpecialSummoned(e,0,tp,false,false) end,tp,LOCATION_DECK,0,nil,e,tp)
    if Duel.IsExistingMatchingCard(function(c) return not c:IsAttack(0) end,tp,0,LOCATION_MZONE,1,nil) and g:CheckSubGroup(cm.f,2,2,tp) and Duel.GetLocationCount(tp,LOCATION_MZONE)>1 and not Duel.IsPlayerAffectedByEffect(tp,59822133) then goto start end
end