#######################################################
# 
# ClientInformationController.py
# Python implementation of the Class ClientInformationController
# Generated by Enterprise Architect
# Created on:      19-May-2020 6:56:00 PM
# Original author: Natha Paquette
# 
#######################################################
from lxml import etree
from FreeTAKServer.controllers.model.Event import Event
from FreeTAKServer.controllers.BasicModelInstantiate import BasicModelInstantiate
import uuid
from logging.handlers import RotatingFileHandler
import logging
from FreeTAKServer.controllers.configuration.LoggingConstants import LoggingConstants
import sys
from FreeTAKServer.controllers.CreateLoggerController import CreateLoggerController
from FreeTAKServer.controllers.model.Event import Event
from FreeTAKServer.controllers.model.ClientInformation import ClientInformation

logger = CreateLoggerController("ClientInformationController").getLogger()

loggingConstants = LoggingConstants()

class ClientInformationController(BasicModelInstantiate):
    def __init__(self):
        pass
    '''
    connection setup is obsolete with intstantiateClientInformationModelFromController
    '''

    def intstantiateClientInformationModelFromConnection(self, rawClientInformation, queue):
        try:
            self.m_clientInformation = ClientInformation()
            argument = "initialConnection"
            self.m_clientInformation.dataQueue = queue
            self.m_clientInformation.socket = rawClientInformation.socket
            self.m_clientInformation.IP = rawClientInformation.ip
            self.m_clientInformation.idData = rawClientInformation.xmlString
            self.m_clientInformation.alive = 1
            self.m_clientInformation.ID = uuid.uuid1().int
            self.m_clientInformation.modelObject = Event.Connection(etree.fromstring(rawClientInformation.xmlString.encode()))
            return self.m_clientInformation
        except Exception as e:
            logger.error('error in client information controller '+str(e))
            return -1
        
        


    def connectionSetup(self, client, address):
        pass
        '''
        try:

            sqliteServer = sqlite3.connect(const.DATABASE)
            cursor = sqliteServer.cursor()

            first_run = 1
            #create client dictionary within main dictionary containing arrays for data and chat also other stuff for client enitial connection
            current_id = 0
            total_clients_connected = 0
            total_clients_connected += 1
            id_data = client.recv(const.STARTBUFFER)
            print(id_data)
            print('\n'+str(id_data))
            print('\n \n')
            tree = ET.fromstring(id_data)
            uid = tree.get('uid')
            if uid == self.bandaidUID:
                return 'Bandaid'
            callsign = tree[1][1].attrib['callsign']
            current_id = uuid.uuid1().int

            #add identifying information
            self.client_dict[current_id] = {'id_data': '', 'main_data': [], 'alive': 1, 'uid': '', 'client':client, 'callsign':callsign}
            self.client_dict[current_id]['id_data'] = id_data
            self.client_dict[current_id]['uid'] = uid
            cursor.execute(sql.INSERTNEWUSER,(str(current_id), str(uid), str(callsign)))
            sqliteServer.commit()
            cursor.close()
            sqliteServer.close()
            #print(self.client_dict)
            logger.info('client connected, information is as follows initial'+ '\n'+ 'connection data:'+str(id_data)+'\n'+'current id:'+ str(current_id))
            return str(first_run)+' ? '+str(total_clients_connected)+' ? '+str(id_data)+' ? '+str(current_id)
        except Exception as e:
            logger.warning('error in connection setup: ' + str(e))
            logger.warning(id_data)
            return "error"
    '''