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

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

// Layers
#define QW_M    0       // Main
#define QW_L    1       // Lower
#define QW_R    2       // Raise
#define QW_A    3       // Adjust

#define QC_N    4       // Number Pad
#define QC_B    5       // Firmware


// Short versions
#define BT0         BT_SEL 0
#define BT1         BT_SEL 1
#define BT2         BT_SEL 2
#define BT3         BT_SEL 3
#define BT4         BT_SEL 4

#define BOOTLDR     &bootloader

#define RGBON   &rgb_ug RGB_ON
#define RGBOFF  &rgb_ug RGB_OFF
#define RGBTOG  &rgb_ug RGB_TOG
#define RGBHUI  &rgb_ug RGB_HUI
#define RGBHUD  &rgb_ug RGB_HUD
#define RGBSAI  &rgb_ug RGB_SAI
#define RGBSAD  &rgb_ug RGB_SAD
#define RGBBRI  &rgb_ug RGB_BRI
#define RGBBRD  &rgb_ug RGB_BRD
#define RGBEFF  &rgb_ug RGB_EFF


/*
 * Assign the cs-gpios pin to 4.
 * Uncomment these next few lines if implementing nice!view Displays.
 */
//nice_view_spi: &spi0 {
//  cs-gpios = <&pro_micro 4 GPIO_ACTIVE_HIGH>;
//};


/ {
    conditional_layers {
        compatible = "zmk,conditional-layers";
        adjust_layer {
            if-layers = <QW_L QW_R>;
            then-layer = <QW_A>;
        };
    };

    combos {
        compatible = "zmk,combos";

        combo_esc {
            timeout-ms = <50>;
            key-positions = <0 1>;
            layers = <QW_M>;
            bindings = <&kp ESC>;
        };
    };

    keymap {
        compatible = "zmk,keymap";

        default_layer {
            display-name = " QWERTY";
            bindings = <
&kp Q      &kp W      &kp E      &kp R      &kp T                            &kp Y      &kp U      &kp I      &kp O      &kp P
&kp A      &kp S      &kp D      &kp F      &kp G                            &kp H      &kp J      &kp K      &kp L      &kp SEMI
&mt LSHFT Z &kp X      &kp C      &kp V      &kp B     &mo QC_N    &kp RGUI   &kp N      &kp M      &kp COMMA  &kp DOT    &mt RSHFT FSLH
                  &kp LALT   &kp LCTRL  &lt 1 RET  &lt 2 MINUS    &lt 2 EQUAL  &lt 1 SPACE  &kp BSPC   &mo QC_B
            >;

            sensor-bindings = <&inc_dec_kp C_VOL_DN C_VOL_UP>;
        };

        lower_layer {
            display-name = " Lower";
            bindings = <
&kp N1     &kp N2     &kp N3     &kp N4     &kp N5                           &kp N6     &kp N7     &kp N8     &kp N9     &kp N0
&trans     &trans     &trans     &trans     &trans                           &trans     &trans     &trans     &trans     &kp SQT
&kp LSHFT  &trans     &trans     &trans     &trans     &trans     &trans     &trans     &trans     &trans     &trans     &kp RSHFT
                      &trans     &trans     &trans     &trans     &trans     &trans     &trans     &trans
            >;

            sensor-bindings = <&inc_dec_kp C_VOL_DN C_VOL_UP>;
        };

        raise_layer {
            display-name = " Raise";
            bindings = <
&kp TAB    &trans     &trans     &trans     &trans                           &kp PG_UP  &kp HOME   &kp UP     &kp END    &kp BSLH
&kp CAPS   &trans     &trans     &trans     &trans                           &kp PG_DN  &kp LEFT   &kp DOWN   &kp RIGHT  &kp GRAVE
&trans     &trans     &trans     &trans     &trans     &trans     &trans     &trans     &trans     &trans     &trans     &mt RSHFT TILDE
                      &trans     &trans     &trans     &trans     &trans     &trans     &kp DEL    &trans
            >;

            sensor-bindings = <&inc_dec_kp C_VOL_DN C_VOL_UP>;
        };

        adjust_layer {
            display-name = " Adjust";
            bindings = <

&kp F1     &kp F2     &kp F3     &kp F4     &kp F5                           &trans     &trans     &trans     &trans     &trans
&kp F6     &kp F7     &kp F8     &kp F9     &kp F10                          &trans     &trans     &trans     &trans     &trans
&kp F11    &kp F12    &trans     &trans     &trans     &trans     &trans     &trans     &trans     &trans     &trans     &trans
                      &trans     &trans     &trans     &trans     &trans     &trans     &trans     &trans
            >;

            sensor-bindings = <&inc_dec_kp C_VOL_DN C_VOL_UP>;
        };

        numpad_layer {
            display-name = " NumPad";
            bindings = <

&trans     &none      &none      &none      &none                            &kp SLASH  &kp N7     &kp N8     &kp N9     &kp MINUS
RGBON      RGBTOG     RGBHUI     RGBSAI     RGBBRI                           &kp ASTRK  &kp N4     &kp N5     &kp N6     &kp PLUS
RGBOFF     RGBEFF     RGBHUD     RGBSAD     RGBBRD     &trans     &trans     &none      &kp N1     &kp N2     &kp N3     &kp EQUAL
                      &trans     &trans     &trans     &trans     &trans     &kp N0     &kp DOT    &none
            >;

            sensor-bindings = <&inc_dec_kp C_VOL_DN C_VOL_UP>;
        };

        ble_layer {
            display-name = " BLE";
            bindings = <

&bt BT0    &bt BT1    &bt BT2    &bt BT3    &bt BT4                          &bt BT0    &bt BT1    &bt BT2    &bt BT3    &bt BT4
BOOTLDR    &sys_reset &trans     &trans     &trans                           &trans     &trans     &trans     &sys_reset BOOTLDR
&trans     &trans     &trans     &trans     &trans     &trans     &trans     &trans     &trans     &trans     &trans     &trans
                      &trans     &bt BT_CLR &trans     &trans     &trans     &trans     &bt BT_CLR &trans
            >;

            sensor-bindings = <&inc_dec_kp C_VOL_DN C_VOL_UP>;
        };

    };
};