-- 机壳中的炼狱
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:SetHintTiming(0,TIMING_END_PHASE)
	c:RegisterEffect(e1)
	--sp
	local e2=Effect.CreateEffect(c)
	e2:SetDescription(aux.Stringid(id,0))
	e2:SetType(EFFECT_TYPE_QUICK_O)
	e2:SetRange(LOCATION_SZONE)
	e2:SetCode(EVENT_FREE_CHAIN)
	e2:SetHintTiming(0,TIMING_END_PHASE)
	e2:SetTarget(s.spcost)
	e2:SetOperation(s.spop)
	c:RegisterEffect(e2)
	--link all zone
	local e3=Effect.CreateEffect(c)
	e3:SetDescription(aux.Stringid(id,1))
	e3:SetProperty(EFFECT_FLAG_CARD_TARGET)
	e3:SetType(EFFECT_TYPE_QUICK_O)
	e3:SetRange(LOCATION_SZONE)
	e3:SetCode(EVENT_FREE_CHAIN)
	e3:SetTarget(s.rettg)
	e3:SetOperation(s.retop)
	e3:SetCountLimit(1)
	c:RegisterEffect(e3)
	-- -- test 
	-- local e4=Effect.CreateEffect(c)
	-- e4:SetType(EFFECT_TYPE_QUICK_O)
	-- e4:SetRange(LOCATION_SZONE)
	-- e4:SetCode(EVENT_FREE_CHAIN)
	-- e4:SetOperation(s.testop)
	-- c:RegisterEffect(e4)
end

function s.spfilter(c,e,tp,level)
	return c:IsSetCard(0xbb)
		and c:IsCanBeSpecialSummoned(e,0,tp,false,false)
		and c:IsLevelBelow(level) 
		and (level==99 or c:IsType(TYPE_SPSUMMON))
end
function s.spcost(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return Duel.CheckReleaseGroup(tp,Card.IsSetCard,1,nil,0xaa) end
	local g=Duel.SelectReleaseGroup(tp,Card.IsSetCard,1,1,nil,0xaa)
	if Duel.Release(g,REASON_COST)>0 then
		local c=g:GetFirst()
		if c:IsCode(51194046) then 
			e:SetLabel(99)
		else
			e:SetLabel(c:GetLevel())
		end
	end
end
function s.spop(e,tp,eg,ep,ev,re,r,rp)
	local lv=e:GetLabel()
	if lv==nil then return end
	local tg=Duel.GetFieldGroup(tp,LOCATION_PZONE,0):Filter(Card.IsSetCard,nil,0xaa)
	if #tg>0 and Duel.SelectYesNo(tp,aux.Stringid(id,4)) then
		if Duel.SendtoHand(tg,nil,REASON_EFFECT)~=0 and tg:FilterCount(Card.IsCode,nil,51194046)>0 then
			lv=99
		end
	end
	Duel.BreakEffect()
	local sg=Duel.GetMatchingGroup(s.spfilter,tp,LOCATION_DECK,0,nil,e,tp,lv)
	if #sg>0 and Duel.SelectYesNo(tp,aux.Stringid(id,5)) then
		local c=sg:Select(tp,1,1,nil):GetFirst()
		Duel.SpecialSummon(c,0,tp,tp,true,true,POS_FACEUP)
	end
end

function s.filter(c)
	return c:IsLevelAbove(8) and c:IsSetCard(0xbb) and c:IsAbleToHand()
end
function s.rettg(e,tp,eg,ep,ev,re,r,rp,chk,chkc)
	if chkc then return chkc:IsLocation(LOCATION_MZONE) and chkc:IsAbleToHand() end
	if chk==0 then return Duel.IsExistingTarget(s.filter,tp,LOCATION_MZONE,0,1,nil) end
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_RTOHAND)
	local g=Duel.SelectTarget(tp,s.filter,tp,LOCATION_MZONE,0,1,1,nil)
	Duel.SetOperationInfo(0,CATEGORY_TOHAND,g,1,0,0)
