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

#define DEF 0
#define NUM 2
#define SYM 1
#define META 3
#define MOUSE 4
#define FN 5
#define FAST 6
#define SLOW 7

#include "zmk-helpers/key-labels/42.h"
#include <behaviors.dtsi>
#include <dt-bindings/zmk/bt.h>
#include <dt-bindings/zmk/keys.h>
#include "zmk-helpers/helper.h"
#include "mouse.dtsi"
#include "combos.dtsi"

// TODO: 
// 1. Improve feel of the smart mouse
// 2. Fix the timing issues with the combos;
//    there aren't any false positives, 
//    but actually false negatives 
// 3. Find a more elegant way to do the mouse speed 
//    modifiers that doesn't require two empty layers?



&sk {  // sticky-key config
    release-after-ms = <900>;  // release after 0.9s
    quick-release;             // no double capitalization when rolling keys
};

// Smart Shift & Num Layer (explanation)
// There's a left and right shift key
// Hold one: SHIFT
// Tap one: Sticky SHIFT
// Double tap same one: CAPS WORD
// Hold BOTH: NUM layer
ZMK_MOD_MORPH(left_smart_shift_num,
    bindings = <&left_smart_shft>, <&mo NUM>;
    mods = <(MOD_RSFT)>;
)
ZMK_MOD_MORPH(right_smart_shift_num,
    bindings = <&right_smart_shft>, <&mo NUM>;
    mods = <(MOD_LSFT)>;
)
ZMK_MOD_MORPH(left_smart_shft,
    bindings = <&sk LSHFT>, <&caps_word>;
    mods = <(MOD_LSFT)>;
)
ZMK_MOD_MORPH(right_smart_shft,
    bindings = <&sk RSHFT>, <&caps_word>;
    mods = <(MOD_RSFT)>;
)
// ZMK_BEHAVIOR(long_sk, sticky_key,
//     bindings = <&kp>;
//     quick-release;
//     release-after-ms = <2000>;
// )
ZMK_HOLD_TAP(ht_sk,
    flavor = "hold-preferred";
    bindings = <&kp>, <&lsk>;
    tapping-term-ms = <200>;
)



ZMK_MOD_MORPH(smart_mod,
bindings = <&ht_sk LALT LGUI>, <&kp LGUI>;
mods = <(MOD_LSFT)>;
)

// deprecrstated, unused
ZMK_TAP_DANCE(alt_mod,
bindings = <&ht_sk LALT LGUI>, <&kp LGUI>;
)

// TODO: Will setting the second bind to &tog MOUSE let 
// smart_mouse be a toggle while retaining the smart behavior too?
ZMK_TRI_STATE(
    smart_mouse, bindings = <&tog MOUSE>, <&tog MOUSE>, <&tog MOUSE>;
    ignored-key-positions =
        <RM1 RM2 RM3 RT2 RB1 RB2 RB3 RH1 RH2 LM1 LM2>;
    ignored-layers = <MOUSE FAST SLOW>;)

ZMK_HOLD_TAP(ht_mo,
    flavor = "hold-preferred";
    tapping-term-ms = <200>;
    /* quick-tap-ms = <200>; */
    /* require-prior-idle-ms = <125>; */
    bindings = <&mo>, <&kp>;
)
ZMK_HOLD_TAP(ht_tog,
    flavor = "hold-preferred";
    tapping-term-ms = <200>;
    bindings = <&kp>, <&tog>;
)
&caps_word {  // mods deactivate caps-word, requires PR #1451
    /delete-property/ ignore-modifiers;
    /delete-property/ ignore-numbers;
    continue-list = <UNDERSCORE MINUS BACKSPACE DELETE>;
};


