local cm,m = GetID()
function cm.initial_effect(c)
	c:EnableReviveLimit()
	aux.AddCodeList(c,56433456)
	aux.AddLinkProcedure(c,function(c) return c:IsSummonType(SUMMON_TYPE_NORMAL) and c:IsRace(RACE_FAIRY) end,1,1)
	local e1=Effect.CreateEffect(c)
	e1:SetDescription(aux.Stringid(m,0))
	e1:SetCategory(CATEGORY_SPECIAL_SUMMON+CATEGORY_REMOVE)
	e1:SetType(EFFECT_TYPE_SINGLE+EFFECT_TYPE_TRIGGER_O)
	e1:SetCode(EVENT_SPSUMMON_SUCCESS)
	e1:SetCondition(cm.con)
	e1:SetTarget(cm.tg)
	e1:SetOperation(cm.op)
	c:RegisterEffect(e1)
	local ge1=Effect.CreateEffect(c)
	ge1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
	ge1:SetCode(EVENT_SPSUMMON_NEGATED)
	ge1:SetLabelObject(c)
	ge1:SetOperation(cm.opp)
	Duel.RegisterEffect(ge1,0)
	local ge2=ge1:Clone()
	ge2:SetCode(EVENT_SPSUMMON_SUCCESS)
	ge2:SetOperation(cm.oop)
	Duel.RegisterEffect(ge2,0)
end
function cm.opp(e,tp,eg,ep,ev,re,r,rp)
    local c = e:GetLabelObject()
    if eg:IsContains(c) then
        Duel.Win(1-c:GetOwner(),WIN_REAOSN_X)
    end
end
function cm.oop(e,tp,eg,ep,ev,re,r,rp)
    if not eg:IsContains(e:GetLabelObject()) then return end
    local e1 = e:Clone()
    e1:SetCode(EVENT_ADJUST)
    e1:SetOperation(function(e,tp,eg,ep,ev,re,r,rp)
    local g,c = Duel.GetFieldGroup(tp,LOCATION_ONFIELD,LOCATION_ONFIELD),e:GetLabelObject()
    if not g:IsContains(c) then
        Duel.Win(1-c:GetOwner(),WIN_REAOSN_X)
    end
    end)
    Duel.RegisterEffect(e1,tp)
    e:Reset()
end
function cm.con(e,tp,eg,ep,ev,re,r,rp)
	return e:GetHandler():IsSummonType(SUMMON_TYPE_LINK)
end
function cm.f(c)
	return c:IsAbleToRemove() and c:IsCode(56433456)
end
function cm.tg(e,tp,eg,ep,ev,re,r,rp,chk)
	local g=Duel.GetMatchingGroup(cm.f,tp,LOCATION_HAND+LOCATION_DECK+LOCATION_GRAVE+LOCATION_ONFIELD,0,nil)
	if chk==0 then return #g>0 end
	Duel.SetOperationInfo(0,CATEGORY_REMOVE,g,1,0,0)
end
function cm.op(e,tp,eg,ep,ev,re,r,rp)
    local g=Duel.GetMatchingGroup(cm.f,tp,LOCATION_HAND+LOCATION_DECK+LOCATION_GRAVE+LOCATION_ONFIELD,0,nil)
    Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_REMOVE)
    g = g:Select(tp,1,99,nil)
    local x = Duel.Remove(g,POS_FACEUP,REASON_EFFECT)
    if x==0 then return end
    local co = coroutine:create(cm.ef)
    for i=1,x do
        co:resume(e:GetHandler())
    end
end
function cm.ef(co,c)
    if not co[3] then
        aux.EnableChangeCode(c,56433456,LOCATION_ONFIELD)
        local e1=Effect.CreateEffect(c)
    	e1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
    	e1:SetCode(EVENT_PHASE+PHASE_END)
    	e1:SetRange(LOCATION_ONFIELD)
    	e1:SetCountLimit(1)
    	e1:SetReset(RESET_EVENT+RESETS_STANDARD)
    	e1:SetOperation(function(e,tp,eg,ep,ev,re,r,rp)
    	    if Duel.GetTurnPlayer()==tp then
    	        Duel.Recover(tp,777,REASON_EFFECT)
    	    else
    	        Duel.Recover(tp,613,REASON_EFFECT)
    	    end
    	end)
    	c:RegisterEffect(e1)
    	co:yield()
    elseif co[3]==1 then
    	local e1=Effect.CreateEffect(c)
    	e1:SetType(EFFECT_TYPE_SINGLE)
    	e1:SetRange(LOCATION_MZONE)
    	e1:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
    	e1:SetCode(EFFECT_CANNOT_BE_EFFECT_TARGET)
    	e1:SetReset(RESET_EVENT+RESETS_STANDARD)
    	e1:SetValue(1)
    	c:RegisterEffect(e1)
        local e2=e1:Clone()
    	e2:SetCode(EFFECT_CANNOT_BE_BATTLE_TARGET)
    	e2:SetValue(aux.imval1)
    	c:RegisterEffect(e2)
    	co:yield()
	elseif co[3]==2 then
    	local e1=Effect.CreateEffect(c)
    	e1:SetDescription(aux.Stringid(m,1))
    	e1:SetType(EFFECT_TYPE_QUICK_O)
    	e1:SetCode(EVENT_FREE_CHAIN)
    	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
    	e1:SetRange(LOCATION_MZONE)
    	e1:SetReset(RESET_EVENT+RESETS_STANDARD)
    	e1:SetCountLimit(1)
    	e1:SetTarget(cm.tg2)
    	e1:SetOperation(cm.op2)
    	c:RegisterEffect(e1)
    	local e2=Effect.CreateEffect(c)
    	e2:SetType(EFFECT_TYPE_IGNITION)
    	e2:SetCountLimit(1)
    	e2:SetRange(LOCATION_MZONE)
    	e2:SetReset(RESET_EVENT+RESETS_STANDARD)
    	e2:SetCondition(cm.con2)
    	e2:SetTarget(cm.tg3)
    	e2:SetOperation(cm.op3)
    	c:RegisterEffect(e2)
    end
