import subprocess

from itertools import chain

from django.shortcuts import render
from django.template import loader
from django.urls import reverse
from django.utils import timezone
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib import messages
from django.db.models import Q


from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.forms import AuthenticationForm, UserCreationForm
from django.contrib.auth.models import User

from core.views import gen_page_base, gen_page_sys, get_get_value, get_data_value
from .forms import *
from .models import *

def test_last_client_NUnic(type): 
	if type == "PC":
		range_value = "range-pc"

	elif type == "IMP":
		range_value = "range-imp"
	else :
		range_value = "blop"

	data_range = get_data_value(range_value)

	print( "Get new last ID")
	range_list = data_range.split(";")
	for value in range(int(range_list[0]), int(range_list[1])):
		try:
			test = PIC.objects.get(PIC_NUnic = value)
		except:
			print("OK")
			break
	return value

def switch_get_list(request):
	template = loader.get_template('parc_info_api_switch_get_list.html')

	get_search = request.GET.get('get_search', '')
	if get_search :
		#print(get_search)
		data_query = Swich.objects.exclude(SW_Archive=True).filter(Q(SW_Nom__icontains=get_search)
										|Q(SW_Baie__BAIE_Site_NG__CLIN_Anag__icontains=get_search)
										|Q(SW_Baie__BAIE_Nom__icontains=get_search)
										|Q(SW_IP__icontains=get_search)
										|Q(SW_Commentaires__icontains=get_search)
										).order_by('SW_Baie__BAIE_Site_NG','SW_Baie','SW_Nom')
	else :
		#print("not get_search")
		data_query = Swich.objects.exclude(SW_Archive=True).all().order_by('SW_Baie__BAIE_Site_NG','SW_Baie','SW_Nom')

	html = template.render({
			'data_query': data_query,
		}, request)
		
	return HttpResponse(html)

def switch_edit_info(request, id):
	get_search = request.GET.get('get_search', '')
	if not get_search:
		get_search = ""
	
	template = loader.get_template('parc_info_api_switch_edit.html')
	if id == None or id == '0':
		item = Swich()
		item.id = 0
	else :
		item = Swich.objects.get(id = id)

	form = Edit_switch_form(instance=item)

	html = template.render({
			'get_search':get_search,
			'form': form,
			'item': item,
		}, request)
		
	return HttpResponse(html)

def switch_edit_port(request, switch_id, port_id ):
	template = loader.get_template('parc_info_api_switch_edit_port.html')
	if port_id == None or port_id == '0':
		item = Port_Link()
		item.PL_Swich = Swich.objects.get(id = switch_id)
		item.id = 0
		form = Edit_switch_port_form(instance=item)
	else :
		item = Port_Link.objects.get(id = port_id)
		if item.PL_Port_NG == "old":
			item.PL_Port_NG = item.PL_Port
		form = Edit_switch_port_form(instance=item)

	html = template.render({
			'form': form,
			'item': item,
		}, request)
	
	return HttpResponse(html)

def switch_update_port(request, switch_id):
	template = loader.get_template('parc_info_api_switch_update.html')
	item = Swich.objects.get(id = switch_id)

	form = Update_switch_port(instance=item)

	html = template.render({
			'form': form,
			'item': item,
		}, request)
		
	return HttpResponse(html)

def switch_get_info(request, id):
	template = loader.get_template('parc_info_api_switch_get.html')
	item = Swich.objects.get(id = id)
	item.ports = Port_Link.objects.filter(PL_Swich = item.id).order_by('PL_Port_NG')

	html = template.render({
			'item': item,
		}, request)
		
	return HttpResponse(html)

def srv_get_list(request):
	template = loader.get_template('parc_info_api_srv_get_list.html')

	get_search = request.GET.get('get_search', '')
	if get_search :
		#print(get_search)
		data_query = PIS.objects.filter(Q(PIS_Nom_netbios__icontains=get_search)
										|Q(PIS_Friendly_Name__icontains=get_search)
										|Q(PIS_Site__CLIN_Anag__icontains=get_search)
										|Q(PIS_Adresse_IP__icontains=get_search)
										|Q(PIS_Role__icontains=get_search)
										|Q(PIS_SN__icontains=get_search)
										|Q(PIS_Adresse_Mac__icontains=get_search)
										).order_by('PIS_Archive','PIS_Site','PIS_Role', 'PIS_Adresse_IP')
	else :
		#print("not get_search")
		data_query = PIS.objects.all().order_by('PIS_Archive','PIS_Site','PIS_Role', 'PIS_Adresse_IP')

	html = template.render({
			'data_query': data_query,
		}, request)
		
	return HttpResponse(html)

def srv_get_info(request, id_srv):
	template = loader.get_template('parc_info_api_srv_get.html')
	item = PIS.objects.get(id = id_srv)

	html = template.render({
			'item': item,
		}, request)
		
	return HttpResponse(html)

def srv_edit_info(request, id_srv):
	get_search = request.GET.get('get_search', '')
	if not get_search:
		get_search = ""
	
	template = loader.get_template('parc_info_api_srv_edit.html')
	if id_srv == None or id_srv == '0':
		item = PIS()
		item.id = 0
	else :
		item = PIS.objects.get(id = id_srv)

	form = Edit_srv_form(instance=item)

	html = template.render({
			'get_search':get_search,
			'form': form,
			'item': item,
		}, request)
		
	return HttpResponse(html)

