from django.shortcuts import render,redirect
from django.http import HttpResponse, JsonResponse
from django.conf import settings
from django.apps import apps
from django.contrib import messages

from django.core.mail import send_mail
from django.core.paginator import Paginator
from django.views.generic.edit import FormView,CreateView
from django.forms.models import model_to_dict
from django.views.decorators.csrf import csrf_exempt


from .models import *
import socket
import hubIA.views as hubIA

ADMIN_ROUTE = "/station/"
NETWORK_NAME = settings.NETWORK_NAME 
#IP = 
apps_installed = settings.INSTALLED_APPS

# Create your sessions name here
SESSIONS_NAME = [
	"actual_network"
]

def force_admin(request,bad_link=""):
	return redirect(ADMIN_ROUTE)

def config(request):
	template = "hub/config.html"
	context = dict()
	return render(request,template,context)

def features(request):
	template = "hub/features.html"
	context = dict()

	plugins = [
		{
			"name":"Maketteur",
			"img":"/static/global/maketteur.jpeg",
			"description":"Definissez les differentes donnees que vous utiliserez sur HubStation ",
			"link":"/maketteur/"
		},
		{
			"name":"Autographe",
		 	"img":"/static/global/autograph.png",
			"link":"/autograph/",
			"description":"des Graphes, courbes et autres schemas graphiques pour illustrer le comportement de vos donnees"
		},
		{
			"name":"EasyLife",
			"img":"/static/global/smile.jpg",
			"link":"/easylife/",
			"description":"Des Raccourcis pour effectuer des requetes longues en une interface"
		},
		{
			"name":"ExcelHub",
			"img":"/static/global/excel.png",
			"link":"/excelhub/",
			"description":"Importez et Exportez des donnees excel a partir de votre HubStation"
		},
		{
			"name":"TinyMce Editor",
			"img":"/static/global/tinymce logo.png",
			"link":"/tiny_editor/",
			"description":"Editez et partagez des documents en ligne avec votre equipe"		
		},
		{
			"name":"Desidis",
			"img":"/static/global/image-regular.png",
			"link":"/desidis/",
			"description":"Utilitaire pour personnaliser l'Apparence des interfaces de Donnees"
		},
		{
			"name":"CalendApp",
			"img":"/static/global/calendar-svgrepo-com.svg",
			"link":"/calendapp/",
			"description":"Un Agenda interactif pour vous et votre equipe"
		},
		{
			"name":"StickNotes",
			"img":"/static/global/creaform.png",
			"link":"/calendapp/",
			"description":"Notifiez les Utilisateurs de votre Station sur des actions specifiques"
		}		
	]
	paginator = Paginator(plugins,12)
	try:
		page_number = request.GET.get('page')
	except:
		page_number = 1

	page_obj = paginator.get_page(page_number)
	context['plugins']=page_obj
	context['num_pages']=paginator.num_pages
	context['page_range']=paginator.page_range

	return render(request,template,context)

def hub_infos(request,nature="name"):
	return HttpResponse(NETWORK_NAME)

def get_ip_locale():
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    try:
        # Connexion fictive pour obtenir l'IP locale
        s.connect(("8.8.8.8", 80))
        ip = s.getsockname()[0]
    except:
        ip = "127.0.0.1"
    finally:
        s.close()
    return ip

# =============================================================================================

def index(request):
	template = "hub/index.html"
	context = {
		"NETWORK_NAME":NETWORK_NAME,
		"SERVER_IP":get_ip_locale()
	}
	return render(request,template,context)

def lm(request):
	template = "hub/hubIA.html"
	context = {
		"NETWORK_NAME":NETWORK_NAME,
		"SERVER_IP":get_ip_locale()
	}
	return render(request,template,context)

# =============================================================================================

# ===============================================================================================
def IA_chat(request):
	message = request.GET.get("message")
	answer = hubIA.ia_chat(message)
	data = {
		"answer":answer
	}
	return JsonResponse( data)