
controller = {
    input = { kb=0, pad=1 },
    keysdown = {},
    kb_keys= {  {name="up",   code="keyup"},
                {name="down", code="keydow"},
                {name="left", code="keyleft"},
                {name="right",code="keyright"},
                {name="jump", code="keyjump"},
                {name="shoot",code="keyshoot"}},
    pad_keys= { {name="up",   code="btnup"},
                {name="down", code="btndown"},
                {name="left", code="keleft"},
                {name="right",code="btnright"},
                {name="jump", code="btnjump"},
                {name="shoot",code="btnshoot"},
                {name="next", code="btncycle1"},
                {name="prev", code="btncycle2"},
                {name="pause",code="btnpause"}}
}

function controller:init()
	-- Configuració dels input
    keyUp    = tonumber(config.key("keyup"))    or key.UP
    keyDown  = tonumber(config.key("keydown"))  or key.DOWN
    keyLeft  = tonumber(config.key("keyleft"))  or key.LEFT
    keyRight = tonumber(config.key("keyright")) or key.RIGHT
    keyJump  = tonumber(config.key("keyjump"))  or key.UP
    keyShoot = tonumber(config.key("keyshoot")) or key.SPACE

    btnUp     = tonumber(config.key("btnup"))     or  pad.UP
    btnDown   = tonumber(config.key("btndown"))   or  pad.DOWN
    btnLeft   = tonumber(config.key("btnleft"))   or  pad.LEFT
    btnRight  = tonumber(config.key("btnright"))  or  pad.RIGHT
    btnJump   = tonumber(config.key("btnjump"))   or  pad.B
    btnShoot  = tonumber(config.key("btnshoot"))  or  pad.A
    btnCycle1 = tonumber(config.key("btncycle1")) or  pad.RIGHTSHOULDER
    btnCycle2 = tonumber(config.key("btncycle2")) or  pad.LEFTSHOULDER
    btnPause  = tonumber(config.key("btnpause"))  or  pad.START
end

function controller:define(code, key_code)
    config.key(code, key_code)
    -- To do: comprovar que es un code valid 
    return true
end

function controller:check(action)
    local result = false

    if action=="up" then
        result = key.down(keyUp) or pad.down(btnUp)
    elseif action=="down" then
        result = key.down(keyDown) or pad.down(btnDown)
    elseif action=="left" then
        result = key.down(keyLeft) or pad.down(btnLeft)
    elseif action=="right" then
        result = key.down(keyRight) or pad.down(btnRight)
    elseif action=="jump" then
        result = key.down(keyJump) or pad.down(btnJump)
    elseif action=="shoot" then
        -- No gaste key.press perque no vull "metralleta"
        -- s'obliga a alçar la pulsació
        result = key.down(keyShoot) or pad.down(btnShoot)
        if result then
            if self.keysdown[keyShoot] then 
                result = false 
            else
                result = true
            end
            self.keysdown[keyShoot] = true
        else
            self.keysdown[keyShoot] = false
        end
    elseif action=="ESC" then
        result = key.press(key.ESCAPE) or pad.press(btnPause)
    end
    

    return result
end

function controller:key_release(keyid)
    if keyid==nil then 
        print("INVALID KEY")
        return
    end
    -- if keysdown[keyid] then print("Pressed "..keyid) end
    if key.down(keyid) or pad.down(keyid) then
       self.keysdown[keyid] = true
    elseif self.keysdown[keyid]
    then
        self.keysdown[keyid] = false
        return true
    end
    return false
end

function controller:onKeyUp(action)
    local resultKey = false
    local resultPad = false
    local keyDown = false
    local buttDown = false
    local keyid = nil
    local buttid = nil
    
    action = string.upper(action)
    
    if action=="UP" then
        keyid  = keyUp
        buttid = btnUp
    elseif action=="DOWN" then
        keyid  = keyDown
        buttid = btnDown
    elseif action=="LEFT" then
        keyid  = keyLeft
        buttid = btnLeft
    elseif action=="RIGHT" then
        keyid  = keyRight
        buttid = btnRight
    elseif action=="JUMP" then
        keyid  = keyJump
        buttid = btnJump
    elseif action=="SHOOT" then
        keyid  = keyShoot
        buttid = btnShoot
    elseif action=="ESC" then
        keyid  = key
        buttid = btn
    end
    
    
    if keyid~=nil then resultKey = key.down(keyid) end
    if buttid~=nil then resultButt = pad.down(buttid) end
    
    result = resultKey or resultPad
    if result then
        if keyid~=nil then keyDown = self.keysdown[keyid] end
        if buttid~=nil then buttDown = self.keysdown[buttid] end
        
        result = true
        if keyDown or buttDown then result = false end
        
        -- self.keysdown[keyid] = true
        if keyid~=nil then self.keysdown[keyid]=true end
        if buttid~=nil then self.keysdown[buttid]=true end
    else
        -- self.keysdown[keyid] = false
        if keyid~=nil then self.keysdown[keyid]=false end
        if buttid~=nil then self.keysdown[buttid]=false end
    end
    
    return result
end

function controller:keypressed(action)
    action = string.upper(action)
    
    if action=="UP" then
        keyid  = keyUp
        buttid = btnUp
    elseif action=="DOWN" then
        keyid  = keyDown
        buttid = btnDown
    elseif action=="LEFT" then
        keyid  = keyLeft
        buttid = btnLeft
    elseif action=="RIGHT" then
        keyid  = keyRight
        buttid = btnRight
    elseif action=="JUMP" then
        keyid  = keyJump
        buttid = btnJump
    elseif action=="SHOOT" then
        keyid  = keyShoot
        buttid = btnShoot
    elseif action=="ESC" then
        keyid  = key.ESCAPE
        buttid = btnPause
    end
    
    return key.press(keyid) or pad.press(buttid)
end