from constructors_visitor import ConstructorsVisitor

import urllib
import urllib2

timeout = 100

class ConstructorsObjectVisitor(ConstructorsVisitor):
    def __init__(self, args):
        ConstructorsVisitor.__init__(self, args)

        self.username = args[0]
        self.obj_file = args[1]
        self.real_file = args[2]
        self.address = args[3]

        self.object_symbols = {}

        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(self.address, urllib.urlencode({"op": "set_input", "element_type": "V", "value": '"%s"' % self.username, "username": "user_manager"}))).read()
        urllib2.urlopen(urllib2.Request(self.address, urllib.urlencode({"op": "set_input", "element_type": "V", "value": '3', "username": self.username}))).read()
        urllib2.urlopen(urllib2.Request(self.address, urllib.urlencode({"op": "set_input", "element_type": "V", "value": '"is_defined"', "username": self.username}))).read()
        urllib2.urlopen(urllib2.Request(self.address, urllib.urlencode({"op": "set_input", "element_type": "V", "value": '"%s"' % self.obj_file, "username": self.username}))).read()

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

                # Remove in Modelverse and recompile
                urllib2.urlopen(urllib2.Request(self.address, urllib.urlencode({"op": "set_input", "element_type": "V", "value": '3', "username": self.username}))).read()
                urllib2.urlopen(urllib2.Request(self.address, urllib.urlencode({"op": "set_input", "element_type": "V", "value": '"remove_obj"', "username": self.username}))).read()
                urllib2.urlopen(urllib2.Request(self.address, 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("[CACHED] %s" % simple_filename)

    def dump(self):
        v = ConstructorsVisitor.dump(self)

        import json
        # Set up interface
        urllib2.urlopen(urllib2.Request(self.address, urllib.urlencode({"op": "set_input", "element_type": "V", "value": '3', "username": self.username}))).read()

        # Start uploading the code
        urllib2.urlopen(urllib2.Request(self.address, urllib.urlencode({"op": "set_input", "element_type": "V", "value": '"upload"', "username": self.username}))).read()
        urllib2.urlopen(urllib2.Request(self.address, urllib.urlencode({"op": "set_input", "element_type": "V", "value": '"%s"' % self.obj_file, "username": self.username}))).read()
        urllib2.urlopen(urllib2.Request(self.address, urllib.urlencode({"op": "set_input", "element_type": "V", "value": '"%s"' % self.hash_file, "username": self.username}))).read()
        urllib2.urlopen(urllib2.Request(self.address, urllib.urlencode({"op": "set_input", "element_type": "V", "value": 'true', "username": self.username}))).read() # Use new interface

        def flush_data(data):
            if data:
                urllib2.urlopen(urllib2.Request(self.address, urllib.urlencode({"op": "set_input", "data": json.dumps(data), "username": self.username})), timeout=timeout).read()
            return []

        var_list = {}
        data = []
        for p in v:
            if isinstance(p, int):
                if p not in var_list:
                    data = flush_data(data)
                    val = urllib2.urlopen(urllib2.Request(self.address, urllib.urlencode({"op": "get_output", "username": self.username})), timeout=timeout).read()
                    val = val.split("=", 2)[1].split("&", 1)[0]
                    var_list[p] = val
                    continue
                else:
                    val = var_list[p]
                    t = "R"
            else:
                val = p
                t = "V"
            data.append([t, val])
        data = flush_data(data)

        # 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(self.address, urllib.urlencode({"op": "set_input", "data": json.dumps(data), "username": self.username}))).read()

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

        # Wait for kernel to signal that it finished
        urllib2.urlopen(urllib2.Request(self.address, urllib.urlencode({"op": "set_input", "element_type": "V", "value": '2', "username": self.username}))).read()
        v = urllib2.urlopen(urllib2.Request(self.address, urllib.urlencode({"op": "get_output", "username": self.username}))).read()
        v = v.split("=", 2)[2]
        if v == "DONE":
            return True
        else:
            return False

    def visit_definition(self, tree):
        for a in tree.get_children("ID"):
            name = a.get_tail()[0]
            self.object_symbols[name] = True
            return ConstructorsVisitor.visit_definition(self, tree)

    def visit_vardecl(self, tree):
        if len(tree.get_tail()) > 2:
            for a in tree.get_children("ID"):
                name = a.get_tail()[0]
                self.object_symbols.setdefault(name, False)
                return ConstructorsVisitor.visit_vardecl(self, tree)
        else:
            return ConstructorsVisitor.visit_vardecl(self, tree)

    def visit_funcdecl(self, tree):
        for a in tree.get_children("func_name"):
            for b in a.get_children("ID"):
                name = b.get_tail()[0]
                if tree.get_children("func_body") or tree.get_children("ASSIGN"):
                    self.object_symbols[name] = True
                else:
                    self.object_symbols.setdefault(name, False)
                return ConstructorsVisitor.visit_funcdecl(self, tree)
