from connection.parser import Parser
from .worker.cryptor import Cryptor
from .worker.connector import Connector
from .worker.bearer import Bearer

class OracleParser(Parser):
    def __init__(self,oracle):
        self.socket = oracle.socketPort
        self.oracle = oracle
        self.type = None
        self.id = None
        print("createt oracle Parser")

    def parser(self,data,addr,Connection):
        try:
            # Broadcast Parser
            if data["type"] == "requestOracleBroadcast":
                self.requestOracleBroadcast(data,addr) 
            # Socket Parser
            elif data["type"] == "socketConnecting":
                self.socketConnecting(data)
            elif data["type"] == "oracleInitial":
                self.type.oracleInitial(data)
            elif data["type"] == "keyExchange":
                self.type.keyExchange(data)
            elif data["type"] == "fromContoCry":
                self.type.fromContoCry(data)
            elif data["type"] == "fromConMPC":
                self.type.MPC(data)
            elif data["type"] == "fromCrytoCon":
                self.type.fromCrytoCon(data)
            elif data["type"] == "fromContoCryHelloCLient":
                self.type.fromContoCryHelloCLient(data)
        except :
            print("\33[41m\33[41mAn exception occurred parser" ,data["type"],self.type,"\33[0m")

    def requestOracleBroadcast(self,data,addr):
        try:
            d = {}
            self.add(d,"type","responseUserBroadcast")
            self.id = data["id"]
            self.add(d,"message","Hello "+data["id"])
            self.add(d,"socket",self.socket)
            self.oracle.b.send(self.toSEND(d),addr)
        except:
            print("\33[41mAn exception occurred requestOracleBroadcast\33[0m")

    def socketConnecting(self,data):
        try:
            if data["oracle"] == 0: 
                self.type = Connector(self,self.socket+1)
            elif data["oracle"] == 1: 
                self.type = Cryptor(self)
            elif data["oracle"] == 2: 
                self.type = Bearer(self)
            else:
                print("An exception occurred socketConnecting Oracle data ")
        except:
            print("\33[41mAn exception occurred socketConnecting\33[0m")


    # def fromConMPC(self,function,port,connection):
    #     try:
    #         d = {}
    #         self.add(d,"type","fromConMPC")
    #         self.add(d,"function",function)
    #         self.add(d,"port",port)
    #         connection.send(self.toSEND(d))
    #     except Exception as exception:
    #         print("\33[41mAn exception occurred Parse fromContoCryMPC ",exception,"\33[0m")

    # def fromCrytoCon(self,data,connection):
    #     try:
    #         d = {}
    #         self.add(d,"type","fromCrytoCon")
    #         self.add(d,"data",data)
    #         connection.send(self.toSEND(d))
    #     except Exception as exception:
    #         print("\33[41mAn exception occurred Parse fromCrytoCon ",exception,"\33[0m")

    def parserSend(self,data,connection,typeO):
        try:
            d = {}
            self.add(d,"type",typeO)
            self.add(d,"data",data)
            connection.send(self.toSEND(d))
        except Exception as exception:
            print("\33[41mAn exception occurred Parse ",typeO ,exception,"\33[0m")
