--未来来
local s,id,o=GetID()
function s.initial_effect(c)
	c:EnableCounterPermit(0x10ea)
	--activate
	local e0=Effect.CreateEffect(c)
	e0:SetCategory(CATEGORY_COUNTER)
	e0:SetType(EFFECT_TYPE_ACTIVATE)
	e0:SetCode(EVENT_FREE_CHAIN)
    e0:SetCost(s.cost)
	e0:SetTarget(s.target)
	c:RegisterEffect(e0)
	--indes
	local e1=Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_FIELD)
	e1:SetCode(EFFECT_INDESTRUCTABLE_EFFECT)
	e1:SetRange(LOCATION_SZONE)
	e1:SetTargetRange(LOCATION_ONFIELD,0)
    e1:SetCondition(s.indescon)
    e1:SetTarget(s.indestg)
	e1:SetValue(1)
	c:RegisterEffect(e1)
	-- --disable spsummon
	local e1p=Effect.CreateEffect(c)
	e1p:SetType(EFFECT_TYPE_FIELD)
	e1p:SetRange(LOCATION_SZONE)
	e1p:SetCode(EFFECT_CANNOT_SPECIAL_SUMMON)
	e1p:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
	e1p:SetTargetRange(1,0)
    e1p:SetCondition(s.indescon)
	e1p:SetTarget(s.sumlimit)
	c:RegisterEffect(e1p)
	--scry
	local e2=Effect.CreateEffect(c)
	e2:SetDescription(aux.Stringid(id,0))
	e2:SetType(EFFECT_TYPE_QUICK_F)
	e2:SetCode(EVENT_CHAINING)
	e2:SetRange(LOCATION_SZONE)
	e2:SetCondition(s.scrycon)
	e2:SetOperation(s.scryop)
	c:RegisterEffect(e2)
    --wakeup (return card to deck top)
    local e3=Effect.CreateEffect(c)
    e3:SetDescription(aux.Stringid(id,1))
	e3:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_TRIGGER_F)
	e3:SetCategory(CATEGORY_SPECIAL_SUMMON)
	e3:SetCode(EVENT_SPSUMMON_SUCCESS)
	e3:SetRange(LOCATION_SZONE)
	e3:SetCondition(s.wakeupcon)
	e3:SetOperation(s.wakeupop)
	c:RegisterEffect(e3)
    -- reset the world
	local e4=Effect.CreateEffect(c)
	e4:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_TRIGGER_F)
	e4:SetCategory(CATEGORY_REMOVE)
	e4:SetDescription(aux.Stringid(id,4))
	e4:SetCode(EVENT_PHASE+PHASE_END)
	e4:SetRange(LOCATION_SZONE)
	e4:SetCountLimit(1)
	e4:SetCondition(s.resetcon)
	e4:SetTarget(s.resettg)
	e4:SetOperation(s.resetop)
	c:RegisterEffect(e4)
end
function s.filter(c)
    return Card.IsAbleToRemoveAsCost(c) and c:IsSetCard(0xe6)
end
function s.cost(e,tp,eg,ep,ev,re,r,rp,chk)
    if chk==0 then return Duel.IsExistingMatchingCard(s.filter,tp,LOCATION_MZONE,0,1,nil) end
	local g=Duel.GetMatchingGroup(s.filter,tp,LOCATION_MZONE,0,nil)
	Duel.Remove(g,POS_FACEUP,REASON_COST)
    local tg=Duel.GetOperatedGroup()
    tg:KeepAlive()
    e:SetLabelObject(tg)
end
function s.target(e,tp,eg,ep,ev,re,r,rp,chk)
	local c=e:GetHandler()
	if chk==0 then return Duel.IsCanAddCounter(tp,0x10ea,7,c) end
	c:AddCounter(0x10ea,3)
end

function s.indescon(e,tp,eg,ep,ev,re,r,rp)
    local c=e:GetHandler()
    local ct=c:GetCounter(0x10ea)
    return ct>0
end
function s.indestg(e,c)
    local cc=e:GetHandler()
	return (c:IsFaceup() and c:IsSetCard(0xe6)) or c==cc
end
function s.sumlimit(e,c)
	return not (c:IsSetCard(0xe6)) and c:IsLocation(LOCATION_EXTRA)
end

function s.scrycon(e,tp,eg,ep,ev,re,r,rp)
    local c=e:GetHandler()
    local ct=c:GetCounter(0x10ea)
    local rc=re:GetHandler()
    return ct>0 and Duel.GetFieldGroupCount(tp,LOCATION_DECK,0)>=ct and 
        re:IsActiveType(TYPE_MONSTER) and rc:IsSetCard(0xe6)
        -- and not e:GetHandler():IsStatus(STATUS_CHAINING)
