import { Action, Direction, KeyMap } from './types';
import { BrowserUtils } from '../utils/BrowserUtils';

const TizenKeyMap = {
  isApplicable: () => BrowserUtils.isTizen,
  keyCodes: {
    // D-pad Up
    38: Direction.UP,
    // D-pad Down
    40: Direction.DOWN,
    // D-pad Left
    37: Direction.LEFT,
    // D-pad Right
    39: Direction.RIGHT,
    // D-pad OK
    13: Action.SELECT,
    // Back
    10009: Action.BACK,
  },
};

const WebOsKeyMap = {
  isApplicable: () => BrowserUtils.isWebOs,
  keyCodes: {
    // D-pad Up
    38: Direction.UP,
    // D-pad Down
    40: Direction.DOWN,
    // D-pad Left
    37: Direction.LEFT,
    // D-pad Right
    39: Direction.RIGHT,
    // D-pad OK
    13: Action.SELECT,
    // Back
    461: Action.BACK,
  },
};

const PlayStationKeyMap = {
  isApplicable: () => BrowserUtils.isPlayStation,
  keyCodes: {
    // D-pad Up
    38: Direction.UP,
    // D-pad Down
    40: Direction.DOWN,
    // D-pad Left
    37: Direction.LEFT,
    // D-pad Right
    39: Direction.RIGHT,
    // Cross
    13: Action.SELECT,
    // Circle
    27: Action.BACK,
  },
};

const AndroidKeyMap = {
  isApplicable: () => BrowserUtils.isAndroid,
  keyCodes: {
    // D-pad Up
    38: Direction.UP,
    // D-pad Down
    40: Direction.DOWN,
    // D-pad Left
    37: Direction.LEFT,
    // D-pad Right
    39: Direction.RIGHT,
    // D-pad OK
    13: Action.SELECT,
    // Back
    27: Action.BACK,
  },
};

const HisenseKeyMap = {
  isApplicable: () => BrowserUtils.isHisense,
  keyCodes: {
    // D-pad Up
    38: Direction.UP,
    // D-pad Down
    40: Direction.DOWN,
    // D-pad Left
    37: Direction.LEFT,
    // D-pad Right
    39: Direction.RIGHT,
    // OK
    13: Action.SELECT,
    // Back
    8: Action.BACK,
  },
};

const XboxKeyMap = {
  isApplicable: () => BrowserUtils.isXbox,
  keyCodes: {
    // D-pad Up
    203: Direction.UP,
    211: Direction.UP,
    // D-pad Down
    204: Direction.DOWN,
    212: Direction.DOWN,
    // D-pad Left
    205: Direction.LEFT,
    214: Direction.LEFT,
    // D-pad Right
    206: Direction.RIGHT,
    213: Direction.RIGHT,
    // A
    195: Action.SELECT,
  },
};

// Default key map used on desktops
const DefaultKeyMap = {
  // Arrow Up
  38: Direction.UP,
  // Arrow Down
  40: Direction.DOWN,
  // Arrow Left
  37: Direction.LEFT,
  // Arrow Right
  39: Direction.RIGHT,
  // Enter
  13: Action.SELECT,
  // Escape
  27: Action.BACK,
};

/**
 * Returns the matching key map for the current platform.
 */
export function getKeyMapForPlatform(): KeyMap {
  const applicableKeyMap = [WebOsKeyMap, TizenKeyMap, PlayStationKeyMap, HisenseKeyMap, AndroidKeyMap, XboxKeyMap].find(
    keyMap => keyMap.isApplicable(),
  );

  if (applicableKeyMap) {
    return applicableKeyMap.keyCodes;
  } else {
    return DefaultKeyMap;
  }
}
