import json
import os
import time

class padra():
    def __init__(self,len):
        self.len = len
        print("added",self.len)

    def toSend(self,x):
        j = json.dumps(x)
        l = self.len - len(str(len(j)))
        return "0000"[0:l]+str(len(j))+j

    def toJson(self,x):
        print( x)
        return json.loads(x)


    # user


    def xor(self,o,key,data):
        print("startetd")
        o0data= {
            "function":"xor",
            "players_number":"3",
            "player":"0",
            "ip":"192.168.178.105",
            "data":"",

        }
        o[0].send(self.toSend(o0data))
        o1data= {
            "function":"xor",
            "players_number":"3",
            "player":"1",
            "ip":"192.168.178.105",
            "data":key,

        }
        o[1].send(self.toSend(o1data))
        o2data= {
            "function":"xor",
            "players_number":"3",
            "player":"2",
            "ip":"192.168.178.105",
            "data":data
        }
        o[2].send(self.toSend(o2data))

    def aes(self,o,key,data):
        print("startetd")
        o0data= {
            "function":"aes",
            "players_number":"3",
            "player":"0",
            "ip":"192.168.178.105",
            "data":"",

        }
        o[0].send(self.toSend(o0data))
        o1data= {
            "function":"aes",
            "players_number":"3",
            "player":"1",
            "ip":"192.168.178.105",
            "data":key,

        }
        o[1].send(self.toSend(o1data))
        o2data= {
            "function":"aes",
            "players_number":"3",
            "player":"2",
            "ip":"192.168.178.105",
            "data":data
        }
        o[2].send(self.toSend(o2data))
        print("Data send from USER")
    
    # oracle
    def parse(self,message):
        data = json.loads(message)
        print(data)
        function = data["function"]
        if(function == "aes"):
            self.aesOracle(data["players_number"],data["player"],data["ip"],data["data"])
        elif(function == "xor"):
            self.xorOracle(data["players_number"],data["player"],data["ip"],data["data"])


    def aesOracle(self,players_number,player,ip,data):
        player=int(player)
        players_number=int(players_number)
        time.sleep(player*10)
        line = " "
        t1= "echo "+ data + " > gf2n_vals.in"
        t2= "./gen_input_f2n.x"
        t3= "mv ./gf2n_vals.out ./Player-Data/Private-Input-"+str(player)
        t4= "./mascot-party.x "+str(player)+" aes_m -pn 12598 -h "+ip+" -N "+str(players_number)
        handle = (os.system("ls"))
        print (handle)
        handle = (os.system("cd .."))
        print (handle)
        handle = (os.system("ls"))
        print (handle)
        handle = (os.system(t1))
        print (line)
        handle = (os.system(t2))
        print (line)
        handle = (os.system(t3))
        print (line)
        handle = (os.system(t4))
        print (line)
    
    def xorOracle(self,players_number,player,ip,data):
        player=int(player)
        print(player)
        players_number=int(players_number)
        time.sleep(player*10)
        line = " "
        if player != 0:
            t1= "echo 1 "+ data + " > gf2n_vals.in"
            handle = (os.system(t1))
            print (line)
        if player != 0:
            handle = (os.system("./gen_input_f2n.x"))
            print (line)
        if player != 0:
            handle = (os.system("mv ./gf2n_vals.out ./Player-Data/Private-Input-"+str(player)))
            print (line)


        print("player " + str(player) )
        t4= "./mascot-party.x "+str(player)+" xor -pn 12598 -h "+ip+" -N "+str(players_number)
        
        handle = (os.system(t4))
        print (line)

