name: Surprise
card_text: Activate only as Chain Link 2 or higher; apply the following effects, depending on the Chain Link number of this card, in sequence (skip over any that do not apply). You cannot activate this card if multiple cards/effects with the same name are in that Chain.
  ● 2+: Excavate the top cards of your Deck, equal to the Chain Link number of this card, then place them on the top of your Deck in any order.
  ● 3+: Send the top card of your Deck to the GY.
  ● 4+: Draw 1 card.
  You can only activate 1 "Surprise Chain" per turn.
lua: local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetCondition(s.condition)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetCurrentChain() > 0 and Duel.CheckChainUniqueness()
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	local cl = Duel.GetCurrentChain()
  	if chk == 0 then
  		return Duel.GetFieldGroupCount(tp, LOCATION_DECK, 0) >= cl and (cl < 3 or Duel.IsPlayerCanDiscardDeck(tp, 1)) and (cl < 4 or Duel.IsPlayerCanDraw(tp, 1))
  	end
  	local cat = 0
  	if cl >= 3 then
  		cat = cat | CATEGORY_TOGRAVE
  		Duel.SetOperationInfo(0, CATEGORY_TOGRAVE, nil, 1, tp, LOCATION_DECK)
  	end
  	if cl >= 4 then
  		cat = cat | CATEGORY_DRAW
  		Duel.SetOperationInfo(0, CATEGORY_DRAW, nil, 0, tp, 1)
  	end
  	e:SetCategory(cat)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local cl = Duel.GetCurrentChain()
  	if cl >= 2 then
  		Duel.ConfirmDecktop(tp, cl)
  		local g = Duel.GetDecktopGroup(tp, cl)
  		if g:GetCount() > 0 then
  			Duel.SortDecktop(tp, tp, g:GetCount())
  		end
  	end
  	if cl >= 3 then
  		Duel.BreakEffect()
  		Duel.DiscardDeck(tp, 1, REASON_EFFECT)
  	end
  	if cl >= 4 then
  		Duel.BreakEffect()
  		Duel.Draw(tp, 1, REASON_EFFECT)
  	end
  end
  
  
source_text: Surprise
  Activate only as Chain Link 2 or higher; apply the following effects, depending on the Chain Link number of this card, in sequence (skip over any that do not apply). You cannot activate this card if multiple cards/effects with the same name are in that Chain.
  ● 2+: Excavate the top cards of your Deck, equal to the Chain Link number of this card, then place them on the top of your Deck in any order.
  ● 3+: Send the top card of your Deck to the GY.
  ● 4+: Draw 1 card.
  You can only activate 1 "Surprise Chain" per turn.
target_text: -- Surprise
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetCondition(s.condition)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetCurrentChain() > 0 and Duel.CheckChainUniqueness()
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	local cl = Duel.GetCurrentChain()
  	if chk == 0 then
  		return Duel.GetFieldGroupCount(tp, LOCATION_DECK, 0) >= cl and (cl < 3 or Duel.IsPlayerCanDiscardDeck(tp, 1)) and (cl < 4 or Duel.IsPlayerCanDraw(tp, 1))
  	end
  	local cat = 0
  	if cl >= 3 then
  		cat = cat | CATEGORY_TOGRAVE
  		Duel.SetOperationInfo(0, CATEGORY_TOGRAVE, nil, 1, tp, LOCATION_DECK)
  	end
  	if cl >= 4 then
  		cat = cat | CATEGORY_DRAW
  		Duel.SetOperationInfo(0, CATEGORY_DRAW, nil, 0, tp, 1)
  	end
  	e:SetCategory(cat)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local cl = Duel.GetCurrentChain()
  	if cl >= 2 then
  		Duel.ConfirmDecktop(tp, cl)
  		local g = Duel.GetDecktopGroup(tp, cl)
  		if g:GetCount() > 0 then
  			Duel.SortDecktop(tp, tp, g:GetCount())
  		end
  	end
  	if cl >= 3 then
  		Duel.BreakEffect()
  		Duel.DiscardDeck(tp, 1, REASON_EFFECT)
  	end
  	if cl >= 4 then
  		Duel.BreakEffect()
  		Duel.Draw(tp, 1, REASON_EFFECT)
  	end
  end
  
  