(defun volume-up ()
  "Increase the volume by 5%."
  (interactive)
  (start-process-shell-command "volume-up" nil "pactl set-sink-volume 0 +5%"))

(defun volume-down ()
  "Decrease the volume by 5%."
  (interactive)
  (start-process-shell-command "volume-down" nil "pactl set-sink-volume 0 -5%"))

(defun volume-mute-toggle ()
  "Toggle mute."
  (interactive)
  (start-process-shell-command "volume-mute" nil "pactl set-sink-mute 0 toggle"))

(defun change-lan ()
  "change language"
  (interactive)
  (start-process-shell-command "change-local" nil "bash /home/fs/bin/change_layout.sh"))

(defun make-screenshot ()
  "change language"
  (interactive)
  (start-process-shell-command "make-screenshot" nil "flameshot gui"))

(defun my-hide-current-buffer ()
  "Скрыть текущий буфер."
  (interactive)
  (scratch-buffer)
  (delete-window))

(defun efs/exwm-update-class ()
  (exwm-workspace-rename-buffer exwm-class-name))

(defun efs/exwm-init ()
  (exwm-workspace-switch-create 1)
)

(use-package exwm
  :config
  ;; Set the default number of workspaces
  (setq exwm-workspace-number 10)

  ;; When window "class" updates, use it to set the buffer name
  (add-hook 'exwm-update-class-hook #'efs/exwm-update-class)

  (add-hook 'exwm-init-hook #'efs/exwm-init)

  ;; These keys should always pass through to Emacs
  (setq exwm-input-prefix-keys
    '(?\C-x
      ?\C-u
      ;; ?\C-h
      ?\M-x
      ?\M-`
      ?\M-&
      ?\M-:
      ?\M-r
      ?\M-j ; move
      ?\M-k ; move
      ?\M-h ; move
      ?\M-l ; move
      ?\M-J ; move swap
      ?\M-K ; move swap
      ?\M-H ; move swap
      ?\M-L ; move swap
      ?\M-n ; buffers
      ?\M-p ; buffers
      ?\M-\ ;
      ?\M-- ;
      ?\M-m ; hide buffer
      ?\M-1 ; workspace
      ?\M-2 ; workspace
      ?\M-3 ; workspace
      ?\M-4 ; workspace
      ?\M-5 ; workspace
      ?\M-6 ; workspace
      ?\M-7 ; workspace
      ?\M-8 ; workspace
      ?\M-9 ; workspace
      ?\s-\ ;
      ?\C-\M-j  ;; Buffer list
      ?\C-\ ))  ;; Ctrl+Space

  ;; Ctrl+Q will enable the next key to be sent directly
  (define-key exwm-mode-map [?\C-q] 'exwm-input-send-next-key)

  ;; Set up global key bindings.  These always work, no matter the input state!
  ;; Keep in mind that changing this list after EXWM initializes has no effect.
  (global-set-key (kbd "s-SPC") 'change-lan)
  (global-set-key (kbd "M-.") 'change-lan)
  (global-set-key (kbd "M-ю") 'change-lan)
  (global-set-key (kbd "M--") 'change-lan)
  (global-set-key (kbd "M-r") 'counsel-linux-app)
  (global-set-key (kbd "M-m") 'my-hide-current-buffer)

  (global-set-key (kbd "M-n") 'next-buffer)
  (global-set-key (kbd "M-p") 'previous-buffer)

  (global-set-key (kbd "M-1") (lambda () (interactive) (exwm-workspace-switch-create 1)))
  (global-set-key (kbd "M-2") (lambda () (interactive) (exwm-workspace-switch-create 2)))
  (global-set-key (kbd "M-3") (lambda () (interactive) (exwm-workspace-switch-create 3)))
  (global-set-key (kbd "M-4") (lambda () (interactive) (exwm-workspace-switch-create 4)))
  (global-set-key (kbd "M-5") (lambda () (interactive) (exwm-workspace-switch-create 5)))
  (global-set-key (kbd "M-6") (lambda () (interactive) (exwm-workspace-switch-create 6)))
  (global-set-key (kbd "M-7") (lambda () (interactive) (exwm-workspace-switch-create 7)))
  (global-set-key (kbd "M-8") (lambda () (interactive) (exwm-workspace-switch-create 8)))
  (global-set-key (kbd "M-9") (lambda () (interactive) (exwm-workspace-switch-create 9)))

  (global-set-key (kbd "M-h") (lambda () (interactive) (windmove-left)))
  (global-set-key (kbd "M-l") (lambda () (interactive) (windmove-right)))
  (global-set-key (kbd "M-j") (lambda () (interactive) (windmove-down)))
  (global-set-key (kbd "M-k") (lambda () (interactive) (windmove-up)))

  (global-set-key (kbd "M-H") (lambda () (interactive) (windmove-swap-states-left)))
  (global-set-key (kbd "M-L") (lambda () (interactive) (windmove-swap-states-right)))
  (global-set-key (kbd "M-J") (lambda () (interactive) (windmove-swap-states-down)))
  (global-set-key (kbd "M-K") (lambda () (interactive) (windmove-swap-states-up)))

  (global-set-key (kbd "C-c p") (lambda () (interactive) (make-screenshot)))

  (setq exwm-input-global-keys
        `(
          ;; Reset to line-mode (C-c C-k switches to char-mode via exwm-input-release-keyboard)
          ([?\s-r] . exwm-reset)

          (,(kbd "s-h") . windmove-left)    ;; Super + h: окно влево
          (,(kbd "s-l") . windmove-right)   ;; Super + l: окно вправо
          (,(kbd "s-k") . windmove-up)      ;; Super + k: окно вверх
          (,(kbd "s-j") . windmove-down)

          (,(kbd "<XF86AudioRaiseVolume>") . volume-up)
          (,(kbd "<XF86AudioLowerVolume>") . volume-down)
          (,(kbd "<XF86AudioMute>") . volume-mute-toggle)

          ;; Launch applications via shell command
          ([?\s-&] . (lambda (command)
                       (interactive (list (read-shell-command "$ ")))
                       (start-process-shell-command command nil command)))

          ;; Switch workspace
          ([?\s-w] . exwm-workspace-switch)
          ))

  ;; Show battery status in the mode line
  (display-battery-mode 1)

  ;; Show the time and date in modeline
  (setq display-time-day-and-date t)
  (display-time-mode 1)

  (exwm-enable))

(require 'exwm-randr)
(exwm-randr-enable)

(start-process-shell-command "xrandr" nil "xrandr --output HDMI1 --mode 3840x2160 --panning 3840x2160 --rate 60 --output eDP1 --off")

(global-set-key (kbd "<XF86AudioRaiseVolume>") 'volume-up)
(global-set-key (kbd "<XF86AudioLowerVolume>") 'volume-down)
(global-set-key (kbd "<XF86AudioMute>") 'volume-mute-toggle)
