{ lib, pkgs, config, ... }:

let
  inherit (pkgs) mpvScripts;
  anime4k = pkgs.anime4k or pkgs.unstable.anime4k or null;
  a4k = lib.mkIf (anime4k != null);
in

{
  #home.packages = with pkgs; [ mpv ];

  programs.mpv.enable = true;
  programs.mpv.bindings."shift+p" = "playlist-prev";
  programs.mpv.bindings."shift+n" = "playlist-next";
  programs.mpv.bindings."n"       = "sub-seek  1";
  /* programs.mpv.bindings."SHIFT+n" = "sub-seek -1"; */
  programs.mpv.bindings."b"       = ''cycle-values audio-channels 2 5 7 "auto"'';
  programs.mpv.bindings."k"       = "cycle sub-filter-sdh";

  # WeChip g20 voice remote
  programs.mpv.bindings."homepage" = "cycle fullscreen";

  # anime4k - from https://github.com/NixOS/nixpkgs/pull/273830
  # TODO: add to home-manager?
  programs.mpv.bindings."CTRL+0" = a4k ''no-osd change-list glsl-shaders clr ""; show-text "GLSL shaders cleared"'';
  programs.mpv.bindings."CTRL+1" = a4k ''no-osd change-list glsl-shaders set "${anime4k}/Anime4K_Clamp_Highlights.glsl:${anime4k}/Anime4K_Restore_CNN_VL.glsl:${anime4k}/Anime4K_Upscale_CNN_x2_VL.glsl:${anime4k}/Anime4K_AutoDownscalePre_x2.glsl:${anime4k}/Anime4K_AutoDownscalePre_x4.glsl:${anime4k}/Anime4K_Upscale_CNN_x2_M.glsl"; show-text "Anime4K: Mode A (HQ)"'';
  programs.mpv.bindings."CTRL+2" = a4k ''no-osd change-list glsl-shaders set "${anime4k}/Anime4K_Clamp_Highlights.glsl:${anime4k}/Anime4K_Restore_CNN_Soft_VL.glsl:${anime4k}/Anime4K_Upscale_CNN_x2_VL.glsl:${anime4k}/Anime4K_AutoDownscalePre_x2.glsl:${anime4k}/Anime4K_AutoDownscalePre_x4.glsl:${anime4k}/Anime4K_Upscale_CNN_x2_M.glsl"; show-text "Anime4K: Mode B (HQ)"'';
  programs.mpv.bindings."CTRL+3" = a4k ''no-osd change-list glsl-shaders set "${anime4k}/Anime4K_Clamp_Highlights.glsl:${anime4k}/Anime4K_Upscale_Denoise_CNN_x2_VL.glsl:${anime4k}/Anime4K_AutoDownscalePre_x2.glsl:${anime4k}/Anime4K_AutoDownscalePre_x4.glsl:${anime4k}/Anime4K_Upscale_CNN_x2_M.glsl"; show-text "Anime4K: Mode C (HQ)"'';
  programs.mpv.bindings."CTRL+4" = a4k ''no-osd change-list glsl-shaders set "${anime4k}/Anime4K_Clamp_Highlights.glsl:${anime4k}/Anime4K_Restore_CNN_VL.glsl:${anime4k}/Anime4K_Upscale_CNN_x2_VL.glsl:${anime4k}/Anime4K_Restore_CNN_M.glsl:${anime4k}/Anime4K_AutoDownscalePre_x2.glsl:${anime4k}/Anime4K_AutoDownscalePre_x4.glsl:${anime4k}/Anime4K_Upscale_CNN_x2_M.glsl"; show-text "Anime4K: Mode A+A (HQ)"'';
  programs.mpv.bindings."CTRL+5" = a4k ''no-osd change-list glsl-shaders set "${anime4k}/Anime4K_Clamp_Highlights.glsl:${anime4k}/Anime4K_Restore_CNN_Soft_VL.glsl:${anime4k}/Anime4K_Upscale_CNN_x2_VL.glsl:${anime4k}/Anime4K_AutoDownscalePre_x2.glsl:${anime4k}/Anime4K_AutoDownscalePre_x4.glsl:${anime4k}/Anime4K_Restore_CNN_Soft_M.glsl:${anime4k}/Anime4K_Upscale_CNN_x2_M.glsl"; show-text "Anime4K: Mode B+B (HQ)"'';
  programs.mpv.bindings."CTRL+6" = a4k ''no-osd change-list glsl-shaders set "${anime4k}/Anime4K_Clamp_Highlights.glsl:${anime4k}/Anime4K_Upscale_Denoise_CNN_x2_VL.glsl:${anime4k}/Anime4K_AutoDownscalePre_x2.glsl:${anime4k}/Anime4K_AutoDownscalePre_x4.glsl:${anime4k}/Anime4K_Restore_CNN_M.glsl:${anime4k}/Anime4K_Upscale_CNN_x2_M.glsl"; show-text "Anime4K: Mode C+A (HQ)"'';
  /**/


  programs.mpv.scripts = [
    #mpvScripts.thumbnail # TOO SLOW
    mpvScripts.thumbfast
    mpvScripts.mpris
    mpvScripts.mpv-webm # activate with SHIFT+W
    # from https://github.com/mpv-player/mpv/issues/9767#issuecomment-1024831645
    ({ scriptName = "dynamic-subs.lua"; } // (pkgs.writeTextDir "share/mpv/scripts/dynamic-subs.lua" ''
      mp.add_hook('on_load', 10, function()
        sub_paths = {'Subs'}
        sub_paths[#sub_paths+1]='Subs/' .. mp.get_property('filename/no-ext')
        mp.set_property_native('sub-file-paths', sub_paths)
      end)
    ''))
  ];
  #programs.mpv.config.osc = "no"; # likely a 'thumbnail' requirement

  # dynamic-subs.lua
  programs.mpv.config.sub-auto = "all";

  #https://wiki.nixos.org/wiki/Accelerated_Video_Playback
  programs.mpv.config.hwdec       = "auto-safe";
  programs.mpv.config.vo          = "gpu";
  programs.mpv.config.profile     = "gpu-hq";
  #programs.mpv.config.gpu-context = "wayland";

  programs.mpv.config.screenshot-directory = "${config.home.homeDirectory}/Pictures/Screenshots-mpv";
  # https://mpv.io/manual/master/#options-screenshot-template
  # https://man.archlinux.org/man/core/man-pages/strftime.3.en#DESCRIPTION
  #programs.mpv.config.screenshot-template = "mpv-shot%n"; # mpv-shot0001.png (default)
  programs.mpv.config.screenshot-template = "mpv-shot-%tY-%tm-%td-%tH%tM%tS - %F @ %P"; # mpv-shot-YYYY-MM-DD-HHMMSS <filename> <timestamp in HH:MM:SS.mmm>.png
}
