import { KeymapKey, KeymapLayer } from "@keymapkit/ui";
import { keyLegends } from "./keyLegends";
import { keyInfo } from "./keyInfo";
import { keySelection } from "./keySelection";

export function newQwertyKey(
  name: string,
  physicalKeyId: string,
  textLegend?: string,
) {
  return new KeymapKey({
    name,
    textLegend: textLegend || name.toUpperCase(),
    id: physicalKeyId,
    info: keyInfo.qwerty,
    selection: keySelection.qwerty,
  });
}

// #region Left Finger QWERTY
// Qwerty keys, numbers, and symbols, MOSTLY in their positions from a normal keyboard.
export const layer0LeftFingerQwerty = [
  new KeymapKey({
    name: "=+",
    id: "l-f-1-1",
    info: [
      `
        This placement takes a little getting used to,
        but it&apos;s normal for other split keyboard layouts like the ErgoDox, and honestly isn&apos;t too bad.
        It helps me to remember that it mirrors <keymap-indicator id="r-f-25-1"><kbd>-_</kbd></keymap-indicator>
        on the opposite side of the board.
      `,
    ],
  }),
  newQwertyKey("1", "l-f-6-1"),
  newQwertyKey("2", "l-f-10-1"),
  newQwertyKey("3", "l-f-14-1"),
  newQwertyKey("4", "l-f-18-1"),
  newQwertyKey("5", "l-f-22-1"),

  new KeymapKey({
    name: "`~",
    id: "l-f-1-5",
    info: [
      `
              Slightly different from the location of this key on a normal QWERTY
              keyboard. You could theoretically swap it with
              <keymap-indicator id="l-f-1-1">
                <kbd>=/+</kbd>
              </keymap-indicator>
              to keep the QWERTY position, but I decided I preferred that key to
              mirror <kbd>-/_</kbd> on the opposite side of the keyboard so I
              would remember it more easily. That leaves this space as the best
              fit for this key. Additionally, while this is the location for the
              tab key on a normal keyboard, that key is much better suited for a
              <keymap-indicator id="l-f-26-5">different location</keymap-indicator>
              under a stronger finger than the pinky.
            `,
    ],
  }),
  newQwertyKey("q", "l-f-6-5"),
  newQwertyKey("w", "l-f-10-5"),
  newQwertyKey("e", "l-f-14-5"),
  newQwertyKey("r", "l-f-18-5"),
  newQwertyKey("t", "l-f-22-5"),

  newQwertyKey("a", "l-f-6-9"),
  newQwertyKey("s", "l-f-10-9"),
  newQwertyKey("d", "l-f-14-9"),
  newQwertyKey("f", "l-f-18-9"),
  newQwertyKey("g", "l-f-22-9"),

  new KeymapKey({
    name: "[{",
    id: "l-f-1-13",
    selection: keySelection.brackets,
    info: keyInfo.brackets,
  }),
  newQwertyKey("z", "l-f-6-13"),
  newQwertyKey("x", "l-f-10-13"),
  newQwertyKey("c", "l-f-14-13"),
  newQwertyKey("v", "l-f-18-13"),
  newQwertyKey("b", "l-f-22-13"),
];
// #endregion Left Finger QWERTY

// #region Left Finger Mods
// THe left finger mods are the keys that are transparent on most other layers.
export const layer0LeftFingerMods = [
  new KeymapKey({
    id: "l-f-26-1",
    ...keyLegends.escape,
    info: [
      `Reduces pinky strain compared to its QWERTY position, which is on the far left of the keyboard above any available Advantage360 key position.`,
      `<kbd>esc</kbd> is used often enough in computing I think any RSI-afflicted user would benefit from this new location, but I recommend it especially to heavy vim users.`,
      `This placement is easy to remember, particularly because the default location is unavailable to confuse you.`,
    ],
  }),
  new KeymapKey({
    id: "l-f-26-5",
    ...keyLegends.tab,
    info: [
      `
        Reduces pinky strain over its default location. This is a very
        natural place for this key, and it&apos;s very easy to hit.
      `,
      `
        A good location for <kbd>tab</kbd> must take into account the location of other keys.
        In this layout, I can easily chord with the various modifier keys:
        <keymap-indicator id="l-f-26-9"><kbd>cmd</kbd></keymap-indicator>,
        <keymap-indicator id="l-f-18-17"><kbd>alt</kbd></keymap-indicator>,
        <keymap-indicator id="l-t-9-9"><kbd>ctrl</kbd></keymap-indicator>,
        or <keymap-indicator id="l-t-1-5"><kbd>shift</kbd></keymap-indicator>,
        all from the left hand.
      `,
    ],
  }),
  new KeymapKey({
    id: "l-f-26-9",

    ...keyLegends.aerospace,
    selection: keySelection.aerospace,
    info: keyInfo.aerospace,
  }),
  new KeymapKey({
    id: "l-f-18-17",
    ...keyLegends.option,
    selection: keySelection.option,
    info: keyInfo.option,
  }),
];
// #endregion Left Finger Mods

