local s,id,o=GetID()
function s.initial_effect(c)
	aux.AddCodeList(c,78371393,6007213,32491822,69890967)
	--Activate
	local e1=Effect.CreateEffect(c)
	e1:SetCategory(CATEGORY_HANDES+CATEGORY_DRAW)
	e1:SetType(EFFECT_TYPE_ACTIVATE)
	e1:SetCode(EVENT_FREE_CHAIN)
    e1:SetCountLimit(1,id)
    e1:SetCondition(s.condition)
	e1:SetTarget(s.target)
	e1:SetOperation(s.activate)
	c:RegisterEffect(e1)
    	--tohand
	local e3=Effect.CreateEffect(c)
	e3:SetCategory(CATEGORY_TOHAND+CATEGORY_SEARCH)
	e3:SetType(EFFECT_TYPE_IGNITION)
	e3:SetRange(LOCATION_GRAVE)
	e3:SetCountLimit(1,id+o)
	e3:SetCost(aux.bfgcost)
	e3:SetTarget(s.thtg)
	e3:SetOperation(s.thop)
	c:RegisterEffect(e3)
end
function s.cfilter(c)
	return c:IsFaceup() and c:IsLevelAbove(10)
end
function s.condition(e,tp,eg,ep,ev,re,r,rp)
	return Duel.IsExistingMatchingCard(s.cfilter,tp,LOCATION_MZONE,0,1,nil)
end
function s.target(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return true end
	Duel.SetOperationInfo(0,CATEGORY_HANDES,nil,0,PLAYER_ALL,1)
	Duel.SetOperationInfo(0,CATEGORY_DRAW,nil,0,PLAYER_ALL,1)
end
function s.dfilter(c,tp)
	return c:IsLocation(LOCATION_GRAVE) and c:IsControler(tp)
end
function s.activate(e,tp,eg,ep,ev,re,r,rp)
	local h1=Duel.GetFieldGroupCount(tp,LOCATION_HAND,0)
	local h2=Duel.GetFieldGroupCount(tp,0,LOCATION_HAND)
    if h1>0 and h2>0 then
        Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_TOGRAVE)
        local g1=Duel.SelectMatchingCard(tp,aux.TRUE,tp,LOCATION_HAND,0,4,4,nil)
        Duel.ConfirmCards(1-tp,g1)
        Duel.Hint(HINT_SELECTMSG,1-tp,HINTMSG_TOGRAVE)
        local g2=Duel.SelectMatchingCard(1-tp,aux.TRUE,1-tp,LOCATION_HAND,0,4,4,nil)
        g1:Merge(g2)
        Duel.SendtoGrave(g1,REASON_EFFECT)
        local ct1=Duel.GetOperatedGroup():Filter(s.dfilter,nil,tp):GetCount()
        local ct2=Duel.GetOperatedGroup():Filter(s.dfilter,nil,1-tp):GetCount()
        Duel.Draw(tp,ct1,REASON_EFFECT)
        Duel.Draw(1-tp,ct2,REASON_EFFECT)
    elseif h1>0 and h2==0 then
        Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_TOGRAVE)
        local g1=Duel.SelectMatchingCard(tp,aux.TRUE,tp,LOCATION_HAND,0,4,4,nil)
        Duel.ConfirmCards(1-tp,g1)
        g1:Merge(g2)
        Duel.SendtoGrave(g1,REASON_EFFECT)
        local ct1=Duel.GetOperatedGroup():Filter(s.dfilter,nil,tp):GetCount()
        Duel.Draw(tp,ct1,REASON_EFFECT)
        Duel.Draw(1-tp,1,REASON_EFFECT)
    elseif h1==0 and h2>0 then
        Duel.Hint(HINT_SELECTMSG,1-tp,HINTMSG_TOGRAVE)
        local g2=Duel.SelectMatchingCard(1-tp,aux.TRUE,1-tp,LOCATION_HAND,0,4,4,nil)
        Duel.SendtoGrave(g2,REASON_EFFECT)
        local ct2=Duel.GetOperatedGroup():Filter(s.dfilter,nil,1-tp):GetCount()
        Duel.Draw(tp,1,REASON_EFFECT)
        Duel.Draw(1-tp,ct2,REASON_EFFECT)
    else
        Duel.Draw(tp,1,REASON_EFFECT)
        Duel.Draw(1-tp,1,REASON_EFFECT)
    end
end
function s.thfilter(c)
	return c:IsCode(78371393,6007213,32491822,69890967) and c:IsAbleToHand()
end
function s.thtg(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return Duel.IsExistingMatchingCard(s.thfilter,tp,LOCATION_DECK+LOCATION_GRAVE,0,1,nil) end
	Duel.SetOperationInfo(0,CATEGORY_TOHAND,nil,1,tp,LOCATION_DECK+LOCATION_GRAVE)
end
function s.thop(e,tp,eg,ep,ev,re,r,rp)
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_ATOHAND)
	local g=Duel.SelectMatchingCard(tp,s.thfilter,tp,LOCATION_DECK+LOCATION_GRAVE,0,1,1,nil)
	if g:GetCount()>0 then
		Duel.SendtoHand(g,nil,REASON_EFFECT)
		Duel.ConfirmCards(1-tp,g)
	end
end