#!/usr/bin/env python
# Copyright 2017 Descartes Labs.
#
# 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.

from __future__ import print_function

import json
import argparse

import descarteslabs as dl

if __name__ == "__main__":

    parser = argparse.ArgumentParser()

    parser.add_argument('command', choices=['sources', 'summary', 'search', 'keys', 'get'],
                        help='The action to take')

    parser.add_argument('argument', nargs='?')

    parser.add_argument('-url', help='The url of the service')
    parser.add_argument('-shape', help='A slug for a named shape')
    parser.add_argument('-const_id', default=None, help='Constellation ID', nargs='+')
    parser.add_argument('-start_time', help='Start of valid date/time range (inclusive)')
    parser.add_argument('-end_time', help='End of valid date/time range (inclusive)')
    parser.add_argument('-geom', help='Region of interest as GeoJSON or WKT')
    parser.add_argument('-params',
                        help='JSON String of additional key/value pairs for searching properties: tile_id, cloud_fraction, etc.')
    parser.add_argument('-limit', default=100, help='Number of items to return (default 100)', type=int)
    parser.add_argument('-offset', help='Number of items to skip (default 0)')
    parser.add_argument('-bbox', help='Whether or not to use a bounding box filter (default: false)',
                        action='store_true')

    args = parser.parse_args()

    metadata = dl.metadata

    if args.url:
        metadata.url = args.url

    kwargs = {}

    if args.command == 'sources':
        sources = metadata.sources()

        print(json.dumps(sources, indent=2))

    if args.command == 'summary':

        if args.shape:
            kwargs['shape'] = args.shape
        if args.const_id:
            kwargs['const_id'] = args.const_id
        if args.start_time:
            kwargs['start_time'] = args.start_time
        if args.end_time:
            kwargs['end_time'] = args.end_time
        if args.geom:
            kwargs['geom'] = args.geom
        if args.params:
            kwargs['params'] = args.params
        if args.bbox:
            kwargs['bbox'] = args.bbox

        summary = metadata.summary(**kwargs)

        print(json.dumps(summary))

    if args.command == 'search':

        if args.shape:
            kwargs['shape'] = args.shape
        if args.const_id:
            kwargs['const_id'] = args.const_id
        if args.start_time:
            kwargs['start_time'] = args.start_time
        if args.end_time:
            kwargs['end_time'] = args.end_time
        if args.geom:
            kwargs['geom'] = args.geom
        if args.params:
            kwargs['params'] = args.params
        if args.limit:
            kwargs['limit'] = args.limit
        if args.offset:
            kwargs['offset'] = args.offset
        if args.bbox:
            kwargs['bbox'] = args.bbox

        search = metadata.search(**kwargs)

        print(json.dumps(search, indent=2))

    if args.command == 'keys':

        if args.shape:
            kwargs['shape'] = args.shape
        if args.const_id:
            kwargs['const_id'] = args.const_id
        if args.start_time:
            kwargs['start_time'] = args.start_time
        if args.end_time:
            kwargs['end_time'] = args.end_time
        if args.geom:
            kwargs['geom'] = args.geom
        if args.params:
            kwargs['params'] = args.params
        if args.limit:
            kwargs['limit'] = args.limit
        if args.offset:
            kwargs['offset'] = args.offset
        if args.bbox:
            kwargs['bbox'] = args.bbox

        keys = metadata.keys(**kwargs)

        print(' '.join(keys))

    if args.command == 'get':
        get = metadata.get(args.argument)

        print(json.dumps(get))
