-- 暗黑界的武魂
local s,id,o=GetID()
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_SPSUMMON)
	c:RegisterEffect(e0)

    -- force battle
	local e1=Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_TRIGGER_O)
	e1:SetCode(EVENT_SPSUMMON_SUCCESS)
	e1:SetRange(LOCATION_SZONE)
	e1:SetCondition(s.battlecon)
	e1:SetTarget(s.battletg)
	e1:SetOperation(s.battleop)
	e1:SetCountLimit(1,id)
	c:RegisterEffect(e1) 

    -- draw
	local e2=Effect.CreateEffect(c)
	e2:SetDescription(aux.Stringid(id,1))
	e2:SetCategory(CATEGORY_HANDES+CATEGORY_DRAW)
	e2:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_TRIGGER_F)
	e2:SetCode(EVENT_TO_GRAVE)
	e2:SetRange(LOCATION_SZONE)
	e2:SetProperty(EFFECT_FLAG_DAMAGE_STEP+EFFECT_FLAG_DELAY)
	--e2:SetCountLimit(1,id+id)
	e2:SetCondition(s.drcon)
	e2:SetTarget(s.drtg)
	e2:SetOperation(s.drop)
	c:RegisterEffect(e2)
end

function s.battlecon(e,tp,eg,ep,ev,re,r,rp)
    if #eg~=1 then return false end
    local ec=eg:GetFirst()
    return ec:IsSetCard(0x6) and ec:IsFaceup() and ec:IsSummonPlayer(tp) and ec:IsAttackPos() and (Duel.GetCurrentPhase()==PHASE_MAIN1) and Duel.IsAbleToEnterBP()
end
function s.battletg(e,tp,eg,ep,ev,re,r,rp,chk)
	if chkc then return chkc:IsOnField() and chkc~=e:GetHandler() end
	if chk==0 then return Duel.IsExistingTarget(aux.TRUE,tp,nil,LOCATION_MZONE,1,e:GetHandler()) end
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_ATTACKTARGET)
	local g=Duel.SelectTarget(tp,aux.TRUE,tp,nil,LOCATION_MZONE,1,1,e:GetHandler())
end
function s.battleop(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	local tc=Duel.GetFirstTarget()
	local ec=eg:GetFirst()
	if ec:IsAttackable() and tc:IsOnField() then
		-- attacker. Due to ygopro bug(?) must be a trigger_f
		local e1=Effect.CreateEffect(ec)
		e1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_TRIGGER_F)
		e1:SetCode(EVENT_PHASE+PHASE_BATTLE_START)
		e1:SetProperty(EFFECT_FLAG_CANNOT_NEGATE+EFFECT_FLAG_CANNOT_DISABLE)
		e1:SetRange(LOCATION_MZONE)
		e1:SetOperation(s.battleop2)
		e1:SetTarget(s.battle2tg)
		e1:SetLabelObject(tc)
		e1:SetCountLimit(1)
		ec:RegisterEffect(e1)

		-- cannot goto ep
		local e2=Effect.CreateEffect(c)
		e2:SetType(EFFECT_TYPE_FIELD)
		e2:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
		e2:SetCode(EFFECT_CANNOT_EP)
		e2:SetTargetRange(1,1)
		e2:SetReset(RESET_PHASE+PHASE_MAIN2+PHASE_BATTLE+RESET_SELF_TURN+RESET_OPPO_TURN)
		Duel.RegisterEffect(e2,tp)

		-- silent to simulate skip battle start step
		local e3=Effect.CreateEffect(c)
		e3:SetType(EFFECT_TYPE_FIELD)
		e3:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
		e3:SetCode(EFFECT_CANNOT_ACTIVATE)
		e3:SetReset(RESET_PHASE+PHASE_BATTLE_START)
		e3:SetCondition(s.silentcon(e1))
		e3:SetTargetRange(1,1)
		e3:SetValue(1)
		Duel.RegisterEffect(e1,tp)
		
		Duel.SkipPhase(Duel.GetTurnPlayer(),PHASE_MAIN1,RESET_PHASE+PHASE_END,1)
	end
end
function s.silentcon(te)
	return function(e)
		return e~=te
	end
end
function s.battleop2(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	local tc=e:GetLabelObject()
	--draw		
	local e1=Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
	e1:SetCode(EVENT_BATTLED)
	e1:SetRange(LOCATION_MZONE)
	e1:SetOperation(s.battledop)
	e1:SetLabelObject(tc)
	e1:SetReset(RESET_PHASE+PHASE_END)
	c:RegisterEffect(e1)
	--if Duel.GetTurnPlayer()~=tp then
	--	Duel.CalculateDamage(tc,c,true)
	--else
		Duel.CalculateDamage(c,tc,true)
	--end
	e:Reset()
end

function s.battle2tg(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return true end
	Duel.SetChainLimit(aux.FALSE)
end

function s.cfilter(c,tp,re)
	local rc=re:GetHandler()
	return c:IsSetCard(0x6)
        and c:IsPreviousControler(tp)
		and not (c:IsReason(REASON_EFFECT) and c:IsPreviousLocation(LOCATION_HAND))
end

function s.drcon(e,tp,eg,ep,ev,re,r,rp)
	return re and eg:IsExists(s.cfilter,1,nil,tp,re)
end
function s.drtg(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return Duel.IsPlayerCanDraw(tp,2)
		and Duel.IsExistingMatchingCard(Card.IsDiscardable,tp,LOCATION_HAND,0,1,nil,REASON_EFFECT) end
	Duel.SetOperationInfo(0,CATEGORY_HANDES,nil,0,tp,2)
	Duel.SetOperationInfo(0,CATEGORY_DRAW,nil,0,tp,1)
end
function s.drop(e,tp,eg,ep,ev,re,r,rp)
    if Duel.Draw(tp,1,REASON_EFFECT) then
	    Duel.DiscardHand(tp,Card.IsDiscardable,2,2,REASON_EFFECT+REASON_DISCARD)
	end
end
function s.battledop(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	local bc=c:GetBattleTarget()
	if c and c:IsRelateToBattle() and Duel.GetBattleDamage(c:GetOwner())==0 then
		Duel.Draw(c:GetOwner(),1,REASON_EFFECT)
	else
	    Duel.DiscardHand(c:GetOwner(),Card.IsDiscardable,1,1,REASON_EFFECT+REASON_DISCARD)
	end
	if bc and bc:IsRelateToBattle() and Duel.GetBattleDamage(bc:GetOwner())==0 then
		Duel.Draw(bc:GetOwner(),1,REASON_EFFECT)
	else
	    Duel.DiscardHand(bc:GetOwner(),Card.IsDiscardable,1,1,REASON_EFFECT+REASON_DISCARD)
	end
	e:Reset()
end