--于天阴刻开的正道
local s,id,o=GetID()
o=1e9
function s.initial_effect(c)
	--Activate
	local e0=Effect.CreateEffect(c)
	e0:SetType(EFFECT_TYPE_ACTIVATE)
	e0:SetCode(EVENT_FREE_CHAIN)
	e0:SetHintTiming(0,TIMING_END_PHASE)
	c:RegisterEffect(e0)
    -- draw and handes
    local e1=Effect.CreateEffect(c)
	e1:SetCategory(CATEGORY_DRAW+CATEGORY_HANDES)
	e1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
	e1:SetCode(EVENT_CHAINING)
	e1:SetRange(LOCATION_SZONE)
    e1:SetCondition(s.drawcon)
	e1:SetOperation(s.drawop)
	c:RegisterEffect(e1)
    --send to grave
    local e2=Effect.CreateEffect(c)
	e2:SetCategory(CATEGORY_DECKDES)
	e2:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
	e2:SetCode(EVENT_CHAINING)
	e2:SetRange(LOCATION_SZONE)
	e2:SetCondition(s.sgcon)
	e2:SetOperation(s.sgop)
	c:RegisterEffect(e2)
    -- set itself
    local e3=Effect.CreateEffect(c)
	e3:SetDescription(aux.Stringid(id,2))
	e3:SetType(EFFECT_TYPE_SINGLE+EFFECT_TYPE_TRIGGER_O)
	e3:SetCode(EVENT_TO_GRAVE)
	e3:SetCondition(s.backcon)
	e3:SetOperation(s.backop)
    e3:SetCountLimit(1,id)
	c:RegisterEffect(e3)
end

function s.drawcon(e,tp,eg,ep,ev,re,r,rp)
    local rc=re:GetHandler()
    local c=e:GetHandler()
    return re:IsHasCategory(CATEGORY_DECKDES) and rc:GetCode()~=id and c:GetFlagEffect(id)==0 and Duel.GetCurrentPhase()==PHASE_END
end
-- function s.drawtg(e,tp,eg,ep,ev,re,r,rp,chk)
--     local ex,g,gc,dp,dv=Duel.GetOperationInfo(ev,CATEGORY_DECKDES)
-- 	if chk==0 then return dv>0 end
-- 	Duel.SetOperationInfo(0,CATEGORY_HANDES,nil,0,tp,dv)
-- 	Duel.SetOperationInfo(0,CATEGORY_DRAW,nil,0,tp,dv)
-- end
function s.drawop(e,tp,eg,ep,ev,re,r,rp)
    if Duel.SelectYesNo(tp,aux.Stringid(id,0)) then
        Duel.Hint(HINT_CARD,0,id)
        local c=e:GetHandler()
        c:RegisterFlagEffect(id,RESET_EVENT+RESETS_STANDARD+RESET_PHASE+PHASE_END,0,1)
        Duel.ChangeChainOperation(ev,s._drawop(re:GetOperation()))
    end
end
function s._drawop(op)
    return function(e,tp,eg,ep,ev,re2,r,rp)
        local ex,g,gc,dp,dv=Duel.GetOperationInfo(ev,CATEGORY_DECKDES)
        op(e,tp,eg,ep,ev,re,r,rp)
        local d=Duel.Draw(tp,dv,REASON_EFFECT)
        if d>0 then
            Duel.ShuffleHand(tp)
            Duel.DiscardHand(tp,aux.TRUE,dv,dv,REASON_EFFECT+REASON_DISCARD)
        end
    end
end

function s.sgfilter(c)
	return c:IsType(TYPE_MONSTER) 
        and c:IsAttribute(ATTRIBUTE_LIGHT) 
        and c:IsLevelBelow(4) 
        and c:IsAbleToGrave()
end
function s.sgcon(e,tp,eg,ep,ev,re,r,rp)
    local rc=re:GetHandler()
    local c=e:GetHandler()
    return re:IsHasCategory(CATEGORY_HANDES) and rc:GetCode()~=id and c:GetFlagEffect(id+o)==0
end
function s.sgop(e,tp,eg,ep,ev,re,r,rp)
    if Duel.SelectYesNo(tp,aux.Stringid(id,1)) then
        Duel.Hint(HINT_CARD,0,id)
        local c=e:GetHandler()
        c:RegisterFlagEffect(id+o,RESET_EVENT+RESETS_STANDARD+RESET_PHASE+PHASE_END,0,1)
        Duel.ChangeChainOperation(ev,s._sgop(re:GetOperation()))
    end
end
function s._sgop(op)
    return function(e,tp,eg,ep,ev,re2,r,rp)
        local ex,g,gc,dp,dv=Duel.GetOperationInfo(ev,CATEGORY_HANDES)
        op(e,tp,eg,ep,ev,re,r,rp)
        Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_TOGRAVE)
        local g=Duel.SelectMatchingCard(tp,s.sgfilter,tp,LOCATION_DECK,0,dv,dv,nil)
        if g:GetCount()>0 then
            Duel.SendtoGrave(g,REASON_EFFECT)
        end
    end
end

function s.backfilter(c)
    return c:IsType(TYPE_MONSTER) and c:IsAbleToHand()
end
function s.backcon(e,tp,eg,ep,ev,re,r,rp)
	return e:GetHandler():IsPreviousLocation(LOCATION_DECK)
        or (e:GetHandler():IsPreviousLocation(LOCATION_HAND) and bit.band(r,0x4040)==0x4040)
end
function s.backop(e,tp,eg,ep,ev,re,r,rp)
    local c=e:GetHandler()
    if Duel.MoveToField(c,tp,tp,LOCATION_SZONE,POS_FACEUP,true) then
        local g1=Duel.GetMatchingGroup(s.backfilter,tp,LOCATION_MZONE,0,nil)
        if #g1>0 and Duel.SelectYesNo(tp,aux.Stringid(id,3)) then
            local gg1=g1:Select(tp,1,1,nil)
            Duel.SendtoHand(gg1,nil,REASON_EFFECT)
        end
    end
end
