--混沌帝龙 -结末的使者-
local s,id,o=GetID()
o=1e9
function s.initial_effect(c)
	c:EnableReviveLimit()
	--cannot special summon
	local e1=Effect.CreateEffect(c)
	e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_UNCOPYABLE)
	e1:SetType(EFFECT_TYPE_SINGLE)
	e1:SetCode(EFFECT_SPSUMMON_CONDITION)
	c:RegisterEffect(e1)
	--special summon
	local e2=Effect.CreateEffect(c)
	e2:SetDescription(aux.Stringid(id,0))
	e2:SetType(EFFECT_TYPE_FIELD)
	e2:SetCode(EFFECT_SPSUMMON_PROC)
	e2:SetProperty(EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_UNCOPYABLE)
	e2:SetRange(LOCATION_HAND)
	e2:SetCondition(s.spcon)
	e2:SetOperation(s.spop)
	c:RegisterEffect(e2)
	--to grave
	local e3=Effect.CreateEffect(c)
	e3:SetDescription(aux.Stringid(id,1))
	e3:SetCategory(CATEGORY_DECKDES+CATEGORY_DAMAGE)
	e3:SetType(EFFECT_TYPE_IGNITION)
	e3:SetRange(LOCATION_MZONE)
	e3:SetCost(s.sgcost)
	e3:SetTarget(s.sgtg)
	e3:SetOperation(s.sgop)
	c:RegisterEffect(e3)
	Duel.AddCustomActivityCounter(id,ACTIVITY_CHAIN,aux.FALSE)
	--reset
	local e4=Effect.CreateEffect(c)
	e4:SetDescription(aux.Stringid(id,2))
	e4:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
	e4:SetCode(EVENT_CHAIN_NEGATED)
	e4:SetRange(LOCATION_MZONE)
	e4:SetCondition(s.rstcon)
	e4:SetOperation(s.rstop)
	c:RegisterEffect(e4)
	local e4p=e4:Clone()
	e4p:SetCode(EVENT_CHAIN_DISABLED)
	c:RegisterEffect(e4p)
end
function s.spcostfilter(c)
	return c:IsAbleToRemoveAsCost() and c:IsAttribute(ATTRIBUTE_LIGHT+ATTRIBUTE_DARK)
end
function s.spcon(e,c)
	if c==nil then return true end
	local tp=c:GetControler()
	if Duel.GetMZoneCount(tp)<=0 then return false end
	local g=Duel.GetMatchingGroup(s.spcostfilter,tp,LOCATION_GRAVE,0,nil)
	return g:CheckSubGroup(aux.gfcheck,2,2,Card.IsAttribute,ATTRIBUTE_LIGHT,ATTRIBUTE_DARK)
end
function s.spop(e,tp,eg,ep,ev,re,r,rp,c)
	local g=Duel.GetMatchingGroup(s.spcostfilter,tp,LOCATION_GRAVE,0,nil)
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_REMOVE)
	local sg=g:SelectSubGroup(tp,aux.gfcheck,false,2,2,Card.IsAttribute,ATTRIBUTE_LIGHT,ATTRIBUTE_DARK)
	Duel.Remove(sg,POS_FACEUP,REASON_COST)
end
function s.sgcostfilter(c)
	return c:IsAbleToRemoveAsCost() and c:IsType(TYPE_MONSTER)