// #region Left Finger Misc
// Keys that are not in normal QWERTY positions and are not transparent on other layers
export const layer0LeftFingerMisc = [
  new KeymapKey({
    id: "l-f-1-9",
    ...keyLegends.whisperInvoke,
    info: keyInfo.whisperInvoke,
  }),
  new KeymapKey({ id: "l-f-1-17", name: "", unset: true, info: [] }),
  new KeymapKey({ unset: true, name: "", id: "l-f-6-17", info: [""] }),
  new KeymapKey({
    id: "l-f-10-17",
    ...keyLegends.backspace,
    info: [
      `I keep a backspace key here because it's convenient to be able to hit backspace with my left hand while my right hand is on the mouse,
      but when the right hand is on the keyboard, I use its <keymap-indicator id="r-f-1-1"><kbd>backspace</kbd></keymap-indicator> instead.`,
    ],
  }),
  new KeymapKey({
    id: "l-f-14-17",
    ...keyLegends.forwardDelete,
    info: [`As good a place as any for forward delete.`],
  }),
];
// #endregion Left Finger Misc

// #region Left Thumb
export const layer0LeftThumb = [
  new KeymapKey({
    id: "l-t-5-1",
    ...keyLegends.command,
    selection: keySelection.command,
    info: keyInfo.command,
  }),
  new KeymapKey({
    id: "l-t-9-1",
    ...keyLegends.gridcraft,
    selection: keySelection.gridcraft,
    info: keyInfo.gridcraft,
  }),
  new KeymapKey({
    id: "l-t-1-5",
    ...keyLegends.shift,
    info: [
      `
            Moving this key under my thumb was the single most important key
            remap to address my pinky strain.
          `,
      `
            I can easily reach any key necessary &mdash; the longest reach is to
            <keymap-indicator id="l-f-1-1">
              <kbd>=/+</kbd>
            </keymap-indicator>
            , and I can do that easily, repeatedly, painlessly, and with room to
            spare.
          `,
      `
        I did have to remap the normal locations for
        <keymap-indicator id="l-f-1-13"><kbd>left shift</kbd></keymap-indicator> and
        <keymap-indicator id="r-f-25-13"><kbd>right shift</kbd></keymap-indicator>
        to other keys in order to train my hands to use this new location.
        After a day or two the new locations felt very fast and natural.
      `,
    ],
  }),
  new KeymapKey({
    id: "l-t-5-5",
    ...keyLegends.navLayer,
    info: keyInfo.navLayer,
  }),
  new KeymapKey({
    id: "l-t-9-5",
    ...keyLegends.keypadLayer,
    info: keyInfo.keypadLayer,
  }),
  new KeymapKey({
    id: "l-t-9-9",
    ...keyLegends.control,
    selection: keySelection.control,
    info: keyInfo.control,
  }),
];
// #endregion Left Thumb

// #region Right Finger QWERTY
// Qwerty keys, numbers, and symbols, MOSTLY in their positions from a normal keyboard.
export const layer0RightFingerQwerty = [
  newQwertyKey("6", "r-f-5-1"),
  newQwertyKey("7", "r-f-9-1"),
  newQwertyKey("8", "r-f-13-1"),
  newQwertyKey("9", "r-f-17-1"),
  newQwertyKey("0", "r-f-21-1"),
  newQwertyKey("-_", "r-f-25-1"),

  newQwertyKey("y", "r-f-5-5"),
  newQwertyKey("u", "r-f-9-5"),
  newQwertyKey("i", "r-f-13-5"),
  newQwertyKey("o", "r-f-17-5"),
  newQwertyKey("p", "r-f-21-5"),
  newQwertyKey("\\|", "r-f-25-5"),

  newQwertyKey("h", "r-f-5-9"),
  newQwertyKey("j", "r-f-9-9"),
  newQwertyKey("k", "r-f-13-9"),
  newQwertyKey("l", "r-f-17-9"),
  newQwertyKey(";:", "r-f-21-9"),
  newQwertyKey("'\"", "r-f-25-9"),

  newQwertyKey("n", "r-f-5-13"),
  newQwertyKey("m", "r-f-9-13"),
  newQwertyKey(",<", "r-f-13-13"),
  newQwertyKey(".>", "r-f-17-13"),
  newQwertyKey("/?", "r-f-21-13"),
  new KeymapKey({
    name: "]}",
    id: "r-f-25-13",
    selection: keySelection.brackets,
    info: keyInfo.brackets,
  }),
];
// #endregion Right Finger QWERTY