/ {
  behaviors {
  rpi: require_prior_idle {
      compatible = "zmk,behavior-hold-tap";
      #binding-cells = <2>;
      flavor = "tap-preferred";
      tapping-term-ms = <200>;
      quick-tap-ms = <200>;
      require-prior-idle-ms = <125>;
      bindings = <&kp>, <&kp>;
  };
    lsk: long_sticky_key {
        compatible = "zmk,behavior-sticky-key";
        #binding-cells = <1>;
        bindings = <&kp>;
        release-after-ms = <2000>;
        ignore-modifiers;
        quick-release;
    };
  };
    conditional_layers {
      compatible = "zmk,conditional-layers";
      meta_layer {
          if-layers = <1 2>;
          then-layer = <3>;
      };
  };
    macros {
        dbl: double {
            compatible = "zmk,behavior-macro";
            #binding-cells = <0>;
            bindings = <&kp ESC &kp K_CANCEL>;
        };
    };
    keymap {
        compatible = "zmk,keymap";

        default_layer {
            display-name = "base";
            bindings = <

&kp ESC            &kp Q                     &kp W             &kp F              &kp P                     &kp B             &kp J         &kp L              &kp U       &kp Y     &kp SEMICOLON   &rpi LGUI TAB
&kp BSPC                 &kp A                     &kp R             &kp S              &kp T                     &kp G             &kp M         &kp N              &kp E       &kp I     &kp O           &kp SQT
&kp CAPS                 &kp Z                     &kp X             &kp C              &kp D                     &kp V             &kp K         &kp H              &kp COMMA   &kp DOT   &kp FSLH        &to FN
                                        &ht_sk LALT LA(LCTRL)         &rpi LEFT_CONTROL SPACE   &left_smart_shift_num       &right_smart_shift_num        &rpi LGUI ENTER           &mo SYM
            >;
        };

        symbol {
            display-name = "symbol";
            bindings = <

&trans   &kp AT        &kp EXCL    &kp LEFT_PARENTHESIS   &kp RIGHT_PARENTHESIS   &trans          &trans      &kp AMPS     &kp STAR    &kp EQUAL    &kp COLON     &trans
&trans   &kp CARET     &kp MINUS   &kp LEFT_BRACE         &kp RIGHT_BRACE         &kp TILDE       &kp LEFT    &kp DOWN     &kp UP      &kp RIGHT    &kp DOLLAR    &kp PIPE
&trans   &kp PERCENT   &kp UNDER   &kp LEFT_BRACKET       &kp RIGHT_BRACKET       &trans          &trans      &kp HASH     &kp GRAVE   &kp PLUS     &kp BSLH      &trans
                                   &trans                 &trans                  &trans          &trans      &trans       &trans
            >;
        };
        num {
            display-name = "num";
            bindings = <

&trans   &trans   &trans   &trans   &trans   &trans       &trans     &trans     &trans   &trans      &trans   &trans
&trans   &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     &trans   &trans      &trans   &trans
                         &trans   &trans   &trans       &trans     &trans     &trans
            >;
        };
        meta_layer {
            display-name = "meta";
            bindings = <

&bt BT_CLR   &trans   &trans   &trans       &trans       &trans       &trans   &trans   &trans   &trans   &trans   &trans
&trans       &trans   &trans   &bt BT_PRV   &bt BT_NXT   &trans       &trans   &trans   &trans   &trans   &trans   &trans
&trans       &trans   &trans   &trans       &trans       &trans       &trans   &trans   &trans   &trans   &trans   &trans
                             &trans       &trans       &trans       &trans   &trans   &trans
            >;
        };
        mouse_layer {
            display-name = "mouse";
            bindings = <

&trans    &trans   &trans   &trans     &trans     &trans       &trans   &trans           &mmv MOVE_UP     &trans            &trans   &trans
&trans    &trans   &trans   &mo SLOW   &mo FAST   &trans       &trans   &mmv MOVE_LEFT   &mmv MOVE_DOWN   &mmv MOVE_RIGHT   &trans   &trans
&to DEF   &trans   &trans   &trans     &trans     &trans       &trans   &mkp LCLK        &mkp MCLK        &mkp RCLK         &trans   &trans
                          &trans     &trans     &trans       &trans   &msc SCRL_DOWN   &msc SCRL_UP
            >;
        };
        fn_layer {
            display-name = "fn";
            bindings = <

  &trans   &kp F6    &kp F7    &kp F8   &kp F9   &kp F10       &trans   &trans         &trans         &trans       &trans   &trans
  &trans   &kp F1    &kp F2    &kp F3   &kp F4   &kp F5        &trans   &kp C_VOL_DN   &kp C_VOL_UP   &kp C_MUTE   &trans   &trans
  &trans   &kp F11   &kp F12   &trans   &trans   &trans        &trans   &kp C_NEXT     &kp C_PREV     &kp C_PP     &trans   &to DEF
                               &trans   &trans   &trans        &trans   &trans         &trans
            >;
        };
        fast_layer {
            display-name = "fast";
            bindings = <

  &trans   &trans   &trans   &trans   &trans   &trans       &trans   &trans   &trans   &trans   &trans   &trans
  &trans   &trans   &trans   &trans   &trans   &trans       &trans   &trans   &trans   &trans   &trans   &trans
  &trans   &trans   &trans   &trans   &trans   &trans       &trans   &trans   &trans   &trans   &trans   &trans
                             &trans   &trans   &trans       &trans   &trans   &trans
            >;
        };
        slow_layer {
            display-name = "slow";
            bindings = <

  &trans   &trans   &trans   &trans   &trans   &trans       &trans   &trans   &trans   &trans   &trans   &trans
  &trans   &trans   &trans   &trans   &trans   &trans       &trans   &trans   &trans   &trans   &trans   &trans
  &trans   &trans   &trans   &trans   &trans   &trans       &trans   &trans   &trans   &trans   &trans   &trans
                             &trans   &trans   &trans       &trans   &trans   &trans
            >;
        };
    };
};
