#!/bin/bash

set -euo pipefail

usage() {
     cat <<EOF
Usage
  fly-admin-theme -h|--help       Show this help
  fly-admin-theme                 Run astra-systemsettings with kcm_fly_theme opened
  fly-admin-theme --dialog        Run theme settings in separate window

  fly-admin-theme export /path/to/index.theme
  fly-admin-theme import /path/to/archive


  fly-admin-theme wmcolor
     service non-GUI mode to use  ~/.fly/paletterc for generation
     Qt color patette in ~/.config/Trolltech.com
     fly-wm colors    in ~/.fly/theme/current.themerc file

  fly-admin-theme apply-color-scheme /path/to/scheme
     service non-GUI mode to apply colorschemepath

  fly-admin-theme <PageName> [options]
    PageName:
      Wallpaper
      Theme
      Effects
      Sounds
      Icons
      Cursors

Примеры:
  fly-admin-theme Wallpaper ~/Pictures/bg.jpg
  fly-admin-theme apply-color-scheme ~/Downloads/CustomScheme.colors
  fly-admin-theme wmcolor

EOF
}

err() {
     echo "Ошибка: $*" >&2
}

do_wmcolor() {
     exec fly-apply-theme
}

do_apply_color_scheme() {
     exec fly-apply-color-scheme "$1"
}

do_import_theme() {
     exec fly-import-theme "$1"
}

do_export_theme() {
     exec fly-export-theme "$1"
}

handle_wallpaper() {
     if [[ $# -eq 0 ]]; then
          exec astra-systemsettings kcm_fly_wallpaper
     fi

     if [[ $# -eq 1 ]]; then
          local img="$1"
          if [[ -f "$img" ]]; then
               FLY_ADMIN_THEME_SET_WALLPAPER=$img exec astra-systemsettings kcm_fly_wallpaper
          else
               err "Файл не найден: $img"
               exit 2
          fi
     fi

     err "Неверные аргументы для Wallpaper"
     exit 2
}

handle_color_scheme() {
     # add arg?
     exec astra-systemsettings kcm_fly_color
}

handle_effects() {
     exec astra-systemsettings kcm_fly_effects
}

handle_sounds() {
     exec astra-systemsettings kcm_fly_sounds
}

handle_icons() {
     exec astra-systemsettings kcm_fly_icons
}

handle_cursors() {
     exec astra-systemsettings kcm_fly_cursor
}

handle_screensaver() {
     exec astra-systemsettings kcm_fly_screensaver
}

# TODO: Add more
main() {

     if [ -n "${FLY_USE_ADMIN_THEME_OLD+x}" ]; then
         exec fly-admin-theme-old "$@"
     fi

     # run without options
     if [[ "${#@}" -eq 0 ]]; then
          exec astra-systemsettings kcm_fly_theme
     fi

     case "$1" in
          -h|--help)
               usage
               exit 0
          ;;

          # For now let's use kcmshell5
          # FIXME: kcmshell use custom order for modules not from args
          -dialog|--dialog|dialog|Dialog|DIALOG)
               #exec fly-kcmshell5 kcm_fly_theme kcm_fly_color kcm_fly_wallpaper kcm_fly_icons kcm_fly_cursor kcm_fly_effects kcm_fly_sounds kcm_fly_screensaver
               exec astra-systemsettings kcm_fly_theme
          ;;

          wmcolor|--wmcolor)
               shift
               do_wmcolor "$@"
               exit 0
          ;;

          apply-color-scheme|--apply-color-scheme)
               shift
               if [[ -z "${1-}" ]]; then
                    err "Нужен путь к схеме: apply-color-scheme /path/to/scheme"
                    exit 2
               fi
               do_apply_color_scheme "$1"
               exit 0
          ;;

          import|--import|Import|IMPORT)
               shift
               if [[ -z "${1-}" ]]; then
                    err "Нужен путь к теме, которую вы хотите импортировать."
                    exit 2
               fi
               do_import_theme "$1"
               exit 0
          ;;

          export|--export|Export|EXPORT)
               shift
               if [[ -z "${1-}" ]]; then
                    err "Нужен путь к теме, которую вы хотите экспортировать."
                    exit 2
               fi
               do_export_theme "$1"
               exit 0
          ;;

          Wallpaper|wallpaper|WALLPAPER|wallPaper|WallPaper)
               shift || true
               handle_wallpaper "$@"
               exit 0
          ;;

          Scheme|SCHEME|scheme|ColorScheme|colorscheme|COLORSCHEME)
               shift || true
               handle_color_scheme "$@"
               exit 0
          ;;

          Effects|effects|EFFECTS)
               shift || true
               handle_effects "$@"
               exit 0
          ;;

          Icons|icons|ICONS|Icon|ICON|icon)
               shift || true
               handle_icons "$@"
               exit 0
          ;;

          Cursors|cursors|CURSORS|Cursor|CURSOR|cursor)
               shift || true
               handle_cursors "$@"
               exit 0
          ;;

          *)
               err "Неправильный вызов или неизвестный параметр: $1"
               usage
               exit 2
          ;;
     esac
}

main "$@"
