# Copyright  2015, Province of British Columbia
# License: https://github.com/bcgov/ckanext-bcgov/blob/master/license

from ckan.common import  c, _

import pylons.config as config
import ckan.lib.base as base
import ckan.plugins as plugins
import ckan.plugins.toolkit as toolkit

from routes.mapper import SubMapper

from ckanext.bcgov.util.util import (get_edc_tags,
                                          edc_type_label,
                                          get_state_values,
                                          get_username,
                                          get_user_orgs,
                                          get_user_role_orgs,
                                          get_user_orgs_id,
                                          get_user_toporgs,
                                          get_organization_branches,
                                          get_all_orgs
                                          )

from ckanext.bcgov.util.helpers import (get_suborg_sector,
                                             get_user_dataset_num,
                                             get_package_data,
                                             is_license_open,
                                             get_record_type_label,
                                             get_suborgs,
                                             record_is_viewable,
                                             get_facets_selected,
                                             get_facets_unselected,
                                             get_sectors_list,
                                             get_dataset_type,
                                             get_organizations,
                                             get_organization_title,
                                             get_espg_id,
                                             get_edc_org,
                                             get_iso_topic_values,
                                             get_eas_login_url,
                                             get_fqdn,
                                             get_environment_name,
                                             get_version,
                                             get_bcgov_commit_id,
                                             resource_prefix,
                                             )

abort = base.abort


class SchemaPlugin(plugins.SingletonPlugin):

    plugins.implements(plugins.IConfigurer)

    plugins.implements(plugins.IRoutes, inherit=True)

    plugins.implements(plugins.ITemplateHelpers, inherit=False)

    plugins.implements(plugins.IPackageController, inherit=True)

    plugins.implements(plugins.IFacets, inherit=True)

    plugins.implements(plugins.IActions, inherit=True)

    def get_helpers(self):
        return {
                "dataset_type" : get_dataset_type,
                "edc_tags" : get_edc_tags,
                "edc_orgs" : get_organizations,
                "edc_org_branches" : get_organization_branches,
                "edc_org_title" : get_organization_title,
                "edc_type_label" : edc_type_label,
                "edc_state_values" : get_state_values,
                "edc_username": get_username,
                "get_sector" : get_suborg_sector,
                "get_user_orgs" : get_user_orgs,
                "get_user_orgs_id" : get_user_orgs_id,
                "get_user_toporgs": get_user_toporgs,
                "get_suborg_sector" : get_suborg_sector,
                "get_user_dataset_num" : get_user_dataset_num,
                "get_edc_package" : get_package_data,
                "is_license_open" : is_license_open,
                "record_type_label" : get_record_type_label,
                "get_suborgs": get_suborgs,
                "record_is_viewable": record_is_viewable,
                "get_espg_id" : get_espg_id,
                "get_user_role_orgs" : get_user_role_orgs,
                "get_all_orgs" : get_all_orgs,
                "get_facets_selected": get_facets_selected,
                "get_facets_unselected" : get_facets_unselected,
                "get_sectors_list": get_sectors_list,
                "get_edc_org" : get_edc_org,
                "get_iso_topic_values" : get_iso_topic_values,
                "get_eas_login_url": get_eas_login_url,
                "get_fqdn": get_fqdn,
                "get_environment_name": get_environment_name,
                "get_version": get_version,
                "get_bcgov_commit_id": get_bcgov_commit_id,
                "googleanalytics_resource_prefix": resource_prefix,
                }


    def update_config(self, config):
        toolkit.add_public_directory(config, 'public')
        toolkit.add_template_directory(config, 'templates')
        toolkit.add_resource('fanstatic', 'edc_resource')
        toolkit.add_resource('public/scripts', 'theme_scripts')


    #Customizing action mapping
    def before_map(self, map):

        from routes.mapper import SubMapper
        package_controller = 'ckanext.bcgov.controllers.package:EDCPackageController'
        user_controller = 'ckanext.bcgov.controllers.user:EDCUserController'
        org_controller = 'ckanext.bcgov.controllers.organization:EDCOrganizationController'
        site_map_controller = 'ckanext.bcgov.controllers.site_map:GsaSitemapController'
        api_controller = 'ckanext.bcgov.controllers.api:EDCApiController'

#        map.redirect('/', '/dataset')
        map.connect('package_index', '/', controller=package_controller, action='index')

        map.connect('/dataset/add', controller=package_controller, action='typeSelect')

        with SubMapper(map, controller=package_controller) as m:
            m.connect('add dataset', '/dataset/new', action='new')
            #m.connect('dataset_edit', '/dataset/edit/{id}', action='edc_edit',ckan_icon='edit')
            m.connect('search', '/dataset', action='search', highlight_actions='index search')
            m.connect('dataset_read', '/dataset/{id}', action='read', ckan_icon='sitemap')
            m.connect('duplicate', '/dataset/duplicate/{id}', action='duplicate')
            m.connect('/dataset/{id}/resource/{resource_id}', action='resource_read')
            m.connect('/dataset/{id}/resource_delete/{resource_id}', action='resource_delete')
            m.connect('/authorization-error', action='auth_error')
            m.connect('resource_edit', '/dataset/{id}/resource_edit/{resource_id}', action='resource_edit', ckan_icon='edit')
            m.connect('new_resource', '/dataset/new_resource/{id}', action='new_resource')

        with SubMapper(map, controller=user_controller) as m:
            m.connect('user_dashboard_unpublished', '/dashboard/unpublished',
                  action='dashboard_unpublished', ckan_icon='group')
            m.connect('/user/edit', action='edit')
            m.connect('/user/activity/{id}/{offset}', action='activity')
            m.connect('user_activity_stream', '/user/activity/{id}',
                  action='ac
tivity', ckan_icon='time')
            m.connect('user_dashboard', '/dashboard', action='dashboard',
                  ckan_icon='list')
            m.connect('user_dashboard_datasets', '/dashboard/datasets',
                  action='dashboard_datasets', ckan_icon='sitemap')
            m.connect('user_dashboard_organizations', '/dashboard/organizations',
                  acti
on='dashboard_organizations', ckan_icon='building')
            m.connect('/dashboard/{offset}', action='dashboard')
            m.connect('user_follow', '/user/follow/{id}', action='follow')
            m.connect('/user/unfollow/{id}', action='unfollow')
            m.connect('user_followers', '/user/followers/{id:.*}',
                  action='followers', ckan_icon='group')
            m.connect('user_edit', '/user/edit/{id:.*}', action='edit',
                  ckan_icon='cog')
            m.connect('user_delete', '/user/delete/{id}', action='delete')
            m.connect('/user/reset/{id:.*}', action='perform_reset')
            m.connect('register', '/user/register', action='register')
            m.connect('login', '/user/logi