end
function s.sgcost(e,tp,eg,ep,ev,re,r,rp,chk)
	local c=e:GetHandler()
	if chk==0 then
        local t=c:GetFlagEffectLabel(id)
        if t==nil then t=0 end
		--print("f",Duel.GetCustomActivityCount(id,tp,ACTIVITY_CHAIN),t)
		return Duel.CheckLPCost(tp,1000) 
			and Duel.GetCustomActivityCount(id,tp,ACTIVITY_CHAIN)<=t
			and Duel.GetFlagEffect(tp,id+o)==0
			and Duel.IsExistingMatchingCard(s.sgcostfilter,tp,LOCATION_GRAVE,0,1,nil)
	end
	Duel.PayLPCost(tp,Duel.GetCustomActivityCount(s,tp,ACTIVITY_CHAIN)*1000)
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_REMOVE)
	local g=Duel.SelectMatchingCard(tp,s.sgcostfilter,tp,LOCATION_GRAVE,0,1,1,nil)
    local c=g:GetFirst()
	Duel.Remove(g,POS_FACEUP,REASON_COST)
    e:SetLabel(c:GetAttribute())
	-- Cannot start second time
	Duel.RegisterFlagEffect(tp,id+o,RESET_PHASE+PHASE_END,0,1)
	-- Cannot activitate
    local e1=Effect.CreateEffect(e:GetHandler())
	e1:SetType(EFFECT_TYPE_FIELD)
	e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET+EFFECT_FLAG_OATH)
	e1:SetCode(EFFECT_CANNOT_ACTIVATE)
	e1:SetTargetRange(1,0)
	e1:SetValue(aux.TRUE)
	e1:SetCondition(s.actcon)
	e1:SetReset(RESET_PHASE+PHASE_END)
	Duel.RegisterEffect(e1,tp)
end
function s.actcon(e)
	return Duel.GetFlagEffect(tp,id+o)~=0
end
function s.damfilter(c,p)
	return c:GetOwner()==p and c:IsAbleToGrave() and c:IsType(TYPE_MOSNTER)
end
function s.sgtg(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return true end
	local ctp =Duel.GetFieldGroup(tp,LOCATION_DECK,0)
	local cntp=Duel.GetFieldGroup(tp,0,LOCATION_DECK)
    -- correctly set damage info for Super LP-Z
end
function s.sgfilter(c,p)
	return c:IsLocation(LOCATION_GRAVE) and c:IsType(TYPE_MONSTER) and c:IsControler(p)
end
function s.exdmfilter(c,p,attr)
	return s.sgfilter(c,p) and c:IsAttribute(attr)
end
function s.sgop(e,tp,eg,ep,ev,re,r,rp)
    local attr=e:GetLabel()
	for oc in aux.Next(Duel.GetDecktopGroup(tp,10))   do Duel.MoveSequence(oc,SEQ_DECKBOTTOM) end
	for oc in aux.Next(Duel.GetDecktopGroup(1-tp,10)) do Duel.MoveSequence(oc,SEQ_DECKBOTTOM) end
	local ctp =Duel.GetFieldGroup(tp,LOCATION_DECK,0)
	local cntp=Duel.GetFieldGroup(tp,0,LOCATION_DECK)
	Duel.DiscardDeck(tp,#ctp-10,REASON_EFFECT)
	local tpg=Duel.GetOperatedGroup()
	Duel.DiscardDeck(1-tp,#cntp-10,REASON_EFFECT)
	local ntpg=Duel.GetOperatedGroup()
	local tpc = tpg:FilterCount(s.sgfilter,nil,tp)  + tpg:FilterCount(s.exdmfilter,nil,tp,attr)  
	local ntpc=ntpg:FilterCount(s.sgfilter,nil,1-tp)+ntpg:FilterCount(s.exdmfilter,nil,1-tp,attr)
	if tpc>0  then Duel.Damage(  tp,tpc *300,REASON_EFFECT,true) end
	if ntpc>0 then Duel.Damage(1-tp,ntpc*300,REASON_EFFECT,true) end
	Duel.RDComplete()
end

function s.rstcon(e,tp,eg,ep,ev,re,r,rp)
	local de,dp=Duel.GetChainInfo(ev,CHAININFO_DISABLE_REASON,CHAININFO_DISABLE_PLAYER)
	return rp==tp and dp==1-tp
end
function s.rstop(e,tp,eg,ep,ev,re,r,rp)
	Duel.Hint(HINT_CARD,0,id)
	local c=e:GetHandler()
	local target=c:GetFlagEffectLabel(id)
	local current=Duel.GetCustomActivityCount(id,tp,ACTIVITY_CHAIN)
	--print("->", current)
	if target==nil then
		c:RegisterFlagEffect(id,RESET_EVENT+RESETS_STANDARD+RESET_PHASE+PHASE_END,0,1,current)
	else
		c:SetFlagEffectLabel(id,current)
	end
	Duel.ResetFlagEffect(tp,id+o)
end
