import socket
import threading
import config as cfg
from nacl.public import PrivateKey
from connection.socket import SocketClient
from tcp import TLSClientSession
import time
def new_x25519():
    private_key = PrivateKey.generate()
    key_exchange = bytes(private_key.public_key)
    return bytes(private_key).hex() , key_exchange.hex()

class Cryptor():

    def __init__(self, parser):
        self.privatekey , self.key_exchange = new_x25519()
        self.server = None
        self.parser = parser
        self.connector = None
        self.data = []
        print("\n\n CRYPTOR,  \n ",self.privatekey ,self.key_exchange,"\n")

    def oracleInitial(self,data):
        try:
            self.server = data["server"]
            self.connector = SocketClient(data["connector"][0],data["connector"][1],self.parser,"Cryptor")
            time.sleep(1)

            self.keyExchange()
        except:
            print("\33[41mAn exception occurred oracleInitial Cryptor\33[0m" )   
    # def socketOracle(self,HOST,PORT):
    #     self.connector = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    #     self.connector.connect((HOST, PORT))
    #     t = threading.Thread(target=self.receive)
    #     t.start()
    #     self.toSEND({
    #         "type":"oracleInit",
    #         "oracle":"Cryptor"
    #     })
    #     print("socket is created and Listener is created\n")
    #     self.toSEND({
    #         "type":"key_exchange",
    #         "key_exchange":self.key_exchange
    #     })


    def keyExchange(self):
        try:
            d = {}
            self.parser.add(d,"type","keyExchange")
            self.parser.add(d,"keyExchange",self.key_exchange)
            self.connector.send(self.parser.toSEND(d))
        except:
            print("\33[41mAn exception occurred keyExchange Cryptor\33[0m")
    # def start(self,a):
    #     print("Cryptor",a)
    #     self.socketOracle(self.connectorIP,self.connectorPort)

    def fromContoCryHelloCLient(self, data):
        try:
            self.tcp = TLSClientSession(self.privatekey , self.key_exchange,memoryview(bytes.fromhex(data["data"])))

            print("TODO fromContoCry",data["data"]) # TODO
        except Exception as exception :
            print("\33[41mAn exception occurred fromContoCryHelloCLient Cryptor " ,exception,"\33[0m" )


    def toCryptorFromConnector(self,data):
        try:
            print("TODO") # TODO
        except Exception as exception :
            print("\33[41mAn exception occurred toCryptorFromConnector Cryptor " ,exception,"\33[0m" )   

        
    def MPC(self,data):
        try:
            print("TODO") # TODO
        except Exception as exception :
            print("\33[41mAn exception occurred MPC Cryptor " ,exception,"\33[0m" )   

    def fromContoCry(self,data):
        try:
            self.tcp.tls_response(data["data"])
            print("TODO fromContoCry") # TODO
        except Exception as exception :
            print("\33[41mAn exception occurred fromContoCry Cryptor " ,exception,"\33[0m" )






