#!python
# -*- coding: utf-8 -*
###############################################################################
#                                                                             #
#    LSFM image server                                                        #
#                                                                             #
#    Copyright (C) 2017-2018 Sylwia Bednarek, Piotr Majka                     #
#    (Laboratory of Neuroinformatics; Nencki Institute of Experimental        #
#    Biology of Polish Academy of Sciences)                                   #
#                                                                             #
#    This software is free software: you can redistribute it and/or modify    #
#    it under the terms of the GNU General Public License as published by     #
#    the Free Software Foundation, either version 3 of the License, or        #
#    (at your option) any later version.                                      #
#                                                                             #
#    This software is distributed in the hope that it will be useful,         #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of           #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            #
#    GNU General Public License for more details.                             #
#                                                                             #
#    You should have received a copy of the GNU General Public License        #
#    along with this software.  If not, see http://www.gnu.org/licenses/.     #
#                                                                             #
###############################################################################

import argparse

AUTOCOMPLETE_CONTENT = """#!/usr/bin/env bash
# bash completion support for lsfmpy

_complete-lsfmpy()
{
  local start cur prev opts
  COMPREPLY=()
  start="${COMP_WORDS[@]:0:1}"
  if [ ${COMP_CWORD} -gt 1  ]  ; then
    start="${COMP_WORDS[@]:0:2}"
  fi
  cur="${COMP_WORDS[COMP_CWORD]}"
  prev="${COMP_WORDS[COMP_CWORD-1]}"

  opts=""

  if [[ "$prev" == "--hdf-path" ]] ; then
    COMPREPLY=( $(compgen -o plusdirs -o nospace -f -X '!*.h5' -- ${cur}) )
    return 0
  fi

  if [[ "$prev" == "--affine-path" ]] ; then
    COMPREPLY=( $(compgen -o plusdirs -o nospace -f -X '!*.txt' -- ${cur}) )
    return 0
  fi

  if [[ "$prev" == "--metadata-path" ]] ; then
    COMPREPLY=( $(compgen -o plusdirs -o nospace -f -X '!*.json' -- ${cur}) )
    return 0
  fi

  if [[ "$prev" == "--image-path" ]] ; then
    COMPREPLY=( $(compgen -o plusdirs -o nospace -f -X '!*.tif' -- ${cur}) )
    return 0
  fi

  if [[ "$start" == "lsfmpy info" ]] ; then
    opts="--channel-name --hdf-path"
  fi

  if [[ "$start" == "lsfmpy export" ]] ; then
    opts="--channel-name --grid-size --hdf-path --input-orientation --input-resolution-level --output-path --output-resolution --overlap-mm --phys-origin --phys-size --region-id --segmentation-name"
  fi

  if [[ "$start" == "lsfmpy add-metadata" ]] ; then
    opts="--hdf-path --m-key --m-value"
  fi

  if [[ "$start" == "lsfmpy show-log" ]] ; then
    opts="--hdf-path"
  fi

  if [[ "$start" == "lsfmpy export-slices" ]] ; then
    opts="--axis --channel-name --extract-roi --hdf-path --input-orientation --input-resolution-level --output-path --slicing-range"
  fi

  if [[ "$start" == "lsfmpy" ]] ; then
    opts="add-metadata add-transform export export-slices info show-log write write-affine"
  fi

  if [[ "$start" == "lsfmpy write" ]] ; then
    opts="--bdv-xml --channel-name --file-name-format --hdf-path --image-path --is-multichannel --is-segmentation --metadata-path --n_cpus --slab-memory-size"
  fi

  if [[ "$start" == "lsfmpy add-transform" ]] ; then
    opts="--invert --name --ordn --transform-type"
  fi

  if [[ "$start" == "lsfmpy write-affine" ]] ; then
    opts="--affine-name --affine-path --channel-name --hdf-path"
  fi

  COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
  return 0
}

complete -F _complete-lsfmpy lsfmpy

"""

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Wil create autocomplete script for lsfmpy (bash only)',
                                     prog='lsfmpy_create_autocomplete')

    parser.add_argument('--output-file',
                        required=True,
                        help='path to output autocomplete script',
                        type=str)

    args = parser.parse_args()
    file_path = args.output_file

    with open(file_path, 'w') as fh:
        fh.write(AUTOCOMPLETE_CONTENT)

    print("Created autocomplete script at {}".format(file_path))