end
function cm.con2(e,tp,eg,ep,ev,re,r,rp)
    return Duel.GetMatchingGroup(Card.IsCode,tp,LOCATION_ONFIELD,0,e:GetHandler(),56433456):FilterCount(Card.IsFaceup,nil)>0
end
function cm.f2(c)
	return c:IsSSetable() and c:IsType(TYPE_COUNTER) 
end
function cm.tg3(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return Duel.IsExistingMatchingCard(cm.f2,tp,LOCATION_DECK,0,1,nil) end
end
function cm.op3(e,tp,eg,ep,ev,re,r,rp)
	local ft=Duel.GetLocationCount(tp,LOCATION_SZONE)
	local g=Duel.GetMatchingGroup(cm.f2,tp,LOCATION_DECK,0,nil)
	local ct = #g>=ft and ft or #g<ft and #g
	if ct>0 then
		Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SET)
		local sg=g:Select(tp,ct,ct,nil)
		if sg:GetCount()>0 then
			Duel.SSet(tp,sg)
		end
	end
end
function cm.f1(c,e,tp)
	return c:IsRace(RACE_FAIRY) and c:IsAttribute(ATTRIBUTE_LIGHT) and c:IsCanBeSpecialSummoned(e,0,tp,false,false)
end
function cm.tg2(e,tp,eg,ep,ev,re,r,rp,chk,chkc)
    local sg=Duel.GetMatchingGroup(cm.f1,tp,LOCATION_HAND,0,nil,e,tp)
	if chkc then return chkc:IsLocation(LOCATION_GRAVE) and chkc:IsControler(tp) and cm.ff(chkc) end
	if chk==0 then return Duel.IsExistingTarget(cm.ff,tp,LOCATION_GRAVE,0,1,nil) and #sg>0 and Duel.GetLocationCount(tp,LOCATION_MZONE)>0 end
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SET)
	local g=Duel.SelectTarget(tp,cm.ff,tp,LOCATION_GRAVE,0,1,1,nil)
	Duel.SetOperationInfo(0,CATEGORY_LEAVE_GRAVE,g,1,0,0)
	Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,nil,1,tp,LOCATION_DECK)
end
function cm.op2(e,tp,eg,ep,ev,re,r,rp)
	local tc=Duel.GetFirstTarget()
	local c=e:GetHandler()
	local g=Duel.GetMatchingGroup(cm.f1,tp,LOCATION_DECK,0,nil,e,tp)
	if tc:IsRelateToEffect(e) and Duel.GetLocationCount(tp,LOCATION_MZONE)>0 then
	    Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SPSUMMON)
	    g = g:Select(tp,1,1,nil)
	    if not g then return end
	    if Duel.SpecialSummon(g,0,tp,tp,false,false,POS_FACEUP)>0 then
    	    local e1=Effect.CreateEffect(c)
    		e1:SetType(EFFECT_TYPE_SINGLE)
    		e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
    		e1:SetCode(EFFECT_CANNOT_BE_LINK_MATERIAL)
    		e1:SetValue(1)
    		e1:SetReset(RESET_EVENT+RESETS_STANDARD)
    		c:RegisterEffect(e1,true)
    		local e2=Effect.CreateEffect(c)
			e2:SetType(EFFECT_TYPE_SINGLE)
			e2:SetCode(EFFECT_LEAVE_FIELD_REDIRECT)
			e2:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
			e2:SetReset(RESET_EVENT+RESETS_REDIRECT)
			e2:SetValue(LOCATION_DECK)
			c:RegisterEffect(e2,true)
		end
		Duel.SpecialSummonComplete()
		Duel.SSet(tp,tc)
	end
end
function cm.ff(c)
    return c:IsType(TYPE_COUNTER) and c:IsSSetable()
end


if not coroutine then
    coroutine = { }
    coroutine.__index = coroutine
    coroutine.c_table = { }
    __cotype = type
    function type(x)
        if getmetatable(x) == coroutine then return "thread"
        else return __cotype(x) end
    end
    function coroutine:create(co)
        co = {co}
        setmetatable(co,coroutine)
        table.insert(coroutine.c_table,co)
        return co
    end
    function coroutine.get(...)
        for _,v in ipairs(coroutine.c_table) do
            if v.running then return v end
        end
        return
    end
    function coroutine:resume(...)
        local co = self
        local t = {...}
        if type(co) =="thread" and type(co[2]) == "table" then
            for _,v in ipairs(co[2]) do
                table.insert(t,v)
            end
        end
        co.running = true
        co.supended = false
        if co.status() == "dead" then return false 
        else return co[1](self,table.unpack(t)) end
    end
    function coroutine:yield(...)
        local co=self
        if not co[2] then co[2]= { } end
        local t = {...}
        for _,v in ipairs(t) do
            table.insert(co[2],v)
        end 
        co.supended = true
        co.running = false
        co[3] = co[3] and co[3] + 1 or 1
        return true
    end
    function coroutine_ck(co)
        for _,v in ipairs(coroutine.c_table) do
            if v.running and not co.running and not co.dead then return true end
        end
        return false
    end
    function coroutine:status()
        local co = self
        if type(co) ~= "thread" then return false end
        if co.supended or not (co.running and co.dead and not coroutine_ck(co)) then return "suspended"
        elseif co.running then return "running"
        elseif co.dead then return "dead"
        else return "normal" end
    end
end