import { KeymapKey, KeymapLayer } from "@keymapkit/ui";
import {
  transparentLeftFingerMods,
  transparentLeftThumbkeys,
  transparentRightFingerMods,
  transparentRightThumbkeys,
  transparentSelection,
} from "./keyTransparents";
import { keyInfo } from "./keyInfo";
import { keySelection } from "./keySelection";
import { keyLegends } from "./keyLegends";

const synergyKeys = {
  selection: keySelection.synergy,
  info: [
    `These keys are used to swap my keyboard/mouse input between <a href="https://symless.com/synergy">Synergy</a> clients.`,
  ],
};

function newSynergyKey(num: number) {
  return {
    name: `Synergy ${num}`,
    selection: synergyKeys.selection,
    htmlLegend: `<div style="border: 1px solid currentColor; padding: 0 2px; border-radius: 4px; height: 1rem; width: 1rem; text-align: center; line-height: 1rem;">${num}</div>`,
    info: [
      ...synergyKeys.info,
      `Select Synergy client ${num} by sending Ctrl+Shift+F${num}.`,
    ],
  };
}

// Show all transparent keys, EXCEPT:
// - Keys disabled on this layer.
const excludedTransparentKeys = [
  "l-f-10-17",
  "l-f-14-17",
  "r-f-9-17",
];
const transparentSelectionExceptAFew = transparentSelection.filter(
  (id) => excludedTransparentKeys.indexOf(id) === -1,
);
const transparentAllExceptAFew: KeymapKey[] = [
  ...transparentLeftFingerMods,
  ...transparentLeftThumbkeys,
  ...transparentRightFingerMods,
  ...transparentRightThumbkeys,
]
  .filter((key) => excludedTransparentKeys.indexOf(key.id) === -1)
  .map((key) => {
    return {
      ...key,
      selection: key.id === "l-f-18-17" ? [key.id] : key.selection,
      info: key.info,
    };
  });

export const layer1kp = KeymapLayer.fromKeyList({
  displayName: "Action Layer",
  shortName: "Action",
  welcome: [
    `The Action layer.`,
    `Hold the left Action key for Bluetooth profiles, output selection, and Synergy client selection keys 1–5 (Ctrl+Shift+F1–F5). The numpad is on the Function layer, not Action.`,
    `Select a key from the board above to learn more about it.`,
  ],
  layerEntryKeys: ["l-t-9-5"],
  keys: [
    // #region Left Finger Grid (l-f)
    new KeymapKey({
      name: "Clear Bluetooth Profile",
      textLegend: "BTCLR",
      id: "l-f-1-1",
      info: ["Clear the current Bluetooth profile pairing."],
    }),

    new KeymapKey({
      name: "Toggle Output",
      textLegend: "OUT",
      id: "l-f-1-5",
      info: ["Toggle between USB and Bluetooth output."],
    }),
    new KeymapKey({
      name: "Bluetooth profile 0",
      textLegend: "BT0",
      id: "l-f-6-5",
      selection: keySelection.bluetooth,
      info: [`Select Bluetooth profile 0 (profiles are numbered 0–4).`],
    }),
    new KeymapKey({
      name: "Bluetooth profile 1",
      textLegend: "BT1",
      id: "l-f-10-5",
      selection: keySelection.bluetooth,
      info: [`Select Bluetooth profile 1 (profiles are numbered 0–4).`],
    }),
    new KeymapKey({
      name: "Bluetooth profile 2",
      textLegend: "BT2",
      id: "l-f-14-5",
      selection: keySelection.bluetooth,
      info: [`Select Bluetooth profile 2 (profiles are numbered 0–4).`],
    }),
    new KeymapKey({
      name: "Bluetooth profile 3",
      textLegend: "BT3",
      id: "l-f-18-5",
      selection: keySelection.bluetooth,
      info: [`Select Bluetooth profile 3 (profiles are numbered 0–4).`],
    }),
    new KeymapKey({
      name: "Bluetooth profile 4",
      textLegend: "BT4",
      id: "l-f-22-5",
      selection: keySelection.bluetooth,
      info: [`Select Bluetooth profile 4 (profiles are numbered 0–4).`],
    }),

    new KeymapKey({ id: "l-f-6-9", ...newSynergyKey(1) }),
    new KeymapKey({ id: "l-f-10-9", ...newSynergyKey(2) }),
    new KeymapKey({
      id: "l-f-14-9",
      ...newSynergyKey(3),
    }),
    new KeymapKey({
      id: "l-f-18-9",
      ...newSynergyKey(4),
    }),
    new KeymapKey({ id: "l-f-22-9", ...newSynergyKey(5) }),
    // #endregion Left Finger Grid (l-f)

    // #region Transparent keys
    ...transparentAllExceptAFew,
    // #endregion Transparent keys
  ],
});
