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

local Zg_KeyHandler_Suofeiya = 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_suofeiya )
	end)
end)

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

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

function Zg_KeyHandler_Suofeiya: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_Suofeiya: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 = 
					{
						--["z_skill"] = "z_skill",
						["x_skill"] = "x_skill",
						["c_skill"] = "c_skill",
						["v_skill"] = "v_skill",
						["r_skill"] = "r_skill",
						["l_skill"] = "l_skill",
						["t_skill"] = "t_skill",
					}
					
					----冷却时间
					local cdtable = 
					{
						--["z_skill"] = data.inst.z_skill_cd,
						["x_skill"] = data.inst.x_skill_cd,
						["c_skill"] = data.inst.c_skill_cd,
						["v_skill"] = data.inst.v_skill_cd,
						["r_skill"] = data.inst.r_skill_cd,
						["l_skill"] = data.inst.l_skill_cd,
						["t_skill"] = data.inst.t_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
						
						--ID
						--ThePlayer.userid == TUNING.SUOFEIYA_OKID 
						
					then
					
						--按键进入冷却态
						--if not ( ThePlayer[ skilltable[Action.."_ok"] ] or ThePlayer.components.timer:TimerExists( skilltable[Action.."_ok"] ) ) then
						--	ThePlayer.components.timer:StartTimer(skilltable[Action].."_ok", .1)
						--end
						
						--技能进入冷却状态
						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_Suofeiya