import { debounce, throttle } from "lodash";
import pubsub from "pubsub-js";
import hotkeys from "hotkeys-js";

export default function useHotkey(){
  hotkeys(
    [
      "space,ctrl+p,⌘+p",
      "ctrl+left,⌘+left,ctrl+right,⌘+right",
      "enter,ctrl+enter,⌘+enter",
    ].join(","),
    throttle((e, h) => {
        switch (h.key) {
        // space,ctrl+p,⌘+p
        case "space":
        case "ctrl+p":
        case "⌘+p":
          pubsub.publish("zp.pause");
          break;
        // ctrl+left,⌘+left,ctrl+right,⌘+right
        case "ctrl+left":
        case "⌘+left":
          pubsub.publish("zp.previous");
          break;
        case "ctrl+right":
        case "⌘+right":
          pubsub.publish("zp.next");
          break;
        // ctrl+up,⌘+up,ctrl+down,⌘+down
        case "ctrl+up":
        case "⌘+up":
          pubsub.publish("zp.setVolume",{step: 5});
          break;
        case "ctrl+down":
        case "⌘+down":
          pubsub.publish("zp.setVolume",{step: -5});
          break;
        // enter,ctrl+enter,⌘+enter
        case "enter":
        case "ctrl+enter":
        case "⌘+enter":
          pubsub.publish("zp.toggleSongDetail");
          break;
      }
      e.preventDefault();
    }, 500)
  );
  hotkeys(
    [
      "ctrl+up,⌘+up,ctrl+down,⌘+down",
    ].join(","),
    throttle((e, h) => {
      switch (h.key) {
        // ctrl+up,⌘+up,ctrl+down,⌘+down
        case "ctrl+up":
        case "⌘+up":
          pubsub.publish("zp.setVolume",{step: 5});
          break;
        case "ctrl+down":
        case "⌘+down":
          pubsub.publish("zp.setVolume",{step: -5});
          break;
      }
      e.preventDefault();
    }, 50)
  );
};