end
function s.scryop(e,tp,eg,ep,ev,re,r,rp)
    local c=e:GetHandler()
    local ct=c:GetCounter(0x10ea)
    if Duel.GetFieldGroupCount(tp,LOCATION_DECK,0)<ct then return end
    s.scry(tp,ct)
end

function s.scry(tp,ct)
	Duel.ConfirmDecktop(tp,ct)
    local g=Duel.GetDecktopGroup(tp,ct)
    local dg=g:Select(tp,0,#g,nil)
    local ug=g-dg
    local dc=dg:GetFirst()
    while dc do
        Duel.MoveSequence(dc,SEQ_DECKTOP)
        dc=dg:GetNext()
    end
    Duel.SortDecktop(tp,tp,#dg)
    for i=1,#dg do
        local mg=Duel.GetDecktopGroup(tp,1)
        Duel.MoveSequence(mg:GetFirst(),SEQ_DECKBOTTOM)
    end
    Duel.SortDecktop(tp,tp,#ug)
end

function s.wakeupfilter(c)
    return c:IsLocation(LOCATION_DECK)
end
function s.wakeupcon(e,tp,eg,ep,ev,re,r,rp)
    local c=e:GetHandler()
    local ct=c:GetCounter(0x10ea)
    local rc=re:GetHandler()
    return ct>0 and rc:IsSummonType(SUMMON_TYPE_SYNCHRO) and rc:IsSetCard(0xe6) 
end
function s.wakeupop(e,tp,eg,ep,ev,re,r,rp)
    local c=e:GetHandler()
    local ct=c:GetCounter(0x10ea)
    if ct>0 then
        c:RemoveCounter(tp,0x10ea,1,REASON_EFFECT)
        if Duel.SelectYesNo(tp,aux.Stringid(id,1)) and Duel.SendtoGrave(c,REASON_EFFECT)>0 then
            local g=Duel.GetMatchingGroup(Card.IsSetCard,tp,LOCATION_MZONE+LOCATION_GRAVE,0,nil,0xe6)
            Duel.SendtoDeck(g,nil,SEQ_DECKTOP,REASON_EFFECT)
            local tg=Duel.GetOperatedGroup():Filter(s.wakeupfilter,nil)
            if #tg>0 then
                s.scry(tp,#tg)
                local e0=c:GetActivateEffect()
                local tg2=e0:GetLabelObject()
                Duel.SpecialSummon(tg2,0,tp,tp,false,false,POS_FACEUP)
            end
        end
    end
end

function s.resetcon(e,tp,eg,ep,ev,re,r,rp)
	return tp==Duel.GetTurnPlayer()
end
function s.resettg(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return true end
	local c=e:GetHandler()
    local ct=c:GetCounter(0x10ea)
    if ct>0 then ct=1 else ct=0 end
    local loc=LOCATION_ONFIELD+LOCATION_HAND+LOCATION_GRAVE
    local g=Duel.GetMatchingGroup(aux.TRUE,tp,loc,loc*ct,nil)
    Duel.SetOperationInfo(0,CATEGORY_REMOVE,g,#g,0,0)
end
function s.resetop(e,tp,eg,ep,ev,re,r,rp)
    local c=e:GetHandler()
    local ct=c:GetCounter(0x10ea)
    if ct>0 then ct=0 else ct=1 end
    local loc=LOCATION_ONFIELD+LOCATION_HAND+LOCATION_GRAVE
    local g=Duel.GetMatchingGroup(aux.TRUE,tp,loc,loc*ct,nil)
    Duel.Remove(g,POS_FACEDOWN,REASON_EFFECT)
    
    Duel.SetLP(tp,8000)
    Duel.Draw(tp,5,REASON_EFFECT)
    
    if ct==1 then
       Duel.SetLP(1-tp,8000)
       Duel.Draw(1-tp,5,REASON_EFFECT)
    end
end

function s.discon(e,tp,eg,ep,ev,re,r,rp)
	return tp==Duel.GetTurnPlayer()
end
function s.distg(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return true end
	Duel.SetOperationInfo(0,CATEGORY_DECKDES,nil,0,tp,2)
end
function s.disop(e,tp,eg,ep,ev,re,r,rp)
	Duel.DiscardDeck(tp,2,REASON_EFFECT)
end
