-- 巫独少女 莉莉安
local s,id,o=GetID()
function s.initial_effect(c)
	-- no xyz with one
	local e1=Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_FIELD)
	e1:SetCode(id)
	e1:SetRange(LOCATION_MZONE)
	e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
	e1:SetTargetRange(1,1)
	c:RegisterEffect(e1)
	-- ritual summon
	local e3=Effect.CreateEffect(c)
	e3:SetCategory(CATEGORY_SPECIAL_SUMMON)
	e3:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_TRIGGER_O)
	e3:SetCode(EVENT_PAY_LPCOST)
	e3:SetRange(LOCATION_GRAVE)
	e3:SetProperty(EFFECT_FLAG_DELAY)
	e3:SetCondition(s.rscon)
	e3:SetTarget(s.rstg)
	e3:SetOperation(s.rsop)
	c:RegisterEffect(e3)
	
	if not s.gf then
		s.originXyzAlterFilter=aux.XyzAlterFilter
		aux.XyzAlterFilter=s.XyzAlterFilter
		s.originMustMaterialCheck=aux.MustMaterialCheck
		aux.MustMaterialCheck=s.MustMaterialCheck
		s.gf=true
	end
end

function s.XyzAlterFilter(c,alterf,xyzc,e,tp,alterop)
	return not Duel.IsPlayerAffectedByEffect(tp,id) and s.originXyzAlterFilter(c,alterf,xyzc,e,tp,alterop)
end
function s.MustMaterialCheck(v,tp,code)
	if code==EFFECT_MUST_BE_XMATERIAL and Duel.IsPlayerAffectedByEffect(tp,id) then return false end
	return s.originMustMaterialCheck(v,tp,code)
end

function s.mfilter(c,e)
	return c:IsFaceup() and s.GetLevel(c)>0 and not c:IsImmuneToEffect(e) and c:IsReleasable()
end
function s.filter(c,e,tp)
	return c:IsSetCard(0xf72)
end
function s.GetLevel(c)
	if c==nil then return 0 end
	if c:GetRank()>0 then
		return c:GetRank()
	else
		return c:GetLevel()
	end
end
function s.GetRitualLevel(c,rc)
	if c:IsType(TYPE_XYZ) then
		return c:GetRank()
	else
		return s.originGetRitualLevel(c,rc)
	end
end
function s.RitualCheckEqual(g,c,lv)
	return g:CheckWithSumEqual(s.GetLevel,lv,#g,#g,c)
end
function s.rcheck(gc)
	return  function(tp,g,c)
			  return g:IsContains(gc)
		    end
end
function s.rscon(e,tp,eg,ep,ev,re,r,rp)
	return rp~=tp
end
function s.rstg(e,tp,eg,ep,ev,re,r,rp,chk)
	local c=e:GetHandler()
	if chk==0 then
		return c:IsCanBeSpecialSummoned(e,0,tp,tp,false)
	end
	Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,nil,1,tp,LOCATION_GRAVE+LOCATION_DECK)
end

function s.rsop(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	if Duel.SpecialSummon(c,0,tp,tp,false,false,POS_FACEUP) then
		s.originGetRitualLevel=Card.GetRitualLevel
		Card.GetRitualLevel=s.GetRitualLevel
		local mg1=Duel.GetRitualMaterialEx(tp)
		mg1:Remove(Card.IsLocation,nil,LOCATION_HAND)
		local mg2=Duel.GetMatchingGroup(s.mfilter,tp,0,LOCATION_MZONE,nil,e)
		mg1:Merge(mg2)
		aux.RCheckAdditional=s.rcheck(c)
		if Duel.IsExistingMatchingCard(aux.RitualUltimateFilter,tp,LOCATION_HAND+LOCATION_DECK,0,1,nil,s.filter,e,tp,mg1,nil,s.GetLevel,"Equal")
			and Duel.SelectYesNo(tp,aux.Stringid(id,1)) then
			Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SPSUMMON)
			local tg=Duel.SelectMatchingCard(tp,aux.RitualUltimateFilter,tp,LOCATION_HAND+LOCATION_DECK,0,1,1,nil,s.filter,e,tp,mg1,nil,s.GetLevel,"Equal")
			local tc=tg:GetFirst()
			if tc then
				local mg=mg1:Filter(Card.IsCanBeRitualMaterial,tc,tc)
				if tc.mat_filter then
					mg=mg:Filter(tc.mat_filter,tc,tp)
				else
					mg:RemoveCard(tc)
				end
				Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_RELEASE) 
				aux.GCheckAdditional=aux.RitualCheckAdditional(tc,s.GetLevel(tc),"Equal")
				--local g1=mg:Select(tp,1,1,nil)
				local mat=mg:SelectSubGroup(tp,aux.RitualCheck,false,1,s.GetLevel(tc),tp,tc,s.GetLevel(tc),"Equal")
				aux.GCheckAdditional=nil
				Card.GetRitualLevel=s.originGetRitualLevel
				if not mat or mat:GetCount()==0 then return end
				tc:SetMaterial(mat)
				Duel.ReleaseRitualMaterial(mat)
				Duel.BreakEffect()
				Duel.SpecialSummon(tc,SUMMON_TYPE_RITUAL,tp,tp,false,true,POS_FACEUP)
				tc:CompleteProcedure()
			end
		end
		aux.RCheckAdditional=nil
		Card.GetRitualLevel=s.originGetRitualLevel
	end
end
