InputState = inherited("InputState", State)

function InputState:new()
	local o = instance(self)
	o.joysticks = {}
	o.mouse = {}
	return o
end

function InputState:init()
end


--[[
                                                                  
88        88                      88                               
88        88                      88              ,d               
88        88                      88              88               
88        88 8b,dPPYba,   ,adPPYb,88 ,adPPYYba, MM88MMM ,adPPYba,  
88        88 88P'    "8a a8"    `Y88 ""     `Y8   88   a8P_____88  
88        88 88       d8 8b       88 ,adPPPPP88   88   8PP"""""""  
Y8a.    .a8P 88b,   ,a8" "8a,   ,d88 88,    ,88   88,  "8b,   ,aa  
 `"Y8888Y"'  88`YbbdP"'   `"8bbdP"Y8 `"8bbdP"Y8   "Y888 `"Ybbd8"'  
             88                                                    
             88          

]]

local AXES = 8

function InputState:update(time)
	--for index, joy in pairs(self.joysticks) do
	--	for i=1, AXES do
	--		local x = daisy.getJoystickAxises(index, i)
	--		joy.axes[i] = x
	--		--print(index, i, x)
	--	end
	--end
end

                                                                                                    
--[[
                                                          
88        88                         88                   
88        88                         88                   
88        88                         88                   
88aaaaaaaa88  ,adPPYba,   ,adPPYba,  88   ,d8  ,adPPYba,  
88""""""""88 a8"     "8a a8"     "8a 88 ,a8"   I8[    ""  
88        88 8b       d8 8b       d8 8888[      `"Y8ba,   
88        88 "8a,   ,a8" "8a,   ,a8" 88`"Yba,  aa    ]8I  
88        88  `"YbbdP"'   `"YbbdP"'  88   `Y8a `"YbbdP"'  
                                                          
                                                          
]]

function InputState:render()
end

function InputState:close()
end

function InputState:keyPressed(key)
end

function InputState:mouseClick(x, y, button, clickCount)	
end


ANALOG_ACTIONS = {
	left = {
		axis = 1,
		x = 1,
		y = 2,
		threshold = -1,
		angle = math.pi,
		arc = math.pi*0.33,
		group = "Lstick",
	},
	right = {
		axis = 1,
		x = 1,
		y = 2,
		threshold = 1,
		angle = 0,
		arc = math.pi*0.33,
		group = "Lstick",
	},
	up = {
		axis = 2,
		x = 1,
		y = 2,
		threshold = -1,
		angle = -math.pi*0.5,
		arc = math.pi*0.33,
		group = "Lstick",
	},
	down = {
		axis = 2,
		x = 1,
		y = 2,
		threshold = 1,
		angle = math.pi*0.5,
		arc = math.pi*0.33,
		group = "Lstick",
	},
	Rleft = {
		axis = 4,
		x = 4,
		y = 5,
		threshold = -1,
		angle = math.pi,
		arc = math.pi*0.33,
		group = "Rstick",
	},
	Rright = {
		axis = 4,
		x = 4,
		y = 5,
		threshold = 1,
		angle = 0,
		arc = math.pi*0.33,
		group = "Rstick",
	},
	Rup = {
		axis = 5,
		x = 4,
		y = 5,
		threshold = -1,
		angle = -math.pi*0.5,
		arc = math.pi*0.33,
		group = "Rstick",
	},
	Rdown = {
		axis = 5,
		x = 4,
		y = 5,
		threshold = 1,
		angle = math.pi*0.5,
		arc = math.pi*0.33,
		group = "Rstick",
	},
	Ltrigger2 = {
		axis = 3,
		threshold = 1,
	},
	Rtrigger2 = {
		axis = 6,
		threshold = 1,
	},
	Ltrigger = {
		axis = 7,
		threshold = 1,
	},
	Rtrigger = {
		axis = 8,
		threshold = 1,
	},
}

local DEVICE_CHECKERS = {
	keyboard = function(self, deviceId, key)
		return daisy.isKeyPressed(key)
	end,
	mouse = function(self, deviceId, key)
		return daisy.isMouseButtonPressed(key)
	end,
	joystick = function(self, deviceId, key)
		local joy = self.joysticks[deviceId]
		if joy then
			local adef = ANALOG_ACTIONS[key]
			if adef then
				if adef then
					local val = joy.axes[adef.axis]
					if math.sign(adef.threshold) == math.sign(val) and math.abs(val) > math.abs(adef.threshold*_config.analogAxisThreshold) then
						if not adef.angle or math.abs(math.angleDifference(math.angleBetweenPoints(0,0,joy.axes[adef.x], joy.axes[adef.y]), adef.angle)) <= adef.arc then
							return true
						end
					end
				end
			else
				return joy[key]
			end
		end
	end,
}

function InputState:isPressed(device, deviceId, key)
	return DEVICE_CHECKERS[device](self, deviceId, key)
end

function InputState:joyButtonPressed(joy, btn)
	if not self.joysticks[joy] then
		self.joysticks[joy] = {buttons = {}, axes = {0,0,0,0,0,0,0,0}}
	end
	self.joysticks[joy][btn] = true
end

function InputState:joyButtonReleased(joy, btn)
	if not self.joysticks[joy] then
		self.joysticks[joy] = {buttons = {}, axes = {0,0,0,0,0,0,0,0}}
	end
	self.joysticks[joy][btn] = false
end