#!/usr/bin/env python
# Copyright 2015 Telefonica Investigacion y Desarrollo, S.A.U
#
# This file is part of ckanext-ngsipreview.
#
# Ckanext-ngsiview is free software: you can redistribute it and/or
# modify it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# Ckanext-ngsiview 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 Affero
# General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Orion Context Broker. If not, see http://www.gnu.org/licenses/.

import logging

from ckan.common import json
import ckan.plugins as p
import ckan.lib.helpers as h
import ckan.lib.datapreview as datapreview

log = logging.getLogger(__name__)

try:
    import ckanext.resourceproxy.plugin as proxy
except ImportError:
    pass


def check_query(resource):
    if resource['url'].lower().find('/querycontext') != -1 or resource['url'].lower().find('/contextentities/') != -1:
        return True
    else:
        return False


class NgsiView(p.SingletonPlugin):

    p.implements(p.IRoutes, inherit=True)
    p.implements(p.IConfigurer, inherit=True)
    p.implements(p.IConfigurable, inherit=True)

    if p.toolkit.ckan.__version__ == '2.3':
        p.implements(p.IResourceView, inherit=True)
    else:
        p.implements(p.IResourcePreview, inherit=True)

    NGSI_FORMATS = ['ngsi9','ngsi10']

    def before_map(self, m):
        m.connect('/dataset/{id}/resource/{resource_id}/ngsiproxy',
        controller='ckanext.ngsiview.controller:ProxyNGSIController', action='proxy_ngsi_resource')
        return m

    def get_proxified_ngsi_url(self, data_dict):
        url = h.url_for(action='proxy_ngsi_resource', controller='ckanext.ngsiview.controller:ProxyNGSIController',
        id=data_dict['package']['name'], resource_id=data_dict['resource']['id'])
        log.info('Proxified url is {0}'.format(url))
        return url

    def update_config(self, config):
        p.toolkit.add_public_directory(config, 'theme/public')
        p.toolkit.add_template_directory(config, 'theme/templates')
        p.toolkit.add_resource('theme/public', 'ckanext-ngsiview')

    def setup_template_variables(self, context, data_dict):
        if p.toolkit.ckan.__version__ == '2.3':
            metadata = {'ngsi_formats': self.NGSI_FORMATS}
            resource = data_dict['resource']
            proxy_enabled = p.plugin_loaded('resource_proxy')
            oauth2_enabled = p.plugin_loaded('oauth2')
            same_domain = datapreview.on_same_domain(data_dict)

            if 'oauth_req' not in resource:
                oauth_req = 'false'
            else:
                oauth_req = resource['oauth_req']

            if proxy_enabled and not same_domain:
                if check_query(resource):
                    if oauth_req == 'true' and (not p.toolkit.c.user or not oauth2_enabled):
                        details = "This query may need Oauth-token, please check if the token field on resource_edit is correct"
                        h.flash_error(details, allow_html=False)
                        view_enable = [False, details]
                        url = proxy.get_proxified_resource_url(data_dict)
                    else:
                        url = self.get_proxified_ngsi_url(data_dict)
                        data_dict['resource']['url'] = url
                        view_enable = [True, 'OK']

                else:
                    details = "This is not a ContextBroker query, pleas check CBdocurl"
                    h.flash_error(details, allow_html=False)
                    view_enable = [False, details]
                    url = proxy.get_proxified_resource_url(data_dict)
            else:
                    details = "proxy o archivo"
                    h.flash_error(details, allow_html=False)
                    view_enable = [False, details]
                    url = ''

            return {'preview_metadata': json.dumps(metadata),
                    'resource_json': json.dumps(data_dict['resource']),
                    'resource_url': json.dumps(url),
                    'view_enable': json.dumps(view_enable)}
        else:
            if self.proxy_is_enabled and not data_dict['resource']['on_same_domain']:
                if check_query(data_dict['resource']):
                    url = self.get_proxified_ngsi_url(data_dict)
                    p.toolkit.c.resource['url'] = url
                else:
                    url = proxy.get_proxified_resource_url(data_dict)
                    p.toolkit.c.resource['url'] = url


    def info(self):
        return {'name': 'ngsiview',
                'title': p.toolkit._('NGSI'),
                'icon': 'file-text-alt',
                'default_title': p.toolkit._('NGSI'),
                'default_description': 'NGSI resource',
                'always_available': False,
                'iframed': True,
                'preview_enabled': True,
                'full_page_edit': False,
                }

    def can_view(self, data_dict):
        resource = data_dict['resource']
        format_lower = resource.get('format', '').lower()
        proxy_enabled = p.plugin_loaded('resource_proxy')
        same_domain = datapreview.on_same_domain(data_dict)

        if format_lower in self.NGSI_FORMATS and check_query(resource):
            if check_query(resource):
                return same_domain or proxy_enabled
            else:
                return False
        else:
            return False

    def view_template(self, context, data_dict):
        return 'ngsi.html'

##################
    def configure(self, config):
        self.proxy_is_enabled = config.get('ckan.resource_proxy_enabled')
        if config.get('ckan.plugins').find('oauth2') != -1:
            self.oauth2_is_enabled = True

    def can_preview(self, data_dict):
        resource = data_dict['resource']
        if 'oauth_req' not in resource:
            oauth_req = 'false'
        else:
            oauth_req = resource['oauth_req']

        format_lower = resource['format'].lower()
        if format_lower in self.NGSI_FORMATS:
            if resource['on_same_domain'] or self.proxy_is_enabled:
                if self.check_query(resource) and oauth_req == 'true' and not p.toolkit.c.user:
                    details = "In order to see this resource properly, you need to be logged in"
                    h.flash_error(details, allow_html=False)
                    return {'can_preview': False, 'fixable': details, 'quality': 2}
                elif self.check_query(resource) and oauth_req == 'true' and not self.oauth2_is_enabled:
                   details = "Enable oauth2 extension"
                   h.flash_error(details, allow_html=False)
                   return {'can_preview': False, 'fixable': details, 'quality': 2}
                elif (resource['url'].lower().find('/querycontext') != -1 and 'payload' not in resource):
                    details = "Add a payload to complete the query"
                    h.flash_error(details, allow_html=False)
                    return {'can_preview': False, 'fixable': details, 'quality': 2}
                else:
                    return {'can_preview': True, 'quality': 2}
            else:
                return {'can_preview': False, 'fixable': 'Enable resource_proxy', 'quality': 2}
        else:
            return {'can_preview': False}

    def preview_template(self, context, data_dict):
        return 'ngsi.html'
##################
@@ -6459,21 +6459,16 @@
      if 
-self.
 check_qu
@@ -6794,21 +6794,16 @@
    elif 
-self.
 check_qu

call function error