-- 希望的幻色
local s,id,o=GetID()
function s.initial_effect(c)
    -- xyz summon
	local e1=Effect.CreateEffect(c)
	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
	e1:SetType(EFFECT_TYPE_ACTIVATE)
	e1:SetCode(EVENT_FREE_CHAIN)
    e1:SetHintTiming(0,TIMING_END_PHASE)
    e1:SetCountLimit(1,id)
	e1:SetOperation(s.op)
	e1:SetCost(s.cost)
    c:RegisterEffect(e1)
    -- set from grave
	local e2=Effect.CreateEffect(c)
	e2:SetDescription(aux.Stringid(id,0))
	e2:SetCategory(CATEGORY_DRAW)
	e2:SetType(EFFECT_TYPE_QUICK_O)
	e2:SetRange(LOCATION_GRAVE)
	e2:SetCode(EVENT_SPSUMMON_SUCCESS)
    e2:SetCountLimit(1,id)
	e2:SetCondition(s.setcon)
    e2:SetTarget(s.settg)
	e2:SetOperation(s.setop)
	c:RegisterEffect(e2)
    -- no draw
	if not s.gf then
		s.gf=true
		local ge1=Effect.CreateEffect(c)
		ge1:SetType(EFFECT_TYPE_CONTINUOUS+EFFECT_TYPE_FIELD)
		ge1:SetCode(EVENT_DRAW)
		ge1:SetOperation(s.regop)
		Duel.RegisterEffect(ge1,0)
	end
end

function s.spfilter(c,e,tp,rank)
    if rank~=nil and c:GetRank()~=rank then return false end
	return c:IsType(TYPE_XYZ)
		and c:IsCanBeSpecialSummoned(e,SUMMON_TYPE_XYZ,tp,false,false) 
        and Duel.GetLocationCountFromEx(tp,tp,nil,c,0x60)>0
end
-- function s.target(e,tp,eg,ep,ev,re,r,rp,chk,chkc)
-- 	if chk==0 then return aux.MustMaterialCheck(nil,tp,EFFECT_MUST_BE_XMATERIAL)
-- 		and Duel.IsExistingMatchingCard(s.spfilter,tp,LOCATION_EXTRA,0,1,nil,e,tp,nil) end
-- 	Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,nil,1,tp,LOCATION_EXTRA)
-- end
function s.cost(e,tp,eg,ep,ev,re,r,rp,chk)
    local g=Duel.GetMatchingGroup(s.spfilter,tp,LOCATION_EXTRA,nil,nil,e,tp)
    local t={}
    local tc=g:GetFirst()
	while tc do
        t[tc:GetRank()]=1
        tc=g:GetNext()
    end
    local ls={}
    for level,one in pairs(t) do
        if Duel.CheckLPCost(tp,level*100) then
            table.insert(ls,level)
        end
    end
    if chk==0 then return #ls>0 end
    local rank=Duel.AnnounceNumber(tp,table.unpack(ls))
    Duel.PayLPCost(tp, rank*100)
    e:SetLabel(rank)
end
function s.op(e,tp,eg,ep,ev,re,r,rp)
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SPSUMMON)
    local rank=e:GetLabel()
    local g=Duel.SelectMatchingCard(tp,s.spfilter,tp,LOCATION_EXTRA,0,1,1,nil,e,tp,rank)
	local tc=g:GetFirst()
	if tc then
		if Duel.SpecialSummon(tc,SUMMON_TYPE_XYZ,tp,tp,false,false,POS_FACEUP,0x60)~=0 then
            local e1=Effect.CreateEffect(tc)
            e1:SetType(EFFECT_TYPE_SINGLE)
            e1:SetCode(EFFECT_CANNOT_ATTACK)
            e1:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE)
            e1:SetReset(RESET_EVENT+RESETS_STANDARD+RESET_PHASE+PHASE_END+RESET_SELF_TURN)
            tc:RegisterEffect(e1,true)
            tc:RegisterFlagEffect(id,RESET_EVENT+RESETS_STANDARD+RESET_PHASE+PHASE_END+RESET_SELF_TURN,0,1)
            tc:CompleteProcedure()
            local e2=Effect.CreateEffect(tc)
            e2:SetType(EFFECT_TYPE_SINGLE)
            e2:SetCode(EFFECT_DISABLE)
            e2:SetReset(RESET_EVENT+RESETS_STANDARD+RESET_PHASE+PHASE_END+RESET_SELF_TURN)
            tc:RegisterEffect(e2)
            local e3=Effect.CreateEffect(tc)
            e3:SetType(EFFECT_TYPE_SINGLE)
            e3:SetCode(EFFECT_DISABLE_EFFECT)
            e3:SetValue(RESET_TURN_SET)
            e3:SetReset(RESET_EVENT+RESETS_STANDARD+RESET_PHASE+PHASE_END+RESET_SELF_TURN)
            tc:RegisterEffect(e3)
            local e4=Effect.CreateEffect(tc)
            e4:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
            e4:SetCode(EVENT_PHASE+PHASE_END)
            e4:SetCountLimit(1)
            e4:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE)
            e4:SetLabelObject(tc)
            e4:SetCondition(s.retcon)
            e4:SetOperation(s.retop)
            Duel.RegisterEffect(e4,tp)
        end
	end
end
function s.retcon(e,tp,eg,ep,ev,re,r,rp)
    if tp~=Duel.GetTurnPlayer() then return false end
	local tc=e:GetLabelObject()
	if tc:GetFlagEffect(id)~=0 then
		return true
	else
		e:Reset()
		return false
	end
end
function s.retop(e,tp,eg,ep,ev,re,r,rp)
	local tc=e:GetLabelObject()
    Duel.SendtoDeck(tc,nil,SEQ_DECKSHUFFLE,REASON_TEMPORARY)
end

function s.setcon(e,tp,eg,ep,ev,re,r,rp)
	local xyz=eg:GetCount()==1 and eg:GetFirst():IsSummonType(SUMMON_TYPE_XYZ) and eg:GetFirst():IsControler(tp)
    local nodraw=Duel.GetFlagEffect(tp,id)~=1
    return xyz and nodraw
end
function s.settg(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return e:GetHandler():IsSSetable() end
	Duel.SetOperationInfo(0,CATEGORY_LEAVE_GRAVE,e:GetHandler(),1,0,0)
end
function s.setop(e,tp,eg,ep,ev,re,r,rp)
    if Duel.SSet(tp,e:GetHandler())~=0 then
        Duel.Draw(tp,1,REASON_EFFECT)
    end
end
function s.regop(e,tp,eg,ep,ev,re,r,rp)
	Duel.RegisterFlagEffect(ep,id,RESET_PHASE+PHASE_END,0,1)
end
