#!/usr/bin/python3
#
# Copyright (c) 2018 SUSE Linux GmbH.  All rights reserved.
#
# This file is part of susePublicCloudInfoClient
#
# susePublicCloudInfoClient 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.
#
# susePublicCloudInfoClientis 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 susePublicCloudInfoClient. If not, see
# <http://www.gnu.org/licenses/>.
#
"""
usage: pint -h | --help
       pint providers
          [ --json | --xml ]
       pint image_states
          [ --json | --xml ]
       pint ({PROVIDERS}) server_types 
          [ --json | --xml ]
       pint ({PROVIDERS}) regions
          [ --filter=<filter> ]
          [ --json | --xml ]
       pint ({PROVIDERS}) servers
          [ --filter=<filter> ]
          [ --json | --xml ]
          [ --region=<region> ]
          [ --smt | --regionserver ]
       pint ({PROVIDERS}) images
          [ --active | --inactive | --deleted | --deprecated ]
          [ --filter=<filter> ]
          [ --json | --xml ]
          [ --region=<region> ]
       pint -v | --version

options:
   -h --help
       Show help
   --active
       Only include images which are actively maintained
   --deleted
       Only include images which have been deleted
   --deprecated
       Only include images which are deprecated
       (scheduled for deletion in 6 months)
   --filter=<filter>
       Comma separated list of available attributes
   --inactive
       Only include images which are inactive
       (only receiving critical updates, but not yet deprecated)
   --json
       Output data in JSON format
   --region=<region>
       Provide information for regions given in comma separated list,
       if omitted all regions are included
   --regionserver
       Provide only Region Server information
   --smt
       Provide only SMT Server information
   --xml
       Output data in XML format
   -v --version
       Show program version
"""

import sys
import json

from docopt import docopt

import susepubliccloudinfoclient.infoserverrequests as ifsrequest
import susepubliccloudinfoclient.version as version

provider_data = ifsrequest.get_provider_data(None, None, 'json', 'all', None)
providers = json.loads(provider_data)
cloud_providers = []
for provider in providers['providers']:
    cloud_providers.append(provider['name'])

command_args = docopt(__doc__.format(PROVIDERS = "|".join(cloud_providers)), version=version.VERSION)

framework = None
for csp in cloud_providers:
    if command_args[csp]:
        framework = csp
        break

image_state = None
image_states = ('active', 'inactive', 'deleted', 'deprecated')
for state in image_states:
    if command_args['--%s' % state]:
        image_state = state
        break

server_type = None
server_types = ('regionserver', 'smt')
for item in server_types:
    if command_args['--%s' % item]:
        server_type = item
        break

output_format = 'plain'
output_options = ('json', 'xml')
for out in output_options:
    if command_args['--%s' % out]:
        output_format = out
        break

region = 'all'
if command_args['--region']:
    region = command_args['--region']

try:
    if command_args['images']:
        print(ifsrequest.get_image_data(
            framework,
            image_state,
            output_format,
            region,
            command_args['--filter']))
    elif command_args['servers']:
        print(ifsrequest.get_server_data(
            framework,
            server_type,
            output_format,
            region,
            command_args['--filter']))
    elif command_args['providers']:
        print(ifsrequest.get_provider_data(
            framework,
            server_type,
            output_format,
            region,
            command_args['--filter']))
    elif command_args['image_states']:
        print(ifsrequest.get_image_states_data(
            framework,
            server_type,
            output_format,
            region,
            command_args['--filter']))
    elif command_args['server_types']:
        print(ifsrequest.get_server_types_data(
            framework,
            server_type,
            output_format,
            region,
            command_args['--filter']))
    else:
        print(ifsrequest.get_regions_data(
            framework,
            server_type,
            output_format,
            region,
            command_args['--filter']))
except Exception:
    sys.exit(1)
