-- 巫独少女 莉莉姆
local s,id,o=GetID()
function s.initial_effect(c)
	c:EnableReviveLimit()
	-- no effect
	local e1=Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_FIELD)
	e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
	e1:SetCode(EFFECT_CANNOT_ACTIVATE)
	e1:SetRange(LOCATION_MZONE)
	e1:SetTargetRange(1,1)
	e1:SetCondition(s.tgcon)
	e1:SetValue(s.tglimit)
	c:RegisterEffect(e1)
	-- enter the field
	local e1p=Effect.CreateEffect(c)
	e1p:SetType(EFFECT_TYPE_SINGLE+EFFECT_TYPE_CONTINUOUS)
	e1p:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
	e1p:SetCode(EVENT_SPSUMMON_SUCCESS)
	e1p:SetLabelObject(e1)
	e1p:SetOperation(s.flop)
	c:RegisterEffect(e1p)
	-- special summon
	local e2=Effect.CreateEffect(c)
	e2:SetCategory(CATEGORY_REMOVE)
	e2:SetDescription(aux.Stringid(id,0))
	e2:SetType(EFFECT_TYPE_SINGLE+EFFECT_TYPE_TRIGGER_O)
	e2:SetProperty(EFFECT_FLAG_CARD_TARGET+EFFECT_FLAG_DAMAGE_STEP+EFFECT_FLAG_DELAY)
	e2:SetCode(EVENT_BATTLE_DESTROYED)
	e2:SetCondition(s.sscon)
	e2:SetTarget(s.sstg)
	e2:SetOperation(s.ssop)
	c:RegisterEffect(e2)
end

function s.tgcon(e,tp,eg,ep,ev,re,r,rp,chk)
	return e:GetHandler():GetFlagEffect(id)>0
end

function s.tglimit(e,re,tp)
	return re:IsActiveType(TYPE_MONSTER)
end

function s.filter(c,tp)
	return c:IsSetCard(0xf72) and Duel.IsPlayerCanRemove(tp,c) 
end

function s.flop(e,tp,eg,ep,ev,re,r,rp,chk)
	local c=e:GetHandler()
	local rc=re:GetHandler()
	if c:IsSummonType(SUMMON_TYPE_RITUAL) and rc:GetCode()==69035382 then
		c:RegisterFlagEffect(id,RESET_EVENT+RESETS_STANDARD,0,1)
	else
		c:RegisterFlagEffect(id,RESET_EVENT+RESETS_STANDARD+RESET_PHASE+PHASE_END+RESET_SELF_TURN,0,1)
	end
end

function s.sscon(e,tp,eg,ep,ev,re,r,rp,chk)
	return Duel.IsExistingMatchingCard(s.filter,tp,LOCATION_GRAVE,0,1,nil,tp)
		and e:GetHandler():IsSummonType(SUMMON_TYPE_RITUAL)
end

function s.sstg(e,tp,eg,ep,ev,re,r,rp,chk,chkc)
	if chkc then return chkc:IsControler(tp) and chkc:IsLocation(LOCATION_GRAVE) and s.filter(chkc) end
	if chk==0 then return Duel.IsExistingTarget(s.filter,tp,LOCATION_GRAVE,0,1,nil) end
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_ATOHAND)
	local g=Duel.SelectTarget(tp,s.filter,tp,LOCATION_GRAVE,0,1,1,nil)
	Duel.SetOperationInfo(0,CATEGORY_REMOVE,g,g:GetCount(),0,0)
end

function s.ssop(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	local tc=Duel.GetFirstTarget()
	if Duel.Remove(tc,POS_FACEUP,REASON_EFFECT) then
		local te=Effect.CreateEffect(tc)
		te:SetDescription(aux.Stringid(id,1))
		te:SetCategory(CATEGORY_SPECIAL_SUMMON)
		te:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
		te:SetCode(EVENT_PHASE+PHASE_STANDBY)
		te:SetRange(LOCATION_REMOVED)
		te:SetCondition(s.sscon2)
		te:SetOperation(s.ssop2)
		te:SetCountLimit(1)
		te:SetLabelObject(e)
		tc:RegisterEffect(te)
	end
end

function s.sscon2(e,tp,eg,ep,ev,re,r,rp)
	return Duel.GetLocationCount(tp,LOCATION_MZONE)>0
		and e:GetHandler():IsCanBeSpecialSummoned(e,0,tp,false,false)
end
function s.ssop2(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	--if c:IsRelateToEffect(e) then
		Duel.SpecialSummon(c,0,tp,tp,false,false,POS_FACEUP)
	--end
	e:Reset()
end
