# -*- coding: utf-8 -*-
#########################################################################
#
# Copyright (C) 2016 OSGeo
#
# This program 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 program 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 program. If not, see <http://www.gnu.org/licenses/>.
#
#########################################################################

from django.core.management.base import BaseCommand
from oauth2_provider.models import Application
from oauth2_provider.generators import generate_client_id, generate_client_secret
from geonode.people.models import Profile

from geonode import geoserver, qgis_server  # noqa
from geonode.utils import check_ogc_backend


class Command(BaseCommand):
    """Creates or updates the oauth2 Application
    """
    can_import_settings = True

    def add_arguments(self, parser):

        # Named (optional) arguments
        parser.add_argument(
            '-f',
            '--force',
            action='store_true',
            dest='force_exec',
            default=False,
            help='Forces the regeneration of OAUth keys.')

        parser.add_argument(
            '--target-address',
            dest='target_address',
            help='Target Address (the one to be changed e.g. http://my-public.geonode.org)')

    def handle(self, *args, **options):

        force_exec = options.get('force_exec')
        target_address = options.get('target_address')

        from django.conf import settings
        client_id = None
        client_secret = None

        if check_ogc_backend(geoserver.BACKEND_PACKAGE):
            from geonode.geoserver.helpers import ogc_server_settings
            redirect_uris = '%s\n%s\n%s' % (
                ogc_server_settings.LOCATION,
                ogc_server_settings.public_url,
                "{0}/geoserver/".format(target_address))
            if Application.objects.filter(name='GeoServer').exists():
                Application.objects.filter(name='GeoServer').update(redirect_uris=redirect_uris)
                if force_exec:
                    Application.objects.filter(name='GeoServer').update(
                        client_id=generate_client_id(),
                        client_secret=generate_client_secret()
                    )
                app = Application.objects.filter(name='GeoServer')[0]
                client_id = app.client_id
                client_secret = app.client_secret
            else:
                client_id = generate_client_id()
                client_secret = generate_client_secret()
                Application.objects.create(
                    skip_authorization=True,
                    redirect_uris=redirect_uris,
                    name='GeoServer',
                    authorization_grant_type='authorization-code',
                    client_type='confidential',
                    client_id=client_id,
                    client_secret=client_secret,
                    user=Profile.objects.filter(is_superuser=True)[0]
                )
        return '%s,%s' % (client_id, client_secret)
