-- 卧薪尝胆
local s,id,o=GetID()
function s.initial_effect(c)
    -- draw
	local e1=Effect.CreateEffect(c) 
	e1:SetCategory(CATEGORY_DRAW)
	e1:SetType(EFFECT_TYPE_ACTIVATE)
	e1:SetCode(EVENT_FREE_CHAIN)
	e1:SetHintTiming(TIMING_BATTLE_END)
	e1:SetCondition(s.condition)
	e1:SetTarget(s.target)
	e1:SetOperation(s.op)
    c:RegisterEffect(e1)
	-- resound
	local e2=Effect.CreateEffect(c)
	e2:SetType(EFFECT_TYPE_SINGLE+EFFECT_TYPE_TRIGGER_O)
	e2:SetProperty(EFFECT_FLAG_DELAY)
	e2:SetCode(EVENT_DESTROYED)
	e2:SetCondition(s.recon)
	e2:SetOperation(s.reop)
	c:RegisterEffect(e2)
end
function s.condition(e,tp,eg,ep,ev,re,r,rp)
	return Duel.GetCurrentPhase()==PHASE_BATTLE
end
function s.target(e,tp,eg,ep,ev,re,r,rp,chk)
	local c=Duel.GetFieldGroupCount(tp,0,LOCATION_MZONE)
	if chk==0 then return c==0 end
	Duel.SetOperationInfo(0,CATEGORY_DRAW,nil,0,tp,2)
	Duel.SetOperationInfo(0,CATEGORY_DRAW,nil,0,1-tp,7)
end
function s.op(e,tp,eg,ep,ev,re,r,rp)
	Duel.Draw(tp,2,REASON_EFFECT)
	Duel.Draw(1-tp,7,REASON_EFFECT)
	if e:GetLabel()~=nil then e:Reset() end

	local e1=Effect.CreateEffect(e:GetHandler())
	e1:SetCode(EFFECT_CANNOT_INACTIVATE)
	e1:SetTargetRange(1,1)
	e1:SetCondition(s.accon)
	e1:SetValue(aux.TRUE)
	e1:SetLabel(Duel.GetTurnCount())
	e1:SetReset(RESET_PHASE+PHASE_END,2)
	Duel.RegisterEffect(e1,tp)
    local e2=e1:Clone()
    e2:SetCode(EFFECT_CANNOT_DISEFFECT)
	Duel.RegisterEffect(e2,tp)
	local e3=e1:Clone()
	e3:SetCode(EFFECT_CANNOT_BE_EFFECT_TARGET)
	e3:SetProperty(EFFECT_FLAG_SET_AVAILABLE+EFFECT_FLAG_IGNORE_IMMUNE)
	e3:SetTargetRange(LOCATION_MZONE,0)
	e3:SetValue(aux.tgoval)
	Duel.RegisterEffect(e3,tp)
end
function s.accon(e)
	return e:GetLabel()~=Duel.GetTurnCount()
end

function s.recon(e,tp,eg,ep,ev,re,r,rp)
	return bit.band(r,REASON_EFFECT)~=0
end
function s.reop(e,tp,eg,ep,ev,re,r,rp)
	local e1=Effect.CreateEffect(e:GetHandler())
	e1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
	e1:SetCode(EVENT_PHASE+PHASE_BATTLE)
	e1:SetLabel(Duel.GetTurnCount())
	e1:SetCondition(s.condition)
	e1:SetTarget(s.target)
	e1:SetOperation(s.op)
	e1:SetCountLimit(1)
	-- if Duel.GetCurrentPhase()<=PHASE_BATTLE then
	-- 	e1:SetReset(RESET_PHASE+PHASE_BATTLE+RESET_OPPO_TURN,2)
	-- else
	-- 	e1:SetReset(RESET_PHASE+PHASE_BATTLE+RESET_OPPO_TURN)
	-- end
	Duel.RegisterEffect(e1,tp)
end
