import logging

from django.urls import include, path, re_path
from rest_framework import routers
from rest_framework.authtoken import views

import console.api.users
from console.api import celery
from console.api.connections import ConnectionViewSet
from console.views.index import page_not_found
from core.views.view_login import LoginView, LogoutView
from core.views.view_settings import ProductVersionView
from networkmap.api import (AssetDangerViewSet, AutoNetmapConnectionsViewSet,
                            AutoNetmapElementsViewSet, NetmapElementsViewSet,
                            NetmapGroupsViewSet, UserMapViewSet)

_log = logging.getLogger()

router = routers.DefaultRouter()
router.register('connections', ConnectionViewSet, basename='connections')
router.register('netmap/static/elements', NetmapElementsViewSet, basename='netmap-elements')
router.register('netmap/auto/elements', AutoNetmapElementsViewSet, basename='auto-netmap-elements')
router.register('netmap/auto/connections', AutoNetmapConnectionsViewSet, basename='auto-netmap-connections')
router.register('netmap/groups', NetmapGroupsViewSet, basename='netmap-groups')
router.register('netmap/maps', UserMapViewSet, basename='netmap-maps')
router.register('netmap/in-danger', AssetDangerViewSet, basename='netmap-in-danger')

urlpatterns = (
    path('', include(router.urls)),
    path('devices/', include('devices.urls')),
    path('store/', include('storage.urls')),
    path('assets/', include('assets.urls')),
    path('inputs/', include('inputs.urls')),
    path('license/', include('license_info.urls')),
    path('logstash/', include('logstash.urls')),
    path('dashboard/', include('dashboard.api_urls')),
    path('ncircc/', include('ncircc.urls')),
    path('company/', include('company.urls')),
    path('correlation/', include('correlation.urls')),
    path('netmap/', include('networkmap.urls')),
    path('incidents/', include('incident.urls')),
    path('incident_export/', include('incident_export.urls')),
    path('rotation/', include('rotation.urls')),
    path('endpoint/', include('devices.urls_endpoint')),
    path('users/', include('users.urls')),
    path('notifications/', include('notifications.urls')),
    path('', include('core.urls')),
    path('groups/', console.api.users.all_groups, name='api_get_all_groups'),
    path('groups/<str:g_name>/perms', console.api.users.group_perms, name='api_group_perms'),
    path('groups/except_<str:g_name>', console.api.users.all_perms_wo_group, name='api_get_excluded_group_perms'),
    path('groups/linked_permissions/', console.api.users.get_linked_permissions, name='api_get_linked_permissions'),
    path('groups/<str:g_name>/users', console.api.users.group_users, name='api_get_group_users'),
    path('groups/<str:g_name>/available_users',
         console.api.users.group_users_excluded,
         name='api_get_available_group_users'),
    path('groups/<str:g_name>/update/perms',
         console.api.users.update_group_permission,
         name='api_update_group_permissions'),
    path('groups/<str:g_name>/udpate/users', console.api.users.update_group_users, name='api_update_group_users'),
    path('groups/<str:g_name>/remove', console.api.users.delete_group_api, name='api_delete_group'),
    path('groups/<path:g_name>/add', console.api.users.add_new_group, name='api_add_new_group'),
    path('groups/<str:g_name>/rename/<path:new_g_name>', console.api.users.rename_group, name='api_rename_group'),
    path('groups/<str:g_name>/copy/<path:new_g_name>', console.api.users.copy_group, name='api_copy_group'),
    path('celery/<str:task_id>/', celery.check_task_state, name='celery_check_task_state'),
    path('auth/token/', views.obtain_auth_token, name='auth_get_token'),
    path('events/', include('events.api_urls')),
    path('logout/', LogoutView.as_view(), name='logout'),
    path('login/', LoginView.as_view(), name='api_login'),
    path('product/version/', ProductVersionView.as_view(), name='api_product_version'),
    re_path(r'^', page_not_found, name='not_found'),
)
