from connection.parser import Parser



class UserParser(Parser):
    def __init__(self,user):
        self.user = user
        print("createt User Parser")

    def parser(self,data,addr,Connection):
        try:
            if data["type"] == "responseUserBroadcast":
                self.responseUserBroadcast(data,addr) 
        except:
            print("\33[41mAn exception occurred DATA\33[0m" )



# Broadcast Parser
    def requestOracleBroadcast(self, id):
        try:
            data = {}
            self.add(data,"type","requestOracleBroadcast")
            self.add(data,"id",id)
            return self.toSEND(data)
        except:
            print("\33[41mAn exception occurred requestOracleBroadcast\33[0m" )
    
    def responseUserBroadcast(self, data,addr):
        try:
            self.user.onlineOracles.append((addr[0],data["socket"]))
        except:
            print("\33[41mAn exception occurred responseUserBroadcast" )

# Socket Parser
    def socketConnecting(self,oracleNumber):
        try:
            data = {}
            self.add(data,"type","socketConnecting")
            self.add(data,"oracle",oracleNumber)
            return self.toSEND(data)
        except:
            print("\33[41mAn exception occurred requestOracleBroadcast\33[0m" )

# Socket Parser
    def oracleInitialConnector(self,server,cryptor,bearer):
        try:
            data = {}
            self.add(data,"type","oracleInitial")
            self.add(data,"server",server)
            self.add(data,"bearer",bearer)
            self.add(data,"cryptor",cryptor)
            return self.toSEND(data)
        except:
            print("\33[41mAn exception occurred oracleInitialConnector\33[0m" )
    def oracleInitialCryptor(self,connector,server):
        try:
            data = {}
            self.add(data,"type","oracleInitial")
            self.add(data,"connector",connector)
            self.add(data,"server",server)
            return self.toSEND(data)
        except:
            print("\33[41mAn exception occurred oracleInitialCryptor\33[0m" )
    def oracleInitialBearer(self,connector,d):
        try:
            data = {}
            self.add(data,"type","oracleInitial")
            self.add(data,"connector",connector)
            self.add(data,"data",d)
            return self.toSEND(data)
        except:
            print("\33[41mAn exception occurred oracleInitialBearer\33[0m" )