/*
 * Copyright (c) 2020 The ZMK Contributors
 *
 * SPDX-License-Identifier: MIT
 */

#include <behaviors.dtsi>
#include <dt-bindings/zmk/keys.h>
#include <dt-bindings/zmk/bt.h>
#include <dt-bindings/zmk/outputs.h>
#include <dt-bindings/zmk/ext_power.h>
#include <dt-bindings/led/led.h>
#include <dt-bindings/zmk/backlight.h>
#include <dt-bindings/zmk/rgb.h>
#include <dt-bindings/zmk/reset.h>

&led_strip {
    chain-length = <27>;
};

/ {
    combos {
        compatible = "zmk,combos";
        // Top + home row combos = numbers (like ecosteno)
        combo_1 { key-positions = <1 13>; bindings = <&kp N1>; layers = <0 2>; };
        combo_2 { key-positions = <2 14>; bindings = <&kp N2>; layers = <0 2>; };
        combo_3 { key-positions = <3 15>; bindings = <&kp N3>; layers = <0 2>; };
        combo_4 { key-positions = <4 16>; bindings = <&kp N4>; layers = <0 2>; };
        combo_5 { key-positions = <5 17>; bindings = <&kp N5>; layers = <0 2>; };
        combo_6 { key-positions = <6 18>; bindings = <&kp N6>; layers = <0 2>; };
        combo_7 { key-positions = <7 19>; bindings = <&kp N7>; layers = <0 2>; };
        combo_8 { key-positions = <8 20>; bindings = <&kp N8>; layers = <0 2>; };
        combo_9 { key-positions = <9 21>; bindings = <&kp N9>; layers = <0 2>; };
        combo_0 { key-positions = <10 22>; bindings = <&kp N0>; layers = <0 2>; };
        // K+L (QWRT) / E+O (CARP) = BSPC
        combo_bspc { key-positions = <20 21>; bindings = <&kp BSPC>; layers = <0 2>; };
        // J+K (QWRT) / A+E (CARP) = ENTER
        combo_enter { key-positions = <19 20>; bindings = <&kp RET>; layers = <0 2>; };
        // SPC+ALT = ENTER (all layers except steno)
        combo_thumb_enter { key-positions = <39 40>; bindings = <&kp RET>; layers = <0 1 2 4>; };
    };

    keymap {
        compatible = "zmk,keymap";

        carpalx_layer {
            display-name = "CARP";
            bindings = <
&kp GRAVE  &kp Q   &kp G   &kp M     &kp L     &kp W        &kp Y     &kp F    &kp U      &kp B    &kp SEMI  &to 1
&kp TAB    &kp D   &kp S   &kp T     &kp N     &kp R        &kp I     &kp A    &kp E      &kp O    &kp H     &kp SQT
&kp LSHFT  &kp Z   &kp X   &kp C     &kp V     &kp J        &kp K     &kp P    &kp COMMA  &kp DOT  &kp FSLH  &kp RSHFT
                            &kp ESC   &kp LCTRL &kp LGUI     &kp SPACE &kp RALT &mo 1
            >;
        };

        hub_layer {
            display-name = "HUB";
            // -----------------------------------------------------------------------------------------
            // | STUDIO| BT_CLR| BT0  | BT1  | BT2  | BT3  |   | HOME |  UP  | END  |  {   |  }   | DEL  |
            // | ->BAS | CTRL  | SHFT | ALT  | GUI  |->RAI |   | LEFT | DOWN | RIGHT|  [   |  ]   |->STN |
            // | RESET | RGB_T | RGB_E| RGB- | RGB+ |      |   |  (   |  )   |  \   |  `   |  ~   |      |
            //                        |      |      |      |   |      |      |      |
            // Press LSHFT position (->BAS) to go back to base

            bindings = <
&bt BT_CLR      &bt BT_SEL 0        &bt BT_SEL 1    &bt BT_SEL 2    &bt BT_SEL 3    &bt BT_SEL 4    &kp PLUS  &kp LBKT    &kp UP      &kp RBKT      &kp ASTRK     &kp EQUAL
&out OUT_TOG    &none               &none           &none           &none           &none           &kp MINUS &kp LEFT    &kp DOWN    &kp RIGHT     &kp FSLH      &kp BSLH
&kp LSHFT       &kp Z               &kp X           &kp C           &kp V           &none           &to 0     &to 2       &to 3       &to 4         &to 5         &kp RSHFT
                                                    &kp ESC         &kp LCTRL       &kp LGUI        &kp SPACE &kp RALT    &none
            >;
        };

        qwerty_layer {
            display-name = "QWRT";
            bindings = <
&kp GRAVE  &kp Q   &kp W   &kp E     &kp R     &kp T        &kp Y     &kp U    &kp I      &kp O    &kp P     &to 1
&kp TAB    &kp A   &kp S   &kp D     &kp F     &kp G        &kp H     &kp J    &kp K      &kp L    &kp SEMI  &kp SQT
&kp LSHFT  &kp Z   &kp X   &kp C     &kp V     &kp B        &kp N     &kp M    &kp COMMA  &kp DOT  &kp FSLH  &kp RSHFT
                            &kp ESC   &kp LCTRL &kp LGUI     &kp SPACE &kp RALT &mo 1
            >;
        };

        steno_layer {
            display-name = "STEN";
            // Plover keyboard mode layout
            // # bar:        1  2  3  4  5  |  6  7  8  9  0
            // Upper steno:  S  T  P  H  *  |  *  F  P  L  T  D
            // Lower steno:  S  K  W  R  *  |  *  R  B  G  S  Z
            // Vowels:          A  O        |     E  U
            bindings = <
&none      &kp N1  &kp N2  &kp N3  &kp N4  &kp N5       &kp N6  &kp N7  &kp N8  &kp N9    &kp N0    &to 1
&none      &kp Q   &kp W   &kp E   &kp R   &kp T        &kp Y   &kp U   &kp I   &kp O     &kp P     &kp LBKT
&none      &kp A   &kp S   &kp D   &kp F   &kp G        &kp H   &kp J   &kp K   &kp L     &kp SEMI  &kp SQT
                            &none   &kp C   &kp V        &kp N   &kp M   &none
            >;
        };

        fps_layer {
            display-name = "FPS";
            // WASD shifted right 1 col for ortho comfort
            // T, G, B remapped to left edge/thumbs
            // Right side = macros (empty for now)
            bindings = <
&mt ESC N1 &kp N2    &kp Q   &kp W   &mt N3 E &mt N4 R    &kp N5  &kp N6  &kp N7  &kp N8  &kp N9    &to 1
&kp TAB    &kp LSHFT &kp A   &kp S   &kp D   &kp G        &none   &none   &none   &none   &none     &none
&kp M      &kp Z     &kp X   &kp C   &kp V   &kp F        &none   &none   &none   &none   &none     &none
                              &mt LALT T &kp LCTRL &kp SPACE  &none   &none   &mo 1
            >;
        };

        extra_1 {
            status = "reserved";
        };
    };
};
