----------------------------------------------------------------------------------------------------
-- Button
----------------------------------------------------------------------------------------------------
-- Разработчик: Andrey Fidrya (Zmey) af@svitonline.com
----------------------------------------------------------------------------------------------------

class "ph_button"

function ph_button:__init(obj, storage)
	self.object = obj
	self.st = storage
end

function ph_button:reset_scheme()
	self.object:play_cycle(self.st.anim, self.st.blending)

	self.last_hit_tm = time_global()
end

function ph_button:update(delta)
	if xr_logic.try_switch_to_another_section(self.object, self.st, db.actor) then
		return
	end
end

function ph_button:try_switch()
	local st = db.storage[self.object:id()]
	if st.active_scheme and st.active_scheme == "ph_button" and self.st.on_press then
		--if xr_logic.try_switch_to_another_section(obj, self.st, db.actor) then
		if xr_logic.switch_to_section(self.object, self.st.ini,
		   xr_logic.pick_section_from_condlist(db.actor, self.object, self.st.on_press.condlist)) then
			return true
		end
	end
	return false
end

function ph_button:hit_callback(obj, amount, local_direction, who, bone_index)
	return
--[[	local who_name
	if who then
		who_name = who:name()
	else
		who_name = "nil"
	end

	printf("_bp: ph_button:hit_callback: obj='%s', amount=%d, who='%s'", obj:name(), amount, who_name)

	if time_global() - self.last_hit_tm > 500 then
		self.last_hit_tm = time_global()
		if self:try_switch() then
			return
		end
	end
	]]
end

function ph_button:use_callback(victim, who)
	printf("_bp: ph_button:use_callback: [%s] used by [%s]",
		victim:name(), who:name())

	if self:try_switch() then
		return
	end
end

---------------------------------------------------------------------------------------------------------------------
function add_to_binder(npc, ini, scheme, section, storage)
	printf("DEBUG: add_to_binder: npc:name()='%s', scheme='%s', section='%s'", npc:name(), scheme, section)

 	local new_action = ph_button(npc, storage)

	xr_logic.subscribe_action_for_events(npc, storage, new_action)
end

function set_scheme(npc, ini, scheme, section, gulag_name)
	local st = xr_logic.assign_storage_and_bind(npc, ini, scheme, section)

	st.logic     = xr_logic.cfg_get_switch_conditions(ini, section, npc)
	
	st.on_press  = xr_logic.cfg_get_condlist(ini, section, "on_press", npc)

	st.tooltip  = utils.cfg_get_string(ini, section, "tooltip", npc, false, "")
	if st.tooltip then
		npc:set_tip_text(st.tooltip)
	else
		npc:set_tip_text("")
	end

	st.anim = utils.cfg_get_string(ini, section, "anim", npc, true, "")
	st.blending = utils.cfg_get_bool (ini, section, "anim_blend", npc, false, true)
    if st.blending == nil then 	
       st.blending = true 
    end
end

