extends Reference

const NAME = "working"

var ctx = null
var crafting_item = null

func _init():
	pass

func enter_from(state, item_to_craft = null):
	crafting_item = item_to_craft
	assert(crafting_item != null)
	ctx.coin_machine.enabled = false
	ctx.item_holder.add_enabled = false
	for item_slot in ctx.item_slots:
		item_slot.add_enabled = false
		item_slot.remove_enabled = false
	ctx.gen_timer.start()
	ctx.progress_text.text = "Progress: 0%"

func exit_to(state):
	pass

func update(delta):
	var time_left : float = ctx.gen_timer.time_left
	var time_passed : float = ctx.gen_time - time_left
	var percent_complete : float = (time_passed / ctx.gen_time) * 100.0
	ctx.progress_text.text = "Progress: %d%%" % percent_complete
	
func on_coin_machine_coin_requirement_met(player):
	assert(false, "coin machine should be disabled while crafting")

func on_gen_timer_timeout():
	for item_slot in ctx.item_slots:
		if item_slot.has_item():
			item_slot.destroy_item()
	ctx.item_holder.spawn_item(crafting_item)
	ctx.change_state(ctx.StateBlocked.new(), "Finished: %s" % crafting_item.name)

func on_slot_item_changed(item):
	assert(item == null, "slot items can only be destroyed at this point")

func on_item_holder_item_changed(item):
	assert(item.item_type == crafting_item, "only the crafted item can be added to the item holder")
