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

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

/ {
    keymap {
        compatible = "zmk,keymap";

        layer_0 {
            display-name = "Layer 0";

            // ----------------------------------------------------------------------------------------
            // |  ESC  |  1   |  2  |   3  |   4   |  5  |   |  6  |   7   |  8  |  9  |  0   | BSPC  |
            // |  TAB  |  Q   |  W  |   E  |   R   |  T  |   |  Y  |   U   |  I  |  O  |  P   |   {   |
            // | SHIFT |  A   |  S  |   D  |   F   |  G  |   |  H  |   J   |  K  |  L  |  ;   |   '   |
            // | CTRL  |  Z   |  X  |   C  |   V   |  B  |   |  N  |   M   |  ,  |  .  |  /   | SHIFT |
            // |       |      |     | LALT | MO(1) | SPC |   | SPC | MO(2) | GUI |     |      |       |

            bindings = <
&kp ESC   &kp N1  &kp N2  &kp N3   &kp N4 &kp N5        &kp N6    &kp N7  &kp N8  &kp N9  &kp N0      &kp BSPC
&kp TAB   &kp Q   &kp W   &kp E    &kp R  &mt GRAVE T   &kp Y     &kp U   &kp I   &kp O   &mt LBKT P  &kp LBRC
&kp LSHFT &kp A   &kp S   &kp D    &kp F  &kp G         &kp H     &kp J   &kp K   &kp L   &kp SEMI    &kp SQT
&kp LCTRL &kp Z   &kp X   &kp C    &kp V  &kp B         &kp N     &mt RBKT M  &kp COMMA  &kp DOT  &kp FSLH  &kp RSHFT
                          &kp LALT &mo 1  &kp SPACE     &kp SPACE &mo 2   &kp RGUI
            >;
        };

        layer_1 {
            display-name = "Layer 1";

            // -------------------------------------------------------------------------------------
            // |  F1   |  F2  |  F3 |  F4 |  F5 |  F6 |      |  F7 |  F8 |  F9 | F10 | F11 |  F12  |
            // |  TAB  |  ~   |  (  |  )  |  *  |  /  |      |  7  |  8  |  9  |  0  | INS |  DEL  |
            // | SHIFT |      |  [  |  ]  |  +  |  -  |      |  4  |  5  |  6  |  .  |  /  |   \   |
            // | CTRL  |      |  {  |  }  |  <  |  >  |      |  1  |  2  |  3  |  =  |  _  | SHIFT |
            // |       |      | END | ALT |     | RET |      | RET |     | GUI |     |     |       |

            bindings = <
&kp F1     &kp F2     &kp F3    &kp F4    &kp F5    &kp F6      &kp F7   &kp F8  &kp F9  &kp F10  &kp F11  &kp F12
&kp TAB    &kp TILDE  &kp LPAR  &kp RPAR  &kp STAR  &kp FSLH    &kp N7   &kp N8  &kp N9  &kp N0  &kp INS  &kp DEL
&kp TAB    &none      &kp LBKT  &kp RBKT  &kp PLUS  &kp MINUS   &kp N4   &kp N5  &kp N6  &kp DOT  &kp FSLH  &kp BSLH
&kp LSHFT  &none      &kp LBRC  &kp RBRC  &kp LT    &kp GT      &kp N1   &kp N2  &kp N3  &kp EQUAL  &kp UNDER  &kp RSHFT
                                &kp LALT  &trans    &kp RET     &kp RET  &trans  &kp RGUI
            >;
        };

        layer_2 {
            display-name = "Layer 2";

            // ------------------------------------------------------------------------------------------
            // |       |      |      |     |     |     |      |     |      |      |      |       |       |
            // |       |      |      |     |  UP |     |      |     |      |      |      | PSCRN | PAUSE |
            // |       |      |      | LEFT|     |RIGHT|      |     |      |      |      | NUMLK | SCRLK |
            // | SHIFT |      |      |     | DOWN|     |      |     |      |      |      | MENU  | SHIFT |
            // |       |      |      | ALT |     | RET |      | RET |      | CAPS |      |       |       |

            bindings = <
&none     &none  &none  &none     &none     &none          &none    &none   &none   &none  &none  &none
&none     &none  &none  &none     &kp UP    &none          &none    &none   &none   &none &kp PSCRN  &kp PAUSE_BREAK
&none     &none  &none  &kp LEFT  &none     &kp RIGHT      &none    &none   &none   &none &kp KP_NUM  &kp SLCK
&kp LSHFT &none  &none  &none     &kp DOWN  &none          &none    &none   &none   &none  &kp K_MENU  &kp RSHFT
                        &kp LALT  &trans    &kp RET        &kp RET  &trans  &kp CAPS
            >;
        };

        layer_3 {
            display-name = "Layer 3";

            // -----------------------------------------------------------------------------------
            // | BTCLR | BT0 | BT1 | BT2  | BT3 | BT4 |      |     |     |     |     |     |     |
            // |       |     |     |      |     |     |      |     |     |     |     |     |     |
            // |       |     |     |      |     |     |      |     |     |     |     |     |     |
            // |       |     |     |      |     |     |      |     |     |     |     |     |     |
            // |       |     |     | CALC |     |     |      |     |     |     |     |     |     |

            bindings = <
&bt BT_CLR  &bt BT_SEL 0  &bt BT_SEL 1  &bt BT_SEL 2  &bt BT_SEL 3  &bt BT_SEL 4  &none  &none  &none  &none  &none  &none
&none  &none  &none  &none  &none  &none    &none  &none  &none  &none  &none  &none
&none  &none  &none  &none  &none  &none    &none  &none  &none  &none  &none  &none
&none  &none  &none  &none  &none  &none    &none  &none  &none  &none  &none  &none
                &kp K_CALC  &none  &none    &none  &none  &none
            >;
        };
    };

    conditional_layers {
        compatible = "zmk,conditional-layers";

        layer_3 {
            if-layers = <1 2>;
            then-layer = <3>;
        };
    };

    combos {
        compatible = "zmk,combos";

        combo_bt_clr_left {
            timeout-ms = <50>;
            layers = <1>;
            key-positions = <0 1 2 3>;
            bindings = <&bt BT_CLR>;
        };

        combo_bt_clr_right {
            timeout-ms = <50>;
            layers = <2>;
            key-positions = <11 10 9 8>;
            bindings = <&bt BT_CLR>;
        };
    };
};