// #region Right Finger Mods
// The right finger mods are the keys that are transparent on most other layers.
export const layer0RightFingerMods = [
  new KeymapKey({
    id: "r-f-1-1",
    ...keyLegends.backspace,
    info: [
      `Having this very commonly used key under a strong index finger is a huge improvement over having it under a weak pinky. I apparently have a habit of hammering this key pretty hard, a fact I learned about myself very painfully on a QWERTY keyboard once each press of this key started to induce a sharp pain in my right hand.`,
      `The ErgoDox and the Advantage360 both have this key under the left thumb, and that's where I started with it too, but I found my <keymap-indicator id="l-t-5-5"><kbd>arrow layer</kbd></keymap-indicator> key was more useful there. Backspace works great here instead, and it was easy to gete used to.`,
      `I do keep a <keymap-indicator id="l-f-10-17">second</keymap-indicator> <kbd>backspace</kbd> key on the left side of the keyboard, but I use it only when my right hand is on the mouse. This one is the primary.`,
    ],
  }),
  new KeymapKey({ id: "r-f-1-5", name: "", unset: true, info: [] }),

  new KeymapKey({
    id: "r-f-1-9",
    ...keyLegends.aerospace,
    selection: keySelection.aerospace,
    info: keyInfo.aerospace,
  }),

  new KeymapKey({
    id: "r-f-9-17",
    ...keyLegends.option,
    selection: keySelection.option,
    info: keyInfo.option,
  }),
];
// #endregion Right Finger Mods

// #region Right Finger Misc
// Keys that are not in normal QWERTY positions and are not transparent on other layers
export const layer0RightFingerMisc = [
  new KeymapKey({
    id: "r-f-13-17",
    ...keyLegends.adv360Layer,
    info: keyInfo.adv360Layer,
  }),
  new KeymapKey({
    id: "r-f-17-17",
    ...keyLegends.volumeDown,
    selection: keySelection.volumes,
    info: keyInfo.volumes,
  }),
  new KeymapKey({
    id: "r-f-21-17",
    ...keyLegends.volumeUp,
    selection: keySelection.volumes,
    info: keyInfo.volumes,
  }),
  new KeymapKey({
    id: "r-f-25-17",
    ...keyLegends.volumeMute,
    selection: keySelection.volumes,
    info: keyInfo.volumes,
  }),
];
// #endregion Right Finger Misc

// #region Right Thumb
export const layer0RightThumb = [
  new KeymapKey({
    id: "r-t-1-1",
    ...keyLegends.gridcraft,
    selection: keySelection.gridcraft,
    info: keyInfo.gridcraft,
  }),
  new KeymapKey({
    id: "r-t-5-1",
    ...keyLegends.command,
    selection: keySelection.command,
    info: keyInfo.command,
  }),
  new KeymapKey({
    id: "r-t-1-5",
    ...keyLegends.functionLayer,
    info: keyInfo.functionLayer,
  }),
  new KeymapKey({
    id: "r-t-5-5",
    ...keyLegends.return,
    info: [
      `I hit this key really hard and really often, so moving it out from under a pinky finger eased pain right away.`,
      `This was easy to get used to.`,
    ],
  }),
  new KeymapKey({
    id: "r-t-9-5",
    ...keyLegends.space,
    info: [
      `A great place for <kbd>space</kbd>. I initially had mirrored <kbd>space</kbd> keys, one under each thumb, but I discovered that using just the right hand worked out perfectly fine, and it freed up a valuable thumb key for something else on the left hand, so now I just use this one.`,
    ],
  }),
  new KeymapKey({
    id: "r-t-1-9",
    ...keyLegends.control,
    selection: keySelection.control,
    info: keyInfo.control,
  }),
];
// #endregion Right Thumb

// #region Layer definition
export const layer0base = KeymapLayer.fromKeyList({
  displayName: "Base Layer",
  shortName: "Base",
  welcome: [
    `Welcome to my keymap.`,
    `Select a key from the board above to learn more about it.`,
  ],
  keys: [
    ...layer0LeftFingerQwerty,
    ...layer0LeftFingerMods,
    ...layer0LeftFingerMisc,
    ...layer0LeftThumb,
    ...layer0RightFingerQwerty,
    ...layer0RightFingerMods,
    ...layer0RightFingerMisc,
    ...layer0RightThumb,
  ],
});
// #endregion Layer definition
