# -*- coding: utf-8 -*-
#
# Copyright (c) 2016 B-Open Solutions srl - http://bopen.eu
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# python 2 support via python-future
from __future__ import absolute_import, division, print_function, unicode_literals

import functools

import click

import elevation
from . import util
from . import spatial


# disable overzealous warning
click.disable_unicode_literals_warning = True


@click.group()
@click.version_option()
@click.option('--product', default=elevation.DEFAULT_PRODUCT, type=click.Choice(elevation.PRODUCTS),
              help='DEM product choice (default: %r).' % elevation.DEFAULT_PRODUCT)
@click.option('--cache_dir', default=elevation.CACHE_DIR,
              type=click.Path(resolve_path=True, file_okay=False),
              help='Root of the DEM cache folder (default: %r).' % elevation.CACHE_DIR)
def eio(**kwargs):
    pass


@eio.command(short_help='Audits your installation for common issues.')
def selfcheck():
    print(util.selfcheck(tools=elevation.TOOLS))


def click_merge_parent_params(wrapped):
    @click.pass_context
    @functools.wraps(wrapped)
    def wrapper(ctx, **kwargs):
        if ctx.parent and ctx.parent.params:
            kwargs.update(ctx.parent.params)
        return wrapped(**kwargs)
    return wrapper


@eio.command(short_help='')
@click_merge_parent_params
def info(**kwargs):
    elevation.info(**kwargs)


@eio.command(short_help='Seed the DEM to given bounds.')
@click.option('--bounds', nargs=4, type=float, default=None,
              help='Output bounds: left bottom right top.')
@click_merge_parent_params
def seed(**kwargs):
    elevation.seed(**kwargs)


@eio.command(short_help='Clip the DEM to given bounds.')
@click.option('-o', '--output', default=elevation.DEFAULT_OUTPUT,
              type=click.Path(resolve_path=True, dir_okay=False),
              help="Path to output file. Existing files will be overwritten.")
@click.option('--bounds', nargs=4, type=float,
              help='Output bounds: left bottom right top.')
@click.option('-m', '--margin', default=elevation.MARGIN,
              help="Decimal degree margin added to the bounds. Use '%%' for percent margin. "
              "Defaults to %r" % elevation.MARGIN)
@click.option('-r', '--reference',
              help="Use the extent of a reference GDAL/OGR data source as output bounds.")
@click_merge_parent_params
def clip(bounds, reference, **kwargs):
    if not bounds and not reference:
        raise click.BadOptionUsage("One of --bounds or --reference must be supplied.")
    if not bounds:
        bounds = spatial.import_bounds(reference)
    elevation.clip(bounds, **kwargs)


@eio.command(short_help='Clean up the cache from temporary files.')
@click_merge_parent_params
def clean(**kwargs):
    elevation.clean(**kwargs)


@eio.command(short_help='Clean up the cache from temporary files.')
@click_merge_parent_params
def distclean(**kwargs):
    elevation.distclean(**kwargs)

@@ -986,43 +986,8 @@
 ct',
- default=elevation.DEFAULT_PRODUCT,
  typ
@@ -1040,289 +1040,297 @@
     
-help='DEM product choice (default: %25r).' %25 elevation.DEFAULT_PRODUCT)%0A@click.option('--cache_dir', default=elevation.CACHE_DIR,%0A              type=click.Path(resolve_path=True, file_okay=False),%0A              help='Root of the DEM cache folder (default: %25r).' %25 elevation.CACHE_DIR
+default=elevation.DEFAULT_PRODUCT, show_default=True,%0A              help=%22DEM product choice.%22)%0A@click.option('--cache_dir', type=click.Path(resolve_path=True, file_okay=False),%0A              default=elevation.CACHE_DIR, show_default=True,%0A              help='Root of the DEM cache folder'
 )%0Ade
@@ -1979,22 +1979,8 @@
 oat,
- default=None,
 %0A   
@@ -2206,56 +2206,8 @@
 ut',
- default=elevation.DEFAULT_OUTPUT,%0A             
  typ
@@ -2247,32 +2247,99 @@
 ir_okay=False),%0A
+              default=elevation.DEFAULT_OUTPUT, show_default=True,%0A
               he
@@ -2422,33 +2422,24 @@
 ('--bounds',
- nargs=4,
  type=float,
@@ -2430,32 +2430,41 @@
 ds', type=float,
+ nargs=4,
 %0A              h
@@ -2565,16 +2565,35 @@
 .MARGIN,
+ show_default=True,
 %0A       
@@ -2680,60 +2680,9 @@
 gin.
- %22%0A              %22Defaults to %25r%22 %25 elevation.MARGIN
+%22
 )%0A@c

Make options more homogeneus and use show_default kwarg.