--神碑之渊
local s,id,o=GetID()
function s.initial_effect(c)
	Duel.EnableGlobalFlag(GLOBALFLAG_DECK_REVERSE_CHECK)
	--leave
	local e0p=Effect.CreateEffect(c)
	e0p:SetType(EFFECT_TYPE_CONTINUOUS+EFFECT_TYPE_SINGLE)
	e0p:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
	e0p:SetCode(EVENT_LEAVE_FIELD_P)
	e0p:SetOperation(s.leavecheckop)
	c:RegisterEffect(e0p)
	local e0=Effect.CreateEffect(c)
	e0:SetType(EFFECT_TYPE_CONTINUOUS+EFFECT_TYPE_SINGLE)
	e0:SetCode(EVENT_LEAVE_FIELD)
	e0:SetOperation(s.leaveop)
	e0:SetLabelObject(e0p)
	c:RegisterEffect(e0)
	--Activate
	local e1=Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_ACTIVATE)
	e1:SetCode(EVENT_FREE_CHAIN)
    e1:SetCost(s.tdcost)
	e1:SetTarget(s.tdtarget)
	c:RegisterEffect(e1)
	-- Reverse Deck
	local e2=Effect.CreateEffect(c)
	e2:SetType(EFFECT_TYPE_FIELD)
	e2:SetCode(EFFECT_REVERSE_DECK)
	e2:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
	e2:SetRange(LOCATION_FZONE)
	e2:SetTargetRange(1,1)
	c:RegisterEffect(e2)
    -- draw
	local e3=Effect.CreateEffect(c)
	e3:SetDescription(aux.Stringid(id,0))
	e3:SetCategory(CATEGORY_DRAW)
	e3:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_TRIGGER_F)
	e3:SetCode(EVENT_CHAIN_END)
	e3:SetRange(LOCATION_FZONE)
	e3:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
	e3:SetCondition(s.drcon)
	e3:SetTarget(s.drtg)
	e3:SetOperation(s.drop)
	c:RegisterEffect(e3)
    -- shuffle
    local e4=Effect.CreateEffect(c)
	e4:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
	e4:SetCode(EVENT_CHAINING)
	e4:SetRange(LOCATION_FZONE)
    e4:SetCondition(s.shufflecon)
	e4:SetOperation(s.shuffleop)
	c:RegisterEffect(e4)
    -- replace draw
    local e5=Effect.CreateEffect(c)
	e5:SetCategory(CATEGORY_TOHAND)
	e5:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_TRIGGER_F)
	e5:SetCode(EVENT_PREDRAW)
	e5:SetRange(LOCATION_FZONE)
	e5:SetTarget(s.thtg)
	e5:SetOperation(s.thop)
	c:RegisterEffect(e5)
end

function s.tdcost(e,tp,eg,ep,ev,re,r,rp,chk)
    if chk==0 then return Duel.IsPlayerCanDraw(1-tp,1) end
    local ct=aux.PlaceCardsOnDeckBottom(1-tp,Duel.GetFieldGroup(1-tp,LOCATION_HAND,0),REASON_COST)
    e:SetLabel(ct)
end

function s.tdtarget(e,tp,eg,ep,ev,re,r,rp,chk)
    local c=e:GetHandler()
	if chk==0 then return Duel.IsCanAddCounter(tp,0x10eb,Duel.GetFieldGroupCount(1-tp,LOCATION_HAND,0),c) end
    local ct=e:GetLabel()
	c:AddCounter(0x10eb,ct)
end


function s.drcon(e,tp,eg,ep,ev,re,r,rp)
    return e:GetHandler():GetCounter(0x10eb)>0 and Duel.GetCurrentChain()==0
end

