from visitor_mvs import VisitorMvS
import urllib
import urllib2

class VisitorObjects(VisitorMvS):
    def __init__(self, args):
        self.username = args[0]
        self.obj_file = args[1]
        self.real_file = args[2]
        self.mode = args[3]
        VisitorMvS.__init__(self, [None])

        with open(self.real_file, 'r') as f:
            import hashlib
            md5 = hashlib.md5()
            md5.update(f.read())
        self.hash_file = md5.hexdigest()

        # Check if file is already compiled (with same hash) in Modelverse
        urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '"%s"' % self.username, "username": "user_manager"}))).read()
        urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '3', "username": self.username}))).read()
        urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '"is_defined"', "username": self.username}))).read()
        urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '"%s"' % self.obj_file, "username": self.username}))).read()

        v = urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "get_output", "username": self.username}))).read()
        v = v.split("=", 2)[2]
        if v == "None":
            # Not defined, so recompile
            print("[CC] %s" % self.real_file)
        else:
            # Is defined already, so let's compare hashes
            if v != self.hash_file:
                print("[RC] %s" % self.real_file)

                # Remove in Modelverse and recompile
                urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '3', "username": self.username}))).read()
                urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '"remove_obj"', "username": self.username}))).read()
                urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '"%s"' % self.obj_file, "username": self.username}))).read()
            else:
                self.visit = lambda i: i
                self.dump = lambda: True
                print("[SO] %s" % self.real_file)


    def dump(self):
        v = VisitorMvS.dump(self)
        import json
        # Set up interface
        urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '3', "username": self.username}))).read()

        # Start uploading the code
        urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '"upload"', "username": self.username}))).read()
        urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '"%s"' % self.obj_file, "username": self.username}))).read()
        urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '"%s"' % self.hash_file, "username": self.username}))).read()
        urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": 'false', "username": self.username}))).read() # Use old interface
        urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": json.dumps(v), "username": self.username}))).read()

        # Upload symbol table
        data = []
        for e, v in self.object_symbols.iteritems():
            data.append(["V", "true"])
            data.append(["V", '"%s"' % e])
            data.append(["V", "true" if v else "false"])
        urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "data": json.dumps(data), "username": self.username}))).read()

        # Finish the symbol table
        urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": 'false', "username": self.username}))).read()
        return True
