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

local Zg_keyhandler_zgsabber = 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_zgsabber )
	end)
end)

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

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

function Zg_keyhandler_zgsabber: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_zgsabber: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",
					}
					
					----冷却时间
					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,
					}
				
					----按键的前提条件
					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 
		
						
					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
						
						local item = ThePlayer.replica.inventory and ThePlayer.replica.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
						if item == nil then
							ThePlayer.sword = 0
						elseif item.prefab == "zgsabber_sword" then
							ThePlayer.sword = 40
						elseif item.prefab == "zgsabber_sword2" then
							ThePlayer.sword = 60
						end
						
						if (skilltable[Action] == "x_skill" or skilltable[Action] == "c_skill") and 
							ThePlayer.sword == 0
							then
							--
						else
					
							--技能进入冷却状态
							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
							
						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_zgsabber