local function ignore( self )
	self.ignore_ = not self.ignore_
end

local Zg_KeyHandler_Ram = Class(function(self, inst)
	self.inst = inst
	self.paused = false
	self.ignore_ = false
	self.ignore_event = net_event(self.inst.GUID, "ignore")
	self.handler = TheInput:AddKeyHandler(function(key, down) self:OnRawKey(key, down) end )
	
	self.inst:ListenForEvent( "gamepaused", function(inst, paused) self.paused = paused end )
	self.inst:ListenForEvent( "ignore", function(inst)
		ignore( inst.components.zg_keyhandler_ram )
	end)
end)

function Zg_KeyHandler_Ram:StartIgnoring()
	self.ignore_event:push()
end

function Zg_KeyHandler_Ram:StopIgnoring()
	self.ignore_event:push()
end

function Zg_KeyHandler_Ram:OnRawKey(key, down)
	local player = ThePlayer
	if player ~= nil then
  		if (key and not down) and not self.paused and not self.ignore_ then
      			player:PushEvent("keyup", {inst = self.inst, player = player, key = key})
		elseif key and down and not self.paused and not self.ignore_ then
      			player:PushEvent("keydown", {inst = self.inst, player = player, key = key})
		end
  	end
end

function Zg_KeyHandler_Ram:AddActionListener(Namespace, Key, Action, Event)
	if Event == nil then
		self.inst:ListenForEvent("keyup", function(inst, data)
			if data.inst == ThePlayer then
				if data.key == Key then
				
					local skilltable = 
					{
						["r_skill"] = "r_skill",
					}
					
					----冷却时间
					local cdtable = 
					{
						["r_skill"] = data.inst.r_skill_cd,
					}
				
					----按键的前提条件
					if (
							--没死
							ThePlayer.components.health and not ThePlayer.components.health:IsDead() or
							
							--不是鬼魂
							not ThePlayer:HasTag("playerghost")
							
						) and 
						not ThePlayer.sg:HasStateTag("tent") and 
						not ThePlayer.sg:HasStateTag("skill") and ThePlayer[ skilltable[Action] ] == true --and
						
						--进入技能的条件
						--ThePlayer.canskill( Action )
						
					then
						
						--技能进入冷却状态
						ThePlayer[ skilltable[Action] ] = false
						if not ( ThePlayer[ skilltable[Action] ] or ThePlayer.components.timer:TimerExists( skilltable[Action] ) ) then
							ThePlayer.components.timer:StartTimer( skilltable[Action], cdtable[Action] )
						end
				
						local x,y,z = ( TheInput:GetWorldPosition() or Vector3(0,0,0) ):Get()
						if TheWorld.ismastersim then
							ThePlayer:PushEvent("keyaction"..Namespace..Action, 
							{ Namespace = Namespace, Action = Action, Fn = MOD_RPC_HANDLERS[Namespace][MOD_RPC[Namespace][Action].id], x = x, y = y, z = z})
						else
							--print("ok")
							SendModRPCToServer( MOD_RPC[Namespace][Action], x,y,z)
						end
						
					else
					
						--if ThePlayer.components.talker then
						--	ThePlayer.components.talker:Say("Skill is in CD")
						--end
						
					end
						
				end
			end	
		end)
	elseif Event ~= nil then
		self.inst:ListenForEvent(string.lower(Event), function(inst, data)
			if data.inst == ThePlayer then
				if data.key == Key then
					if TheWorld.ismastersim then
						ThePlayer:PushEvent("keyaction"..Namespace..Action, 
						{ Namespace = Namespace, Action = Action, Fn = MOD_RPC_HANDLERS[Namespace][MOD_RPC[Namespace][Action].id]})
					else
						SendModRPCToServer( MOD_RPC[Namespace][Action])
					end
				end
			end	
		end)
	end

	if TheWorld.ismastersim then
		self.inst:ListenForEvent("keyaction"..Namespace..Action, function(inst, data)
			if not data.Action == Action and not data.Namespace == Namespace then
				return
			end

          		data.Fn(inst, data.x, data.y, data.z)
		end, self.inst) 
	end
end

return Zg_KeyHandler_Ram