#compdef hidutil

# vim:ft=zsh:et:sts=2:sw=2
# -----------------------------------------------------------------------------
# The BSD-3-Clause License
# Copyright (c) 2016, Koichi Shiraishi
# All rights reserved.
# -----------------------------------------------------------------------------

function _hidutil() {
  local context curcontext=$curcontext state line ret=1
  declare -A opt_args

  local -a commands

  commands=(
  'dump:Dump HID Event System state'
  'property:Read/Write HID Event System property'
  'list:List HID Event System services'
  )

  _arguments -C \
    "1: :{_describe 'hidutil command' commands}" \
    '*:: :->args' \
    && ret=0

  case $state in
    (args)
      case $words[1] in
        dump)
          local -a object
          object=(
          'system:HID Event System'
          'clients:HID Event System Clients'
          'services:HID Event System services'
          )
          _arguments \
            {-f,--format}'[Format type]:(xml text)' \
            {-o,--output}'[Output file (or stdout if not specified)]' \
            "1: :{_describe 'dump object' object}" \
            && return
          ;;
        property)
          _arguments \
            '--filter[Filter services by properties (key/value pair JSON style dictionary)]: :->filter' \
            '--get[Get property with key (where key is string value)]:key' \
            '--set[Set property (key/value pair JSON style dictionary)]:dictionary' \
            && return
            case $state in
              (filter)
                local -a dictionary
                dictionary=(
                'ProductID:numeric value (decimal or hex)'
                'VendorID:numeric value (decimal or hex)'
                'LocationID:numeric value (decimal or hex)'
                'PrimaryUsagePage:numeric value (decimal or hex)'
                'PrimaryUsage:numeric value (decimal or hex)'
                'Trasport:string value'
                'Product:string value'
                )
                _arguments "*: :{_describe 'dictionary' filter_dictionary}"
                ;;
            esac
          ;;
        list)
          _arguments \
            '--filter[Filter services by properties (key/value pair JSON style dictionary)]: :->filter' \
            && return
          case $state in
            filter)
              local -a dictionary
              dictionary=(
              'ProductID:numeric value (decimal or hex)'
              'VendorID:numeric value (decimal or hex)'
              'LocationID:numeric value (decimal or hex)'
              'PrimaryUsagePage:numeric value (decimal or hex)'
              'PrimaryUsage:numeric value (decimal or hex)'
              'Trasport:string value'
              'Product:string value'
              )
              _arguments "*: :{_describe 'dictionary' dictionary}"
              ;;
          esac
          ;;
      esac
      ;;
  esac

  return ret
}

_hidutil "$@"