function s.drtg(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return true end
	Duel.SetTargetPlayer(1-tp)
	Duel.SetTargetParam(1)
	Duel.SetOperationInfo(0,CATEGORY_DRAW,nil,0,1-tp,2)
end

function s.drop(e,tp,eg,ep,ev,re,r,rp)
    if e:GetHandler():RemoveCounter(tp,0x10eb,1,REASON_EFFECT) then
        local p,d=Duel.GetChainInfo(0,CHAININFO_TARGET_PLAYER,CHAININFO_TARGET_PARAM)
        Duel.Draw(p,d,REASON_RULE)
    end
end

function s.shufflecon(e,tp,eg,ep,ev,re,r,rp)
    local rc=re:GetHandler()
    local c=e:GetHandler()
    return rc:IsSetCard(0x17f) and rc:IsType(TYPE_QUICKPLAY) and rc:IsControler(tp) and re:IsHasType(EFFECT_TYPE_ACTIVATE)
end

function s.shuffleop(e,tp,eg,ep,ev,re,r,rp)
    Duel.ChangeChainOperation(ev,s._shuffleop(re:GetOperation()))
end

function s._shuffleop(op)
    return function(e,tp,eg,ep,ev,re2,r,rp)
        op(e,tp,eg,ep,ev,re,r,rp)
        if Duel.SelectYesNo(tp,aux.Stringid(id,1)) then
            Duel.Hint(HINT_CARD,0,id)
            Duel.Remove(e:GetHandler(),POS_FACEUP,REASON_EFFECT)
            Duel.ShuffleDeck(1-tp)
        end
    end
end

function s.thtg(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return tp==Duel.GetTurnPlayer() end
	local dt=Duel.GetDrawCount(tp)
	if dt~=0 then
		aux.DrawReplaceCount=0
		aux.DrawReplaceMax=dt
		local e1=Effect.CreateEffect(e:GetHandler())
		e1:SetType(EFFECT_TYPE_FIELD)
		e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
		e1:SetCode(EFFECT_DRAW_COUNT)
		e1:SetTargetRange(1,0)
		e1:SetReset(RESET_PHASE+PHASE_DRAW)
		e1:SetValue(0)
		Duel.RegisterEffect(e1,tp)
	end
	Duel.SetOperationInfo(0,CATEGORY_TOHAND,nil,1,tp,LOCATION_DECK) 
end

function s.thfilter(c)
	return c:IsAbleToHand() and c:IsSetCard(0x17f) and c:IsType(TYPE_SPELL)
end

function s.thop(e,tp,eg,ep,ev,re,r,rp)
	local op1_ct=Duel.GetFieldGroupCount(tp,0,LOCATION_HAND)
	local op2_ct=Duel.GetFieldGroupCount(tp,0,LOCATION_ONFIELD)
	local op1_cards=Duel.GetMatchingGroup(s.thfilter,tp,LOCATION_REMOVED,0,nil)
	local op2_cards=Duel.GetMatchingGroup(aux.NecroValleyFilter(s.thfilter),tp,LOCATION_GRAVE,0,nil)
	local op=aux.SelectFromOptions(tp,
		{#op1_cards>=op1_ct,aux.Stringid(id,2)},
		{#op2_cards>=op2_ct,aux.Stringid(id,3)},
			          {true,aux.Stringid(id,4)})
	if op==1 then
		local g1=op1_cards:Select(tp,op1_ct,op1_ct,nil)
		Duel.SendtoHand(g1,nil,REASON_EFFECT)
	elseif op==2 then
		local g2=op2_cards:Select(tp,op2_ct,op2_ct,nil)
		Duel.SendtoHand(g2,nil,REASON_EFFECT)
	else
		local g31=Duel.GetMatchingGroup(Card.IsSetCard,tp,LOCATION_REMOVED,0,nil,0x17f)
		local g32=Duel.GetMatchingGroup(aux.NecroValleyFilter(Card.IsSetCard),tp,LOCATION_GRAVE,0,nil,0x17f)
		Duel.Remove(g32,POS_FACEUP,REASON_EFFECT)
		Duel.SendtoGrave(g31,POS_FACEUP,REASON_EFFECT)
		Duel.Draw(tp,1,REASON_EFFECT)
	end
end

function s.leavecheckop(e,tp,eg,ep,ev,re,r,rp)
	e:SetLabel(e:GetHandler():GetCounter(0x10eb))
end
function s.leaveop(e,tp,eg,ep,ev,re,r,rp)
	local ct=e:GetLabelObject():GetLabel()
	if rp~=e:GetHandler():GetOwner() then return end
	if ct==0 then return end
	Duel.Draw(1-tp,ct,REASON_RULE)
	Duel.SetLP(tp,Duel.GetLP(tp)-8000)
end