end
function s.retop(e,tp,eg,ep,ev,re,r,rp)
	local tc=Duel.GetFirstTarget()
	if not (tc and tc:IsRelateToEffect(e)) then return end
	if Duel.SendtoHand(tc,nil,REASON_EFFECT)==0 then return end
	local c=e:GetHandler()
	-- Add pend effect to ALL your pend cards
	local pg=Duel.GetMatchingGroup(s.pendfilter,tp,~0,0,nil)
	pg:ForEach(s.addpend)
end

function s.testop(e,tp,eg,ep,ev,re,r,rp)
	local pg=Duel.GetFieldGroup(tp,LOCATION_PZONE,0)
	local pc=pg:GetFirst()
	s.addpend(pc)
end

function s.pendfilter(c)
	return c:IsSetCard(0xaa) and c:IsType(TYPE_PENDULUM)
end
function s.addpend(pc)
	local e1=Effect.CreateEffect(pc)
	e1:SetDescription(aux.Stringid(id,2))
	e1:SetType(EFFECT_TYPE_FIELD)
	e1:SetCode(EFFECT_SPSUMMON_PROC_G)
	e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_UNCOPYABLE)
	e1:SetRange(LOCATION_PZONE)
	e1:SetCondition(s.pendcon)
	e1:SetOperation(s.pendop())
	e1:SetValue(SUMMON_TYPE_PENDULUM)
	e1:SetReset(RESET_PHASE+PHASE_END)
	pc:RegisterEffect(e1)
end

function s.pendcon(e,c,og)
	if c==nil then return true end
	local tp=c:GetControler()
	local eset={Duel.IsPlayerAffectedByEffect(tp,EFFECT_EXTRA_PENDULUM_SUMMON)}
	if Auxiliary.PendulumChecklist&(0x1<<tp)~=0 and #eset==0 then return false end
	local rpz=Duel.GetFieldCard(tp,LOCATION_PZONE,1)
	if rpz==nil or c==rpz then return false end
	local lscale=c:GetLeftScale()
	local rscale=rpz:GetRightScale()
	if lscale>rscale then lscale,rscale=rscale,lscale end
	local loc=0
	if Duel.GetLocationCount(tp,LOCATION_MZONE)>0 then loc=loc+LOCATION_HAND end
	if Duel.GetLocationCountFromEx(tp,tp,nil,TYPE_XYZ)>0 then loc=loc+LOCATION_EXTRA end
	if loc==0 then return false end
	local g=nil
	if og then
		g=og:Filter(Card.IsLocation,nil,loc)
	else
		g=Duel.GetFieldGroup(tp,loc,0)
	end
	return g:IsExists(Auxiliary.PConditionFilter,1,nil,e,tp,lscale,rscale,eset)
end

function s.pendop()
	local origin=aux.PendOperation()
	return function(e,tp,eg,ep,ev,re,r,rp,c,sg,og)
		s.originGetLocationCountFromEx=Duel.GetLocationCountFromEx
		Duel.GetLocationCountFromEx=s.GetLocationCountFromEx
		origin(e,tp,eg,ep,ev,re,r,rp,c,sg,og)
		Duel.GetLocationCountFromEx=s.originGetLocationCountFromEx
		local g=sg:Clone()
		local g1=g:Filter(Card.IsLocation,nil,LOCATION_EXTRA)
		local g2=g:Filter(aux.NOT(Card.IsLocation),nil,LOCATION_EXTRA)
		if #g1>0 and Duel.GetLocationCountFromEx(tp,tp,nil,TYPE_PENDULUM)>0 and Duel.SelectYesNo(tp,aux.Stringid(id,3)) then
			local cg=g1:Select(tp,1,1,nil)
			g1:RemoveCard(cg:GetFirst())
		end
		Duel.Remove(g1,POS_FACEDOWN,REASON_REPLACE)
		Duel.SpecialSummon(g,SUMMON_TYPE_PENDULUM,tp,tp,false,false,POS_FACEUP)
		sg:Clear()
	end
end

function s.GetLocationCountFromEx(tp,tp,sg,t,...)
	if t==TYPE_PENDULUM then t=TYPE_SYNCHRO end
	return s.originGetLocationCountFromEx(tp,tp,sg,t,...)
end