-- 少女的晚祷
local s,id,o=GetID()

function s.initial_effect(c)
	local e1=Effect.CreateEffect(c)
	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
	e1:SetType(EFFECT_TYPE_ACTIVATE)
	e1:SetCode(EVENT_FREE_CHAIN)
	e1:SetCondition(s.rscon)
	e1:SetCost(s.rscost)
    e1:SetTarget(s.rstg)
    e1:SetOperation(s.rsop)
	c:RegisterEffect(e1)
	--destroy replace
	local e2=Effect.CreateEffect(c)
	e2:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
	e2:SetCode(EFFECT_DESTROY_REPLACE)
	e2:SetRange(LOCATION_GRAVE)
	e2:SetTarget(s.reptg)
	e2:SetValue(s.repval)
	e2:SetOperation(s.repop)
	e2:SetLabelObject(e1)
	c:RegisterEffect(e2)
end

function s.rsfilter(c)
	return c:IsType(TYPE_RITUAL) and c:IsAttribute(ATTRIBUTE_DARK) and Duel.IsPlayerCanSpecialSummon(tp,SUMMON_TYPE_RITUAL,POS_FACEUP,tp,c)
end
function s.rsfilter2(c)
    return c:IsType(TYPE_RITUAL) and (c:IsSetCard(0x172) or c:IsSetCard(0xf72)) and Duel.IsPlayerCanSpecialSummon(tp,SUMMON_TYPE_RITUAL,POS_FACEUP,tp,c)
end
function s.rsmonster(tp)
    local g=Duel.GetMatchingGroup(aux.NecroValleyFilter(s.rsfilter),tp,LOCATION_GRAVE,LOCATION_GRAVE,nil,tp)
    if Duel.IsEnvironment(172017010) then
        g=g+Duel.GetMatchingGroup(s.rsfilter2,tp,LOCATION_DECK,0,nil,tp)
    end
    return g
end
function s.rscon(e,tp,eg,ep,ev,re,r,rp)
	local g=Duel.GetMatchingGroup(aux.TRUE,tp,LOCATION_MZONE,0,nil)
	local tc=g:GetFirst()
	return #g==1 and tc:IsRace(RACE_SPELLCASTER)
end
function s.rscost(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return Duel.CheckLPCost(tp,800) end
	Duel.PayLPCost(tp,800)
end

function s.rstg(e,tp,eg,ep,ev,re,r,rp,chk)
    local g=Duel.GetMatchingGroup(aux.TRUE,tp,LOCATION_MZONE,0,nil)
    local tc=g:GetFirst()
    if chk==0 then
        return Duel.IsPlayerCanRelease(tc) and #s.rsmonster(tp)>0
    end
    e:SetLabelObject(tc)
    Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,nil,1,tp,summon_location)
end
function s.rsop(e,tp,eg,ep,ev,re,r,rp)
	local g=Duel.GetMatchingGroup(aux.TRUE,tp,LOCATION_MZONE,0,nil)
	local rc=e:GetLabelObject() 
    local tg=s.rsmonster(tp)
    if Duel.Release(rc,REASON_EFFECT)==1 and #tg>0 then   
        local tc=tg:Select(tp,1,1,nil):GetFirst()
        tc:SetMaterial(Group.FromCards(rc))
        Duel.SpecialSummon(tc,SUMMON_TYPE_RITUAL,tp,tp,false,true,POS_FACEUP)
        tc:CompleteProcedure()
    end
end

function s.repfilter(c,tp)
	return c:IsFaceup() and c:IsRace(RACE_SPELLCASTER) and c:IsLocation(LOCATION_MZONE) and c:GetSequence()==2
		and c:IsControler(tp) and c:IsReason(REASON_EFFECT+REASON_BATTLE) and not c:IsReason(REASON_REPLACE)
end
function s.reptg(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return eg:IsExists(s.repfilter,1,nil,tp) end
	return Duel.SelectEffectYesNo(tp,e:GetHandler(),96)
end
function s.repval(e,c)
	return s.repfilter(c,e:GetHandlerPlayer())
end
function s.repop(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	local rse=e:GetLabelObject()
	if Duel.SSet(tp,c)~=0 then
        local e1=Effect.CreateEffect(c)
        e1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
        e1:SetCode(EVENT_CHAIN_SOLVED)
        e1:SetRange(LOCATION_SZONE)
        e1:SetLabelObject(c:GetActivateEffect())
        e1:SetOperation(s._repop)
        Duel.RegisterEffect(e1,tp)
        local e2=Effect.CreateEffect(c)
        e2:SetType(EFFECT_TYPE_SINGLE)
        e2:SetCode(EFFECT_LEAVE_FIELD_REDIRECT)
        e2:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
        e2:SetValue(LOCATION_REMOVED)
        c:RegisterEffect(e2)
	end
end
function s._repop(e,tp,eg,ep,ev,re,r,rp)
    local te=e:GetLabelObject()
    if te:IsActivatable(tp,true,true) then
        s.rstg(te,tp,eg,ep,ev,re,r,rp,1)
        s.rsop(te,tp,eg,ep,ev,re,r,rp)
    else
        Duel.Destroy(te:GetHandler(),REASON_EFFECT)
    end
    e:Reset()
end