def user_get_list(request):
	template = loader.get_template('parc_info_api_user_get_list.html')

	get_search = request.GET.get('get_search', '')
	if get_search :
		#print(get_search)
		data_query_1 = PIC.objects.exclude(PIC_L_Statut__in = ['INAPTE', 'EN STOCK']).filter(Q(PIC_Nom_netbios__icontains=get_search)
										|Q(PIC_Site__CLIN_Anag__icontains=get_search)
										|Q(PIC_Adresse_IP__icontains=get_search)
										|Q(PIC_Utilisateur__icontains=get_search)
										|Q(PIC_Utilisateur_Fq__icontains=get_search)
										|Q(PIC_Adresse_Mac__icontains=get_search)
										|Q(PIC_Service__SERVICE_Nom__icontains=get_search)
										|Q(PIC_SN__icontains=get_search)
										|Q(PIC_L_Chassi_Type__icontains=get_search)
										).order_by('PIC_Site__CLIN_Anag','PIC_Service__SERVICE_Nom','PIC_NUnic','PIC_L_Statut')
		data_query_2 = PIC.objects.filter(PIC_L_Statut =  'EN STOCK').filter(Q(PIC_Nom_netbios__icontains=get_search)
										|Q(PIC_Site__CLIN_Anag__icontains=get_search)
										|Q(PIC_Adresse_IP__icontains=get_search)
										|Q(PIC_Utilisateur__icontains=get_search)
										|Q(PIC_Utilisateur_Fq__icontains=get_search)
										|Q(PIC_Adresse_Mac__icontains=get_search)
										|Q(PIC_Service__SERVICE_Nom__icontains=get_search)
										|Q(PIC_SN__icontains=get_search)
										|Q(PIC_L_Chassi_Type__icontains=get_search)
										).order_by('PIC_Site__CLIN_Anag','PIC_Service__SERVICE_Nom','PIC_NUnic')
		data_query = list(chain(data_query_1, data_query_2))

	else :
		#print("not get_search")
		data_query = PIC.objects.exclude(PIC_L_Statut__in = ['INAPTE', 'EN STOCK']).order_by('PIC_Site__CLIN_Anag','PIC_Service__SERVICE_Nom','PIC_NUnic','PIC_L_Statut')

	for data in data_query:
		data.accessoir = Accessory_Link.objects.filter(pic = data)

	html = template.render({
			'data_query': data_query,
		}, request)
		
	return HttpResponse(html)

def user_edit_info(request, id_user):
	get_search = request.GET.get('get_search', '')
	if not get_search:
		get_search = ""

	template = loader.get_template('parc_info_api_user_edit.html')
	if id_user == None or id_user == '0':
		item = PIC()
		item.id = 0
	else :
		item = PIC.objects.get(id = id_user)

	if item.PIC_L_Chassi_Type == "IMP":
		form = Edit_imp_form(instance=item)
	else:
		form = Edit_user_mini_form(instance=item)

	html = template.render({
			'form': form,
			'item': item,
			'get_search':get_search,
		}, request)
		
	return HttpResponse(html)

def user_edit_statut(request, id_user):
	get_search = request.GET.get('get_search', '')
	if not get_search:
		get_search = ""

	template = loader.get_template('parc_info_api_user_statut.html')
	if id_user == None or id_user == '0':
		item = PIC()
		item.id = 0
	else :
		item = PIC.objects.get(id = id_user)
	
	form = Edit_user_statut_form(instance=item)

	html = template.render({
			'form': form,
			'item': item,
			'get_search':get_search,
		}, request)
		
	return HttpResponse(html)

def user_get_info(request, id_user):
	template = loader.get_template('parc_info_api_user_get.html')
	item = PIC.objects.get(id = id_user)
	item.accessoir = Accessory_Link.objects.filter(pic = item)

	html = template.render({
			'item': item,
		}, request)
		
	return HttpResponse(html)

def user_get_hist(request, id_user):
	template = loader.get_template('parc_info_api_user_get_hist.html')
	tmp = PIC.objects.get(id = id_user)
	print(id_user)
	print(tmp)
	data_query = PIC_History.objects.filter(H_PIC = tmp).order_by('-H_Date')
	print(data_query)

	html = template.render({
			'data_query': data_query,
		}, request)
		
	return HttpResponse(html)

def user_start_novnc(request, cl_hostname):
	print(cl_hostname)
	cl_hostname = cl_hostname + ":5900"
	subprocess.Popen(["tmux", "new", "-d", "/root/projects/noVNC-1.3.0/utils/novnc_proxy", "--vnc", cl_hostname, "--timeout", "60","--idle-timeout", "60" ], stdout=subprocess.PIPE)
	return HttpResponse("http://SRV-GParc:6080/vnc.html?host=SRV-GParc&port=6080")

def srv_update(request):
	data_query = PIS.objects.all()
	for item in data_query:
		item.save()
	return HttpResponse("ok")

def user_update(request):
	data_query = PIC.objects.all()
	for item in data_query:
		# try:
		# 	tmp_service = Services.objects.get(SERVICE_Nom = item.PIC_Service)
		# except:
		# 	tmp_service = Services.objects.get(SERVICE_Nom = "VOID")
		# item.PIC_Service = tmp_service
		# if item.PIC_L_Chassi_Type == "CL" or item.PIC_L_Chassi_Type == "PC" :
		# 	try :
		# 		list_filter_ecran = Accessory_Link.objects.get(pic = item)
		# 	except :
		# 		item.PIC_Accessory.add(Accessory_Link.objects.get(Accessory_N_slugify = "ecran"))
		if item.PIC_Site.CLIN_Anag == "GEN":
			item.PIC_Site.CLIN_Anag = "HPN"

		item.save()
	return HttpResponse("ok")