--神炎竜ルベリオン
local s,id,o=GetID()
function s.initial_effect(c)
    --fusion material
    c:EnableReviveLimit()
    aux.AddFusionProcCodeFun(c,68468459,aux.FilterBoolFunction(Card.IsFusionAttribute,ATTRIBUTE_DARK),1,true,true)
    --spsummon
    local e1=FusionSpell.CreateSummonEffect(c,{
        fusfilter=s.fusfilter,
        pre_select_mat_location=LOCATION_GRAVE|LOCATION_ONFIELD|LOCATION_REMOVED,
        mat_operation_code_map={
            { [LOCATION_DECK] = FusionSpell.FUSION_OPERATION_GRAVE },
            { [0xff] = FusionSpell.FUSION_OPERATION_SHUFFLE }
        },
        extra_target=s.extra_target
    })
    e1:SetDescription(aux.Stringid(id,0))
    e1:SetType(EFFECT_TYPE_SINGLE+EFFECT_TYPE_TRIGGER_O)
    e1:SetCode(EVENT_SPSUMMON_SUCCESS)
    e1:SetProperty(EFFECT_FLAG_DELAY)
    e1:SetRange(LOCATION_MZONE)
    e1:SetCountLimit(1,id)
    e1:SetCondition(s.spcon)
    e1:SetCost(s.spcost)
    c:RegisterEffect(e1)
end

function s.branded_fusion_check(tp,sg,fc)
    return aux.gffcheck(sg,Card.IsFusionCode,68468459,function(c) return c:IsFusionAttribute(ATTRIBUTE_DARK) and (not c:IsHasEffect(6205579)) end)
end

function s.spcon(e,tp,eg,ep,ev,re,r,rp)
    return e:GetHandler():IsSummonType(SUMMON_TYPE_FUSION)
end

---@param c Card
function s.fusfilter(c)
    return (not c:IsCode(id)) and c:IsLevelBelow(8)
end

function s.spcost(e,tp,eg,ep,ev,re,r,rp,chk)
    if chk==0 then return Duel.IsExistingMatchingCard(Card.IsDiscardable,tp,LOCATION_HAND,0,1,nil) end
    Duel.DiscardHand(tp,Card.IsDiscardable,1,1,REASON_COST+REASON_DISCARD)
end

function s.extra_target(e,tp,eg,ep,ev,re,r,rp,chk)
    if chk==0 then
        return true
    end
    Duel.SetOperationInfo(0,CATEGORY_TODECK,nil,1,tp,LOCATION_ONFIELD+LOCATION_GRAVE+LOCATION_REMOVED)
end