--花火
local s,id,o=GetID()
function s.initial_effect(c)
	--fail
	local e2=Effect.CreateEffect(c)
	e2:SetType(EFFECT_TYPE_ACTIVATE)
	e2:SetCode(EVENT_FREE_CHAIN)
	e2:SetCost(s.cost)
	e2:SetOperation(s.op)
	c:RegisterEffect(e2)
end

function s.cost(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return true end
	local c=e:GetHandler()
	--fail in ep
	local e1=Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
	e1:SetCode(EVENT_PHASE+PHASE_END)
	e1:SetReset(RESET_PHASE+PHASE_END)
	e1:SetOperation(s.op)
	Duel.RegisterEffect(e1,tp)
	--draw
	local ge=Effect.CreateEffect(c)
	ge:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
	ge:SetCode(EVENT_CHAINING)
	ge:SetLabelObject(c)
	ge:SetCondition(s.drawcon)
	ge:SetOperation(s.drawop)
	Duel.RegisterEffect(ge,tp)
end

function s.op(e,tp,eg,ep,ev,re,r,rp)
	Duel.Win(1-tp,0xff)
end

function s.drawop(e,tp,eg,ep,ev,re,r,rp)
	Duel.Hint(HINT_CARD,0,id)
	local rc=re:GetHandler()
	Duel.Draw(rp,1,REASON_COST) 
	if rc:IsOnField() and Duel.SelectYesNo(rp,aux.Stringid(id,0)) and Duel.SendtoGrave(rc,REASON_EFFECT)~=0 then
		Duel.Draw(rp,1,REASON_EFFECT)
	end
end

function s.drawcon(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetLabelObject()
	if c:IsOnField() then
		return true
	else
		e:Reset()
	end
end