-- 黄昏的丧钟
local s,id,o=GetID()
function s.initial_effect(c)
    --Activate
	local e1=Effect.CreateEffect(c)
	e1:SetCategory(CATEGORY_ATKCHANGE)
	e1:SetType(EFFECT_TYPE_ACTIVATE)
	e1:SetCode(EVENT_FREE_CHAIN)
    e1:SetCondition(s.atkcon)
	e1:SetOperation(s.atkop)
	c:RegisterEffect(e1)
    --Deckdes
    local e2=Effect.CreateEffect(c)
    e2:SetCategory(CATEGORY_DECKDES)
	e2:SetType(EFFECT_TYPE_SINGLE+EFFECT_TYPE_TRIGGER_F)
	e2:SetCode(EVENT_TO_GRAVE)
	e2:SetCondition(s.descon)
	e2:SetTarget(s.destg)
	e2:SetOperation(s.desop)
	c:RegisterEffect(e2)
    --Win
    local e3=Effect.CreateEffect(c)
	e3:SetType(EFFECT_TYPE_SINGLE+EFFECT_TYPE_CONTINUOUS)
	e3:SetCode(EVENT_TO_GRAVE)
	e3:SetCondition(s.wincon)
	e3:SetOperation(s.winop)
	c:RegisterEffect(e3)
end

function s.atkfilter(c)
    return c:IsAttribute(ATTRIBUTE_DARK)
end
function s.atkcon(e,tp,eg,ep,ev,re,r,rp)
    return Duel.GetMatchingGroupCount(s.atkfilter,tp,LOCATION_MZONE,nil,nil)>0
end
function s.atkop(e,tp,eg,ep,ev,re,r,rp)
    local g=Duel.GetMatchingGroup(s.atkfilter,tp,LOCATION_ONFIELD,nil,nil)
	local tc=g:GetFirst()
	while tc do
        local e1=Effect.CreateEffect(e:GetHandler())
		e1:SetType(EFFECT_TYPE_SINGLE)
		e1:SetCode(EFFECT_UPDATE_ATTACK)
		e1:SetReset(RESET_EVENT)
		e1:SetValue(500)
		tc:RegisterEffect(e1)
        tc=g:GetNext()
    end
end

function s.descon(e,tp,eg,ep,ev,re,r,rp)
    return Duel.GetMatchingGroupCount(aux.TRUE,tp,LOCATION_DECK,nil,nil)>0
end
function s.destg(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return true end
    local ct=Duel.GetFlagEffectLabel(tp,id)
    if ct==nil then ct=0 end
	Duel.SetOperationInfo(0,CATEGORY_DECKDES,e:GetHandler(),0,tp,ct+2)
	Duel.SetOperationInfo(0,CATEGORY_DECKDES,e:GetHandler(),0,1-tp,ct+2)
end
function s.desop(e,tp,eg,ep,ev,re,r,rp)
    local c=e:GetHandler()
    local ct=Duel.GetFlagEffectLabel(tp,id)
    if ct==nil then 
        Duel.RegisterFlagEffect(tp,id,0,0,1,2)
        ct=0
    else
        Duel.SetFlagEffectLabel(tp,id,ct+2)
    end
    ct=ct+2
	local g1=Duel.GetDecktopGroup(tp,ct)
	local g2=Duel.GetDecktopGroup(1-tp,ct)
	g1:Merge(g2)
    local tc=g1:GetFirst()
	while tc do
        local e1=Effect.CreateEffect(c)
        e1:SetType(EFFECT_TYPE_SINGLE)
        e1:SetCode(EFFECT_DISABLE)
        e1:SetReset(RESET_EVENT+0x17a0000)
        tc:RegisterEffect(e1)
        local e2=Effect.CreateEffect(c)
        e2:SetType(EFFECT_TYPE_SINGLE)
        e2:SetCode(EFFECT_DISABLE_EFFECT)
        e2:SetReset(RESET_EVENT+0x17a0000)
        tc:RegisterEffect(e2)
        tc=g1:GetNext()
    end
    Duel.SendtoGrave(g1,REASON_EFFECT)
	Duel.SendtoDeck(c,nil,SEQ_DECKSHUFFLE,REASON_EFFECT)
end

function s.losefilter(c)
    return not c:IsCode(57774843) and not (c:IsSetCard(0x38) and bit.band(c:GetOriginalAttribute(),ATTRIBUTE_LIGHT)>0)
end
function s.wincon(e,tp,eg,ep,ev,re,r,rp)
    return Duel.GetMatchingGroupCount(aux.TRUE,tp,LOCATION_DECK,nil,nil)==0
end
function s.winop(e,tp,eg,ep,ev,re,r,rp)
	Duel.Hint(HINT_CARD,0,id)
    if Duel.GetMatchingGroupCount(Card.IsCode,tp,LOCATION_MZONE,nil,nil,57774843)>0
        and Duel.GetMatchingGroupCount(Card.IsSetCard,tp,LOCATION_MZONE,nil,nil,0x38)>0
        and Duel.GetMatchingGroupCount(s.losefilter,tp,LOCATION_MZONE,nil,nil,0)==0 then
        Duel.Win(tp,0xe6)
    else
        Duel.Win(1-tp,0xe6)
    end
end