import unittest
from modelverse_kernel.main import ModelverseKernel
from test.utils import execute_until_finished, MvSWrapper, get_inst, get_phase, get_returnvalue, get_phase_ref, read_primitive_interfaces, get_returnvalue_ref

class TestDict(unittest.TestCase):
    def setUp(self):
        self.mvs = MvSWrapper()
        self.root = self.mvs.execute("RR", [])
        self.mvk = ModelverseKernel(self.root)

        self.task_root = self.mvs.execute("CN", [])
        self.task_frame = self.mvs.execute("CN", [])
        self.symbols = self.mvs.execute("CN", [])
        self.evalstack = self.mvs.execute("CN", [])
        self.returnvalue = self.mvs.execute("CN", [])
        self.phase = self.mvs.execute("CNV", ["init"])

        self.mvs.execute("CD", [self.root, "task_1", self.task_root])
        self.mvs.execute("CD", [self.task_root, "frame", self.task_frame])
        self.mvs.execute("CD", [self.task_frame, "symbols", self.symbols])
        self.mvs.execute("CD", [self.task_frame, "evalstack", self.evalstack])
        self.mvs.execute("CD", [self.task_frame, "phase", self.phase])
        self.mvs.execute("CD", [self.task_frame, "returnvalue", self.returnvalue])

        execute_until_finished(self.mvk, self.mvs, "load_primitives", [])
        self.primitives = read_primitive_interfaces(self.root, self.mvs)

    def test_delete(self):
        self.helper_primitives_2_params("dict_delete", {"a": 5}, "a", {})

    def test_in(self):
        self.helper_primitives_2_params("dict_in", {"a": 5}, "a", True)

    def test_in_empty(self):
        self.helper_primitives_2_params("dict_in", {}, "a", False)

    def test_in_similar(self):
        # This is inherited from Python: if the values are actually equal (e.g. 1.0 == 1 and 1 == True), they are also equal in reading from dictionaries
        # This is actually rather helpful, despite being counter-intuitive, since otherwise we might mix up with casts between unicode and string, for example
        self.helper_primitives_2_params("dict_in", {1.0: 2}, 1, True)

    def test_in_value(self):
        self.helper_primitives_2_params("dict_in", {1: 2}, 1, True)

    def test_in_mixed(self):
        self.helper_primitives_2_params("dict_in", {2: "b", "a": 1, False: False}, 1, False)

    def test_in_mixed_true(self):
        self.helper_primitives_2_params("dict_in", {2: "b", "a": 1, False: False}, False, True)

    def test_delete_multi(self):
        self.helper_primitives_2_params("dict_delete", {"a": 5, "b": 6}, "a", {"b": 6})

    def test_read(self):
        self.helper_primitives_2_params("dict_read", {"a": 5, "b": 6}, "a", 5)

    def construct(self, source, dct):
        for i, v in dct.items():
            t = self.mvs.execute("CNV", [v])
            self.mvs.execute("CD", [source, i, t])

    def parse(self, source, expected):
        if isinstance(expected, dict):
            self.assertEquals(len(self.mvs.execute("RO", [source])), len(expected))
            for i, v in expected.items():
                self.assertEquals(self.mvs.execute("RV", [self.mvs.execute("RD", [source, i])]), v)
        else:
            self.assertEquals(get_returnvalue(self.root, self.mvs), expected)

    def helper_primitives_2_params(self, operation, a, b, result):
        self.actual_arg_a = self.mvs.execute("CN", [])
        self.actual_arg_b = self.mvs.execute("CN", [])

        self.inst = self.mvs.execute("CNV", [{"value":"call"}])
        self.const_a = self.mvs.execute("CNV", [{"value":"constant"}])
        self.const_b = self.mvs.execute("CNV", [{"value":"constant"}])
        self.const_c = self.mvs.execute("CNV", [{"value":"constant"}])
        self.const_first = self.mvs.execute("CNV", [{"value":"constant"}])
        self.const_second = self.mvs.execute("CNV", [{"value":"constant"}])
        self.value_a = self.mvs.execute("CNV", ["a"])
        self.value_b = self.mvs.execute("CNV", ["b"])
        self.value_c = self.mvs.execute("CNV", ["c"])
        self.value_first = self.mvs.execute("CN", [])
        self.value_second = self.mvs.execute("CNV", [b])

        self.construct(self.value_first, a)

        self.mvs.execute("CD", [self.task_frame, "IP", self.inst])
        self.const_func = self.mvs.execute("CNV", [{"value":"constant"}])
        self.mvs.execute("CD", [self.inst, "func", self.const_func])
        self.mvs.execute("CD", [self.const_func, "node", self.primitives[operation]])
        self.mvs.execute("CD", [self.inst, "next", self.const_c])
        self.mvs.execute("CD", [self.inst, "params", self.actual_arg_a])
        self.mvs.execute("CD", [self.actual_arg_a, "next_param", self.actual_arg_b])
        self.mvs.execute("CD", [self.inst, "last_param", self.actual_arg_b])
        self.mvs.execute("CD", [self.actual_arg_a, "value", self.const_first])
        self.mvs.execute("CD", [self.actual_arg_b, "value", self.const_second])
        self.mvs.execute("CD", [self.actual_arg_a, "name", self.value_a])
        self.mvs.execute("CD", [self.actual_arg_b, "name", self.value_b])
        self.mvs.execute("CD", [self.const_a, "node", self.value_a])
        self.mvs.execute("CD", [self.const_b, "node", self.value_b])
        self.mvs.execute("CD", [self.const_c, "node", self.value_c])
        self.mvs.execute("CD", [self.const_first, "node", self.value_first])
        self.mvs.execute("CD", [self.const_second, "node", self.value_second])

        ### Execute rules
        self.assertEquals(get_inst(self.root, self.mvs), "call")
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "call")
        self.assertEquals(get_phase_ref(self.root, self.mvs), self.actual_arg_a)
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        self.assertEquals(get_returnvalue_ref(self.root, self.mvs), self.value_first)
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "call")
        self.assertEquals(get_phase_ref(self.root, self.mvs), self.actual_arg_b)
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        self.assertEquals(get_returnvalue(self.root, self.mvs), b)
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "call")
        self.assertEquals(get_phase(self.root, self.mvs), "call")
        execute_until_finished(self.mvk, self.mvs)

        # Body points to empty instruction, as it is a primitive
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        # This will have triggered the primitive, so now we are back where we left off
        self.assertEquals(get_inst(self.root, self.mvs), "call")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")

        self.parse(self.value_first, result)

    def helper_primitives_1_params(self, operation, a, result):
        self.actual_arg_a = self.mvs.execute("CN", [])

        self.inst = self.mvs.execute("CNV", [{"value":"call"}])
        self.const_a = self.mvs.execute("CNV", [{"value":"constant"}])
        self.const_b = self.mvs.execute("CNV", [{"value":"constant"}])
        self.const_c = self.mvs.execute("CNV", [{"value":"constant"}])
        self.const_first = self.mvs.execute("CNV", [{"value":"constant"}])
        self.value_a = self.mvs.execute("CNV", ["a"])
        self.value_b = self.mvs.execute("CNV", ["b"])
        self.value_c = self.mvs.execute("CNV", ["c"])
        self.value_first = self.mvs.execute("CN", [])

        self.mvs.execute("CD", [self.task_frame, "IP", self.inst])
        self.const_func = self.mvs.execute("CNV", [{"value":"constant"}])
        self.mvs.execute("CD", [self.inst, "func", self.const_func])
        self.mvs.execute("CD", [self.const_func, "node", self.primitives[operation]])
        self.mvs.execute("CD", [self.inst, "next", self.const_c])
        self.mvs.execute("CD", [self.inst, "params", self.actual_arg_a])
        self.mvs.execute("CD", [self.inst, "last_param", self.actual_arg_a])
        self.mvs.execute("CD", [self.actual_arg_a, "value", self.const_first])
        self.mvs.execute("CD", [self.actual_arg_a, "name", self.value_a])
        self.mvs.execute("CD", [self.const_a, "node", self.value_a])
        self.mvs.execute("CD", [self.const_b, "node", self.value_b])
        self.mvs.execute("CD", [self.const_c, "node", self.value_c])
        self.mvs.execute("CD", [self.const_first, "node", self.value_first])

        self.construct(self.value_first, a)

        ### Execute rules
        self.assertEquals(get_inst(self.root, self.mvs), "call")
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "call")
        self.assertEquals(get_phase_ref(self.root, self.mvs), self.actual_arg_a)
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        self.assertEquals(get_returnvalue_ref(self.root, self.mvs), self.value_first)
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "call")
        self.assertEquals(get_phase(self.root, self.mvs), "call")
        execute_until_finished(self.mvk, self.mvs)

        # Body points to empty instruction, as it is a primitive
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        # This will have triggered the primitive, so now we are back where we left off
        # Just check whether or not the correct value is in the returnvalue to be used
        self.assertEquals(get_inst(self.root, self.mvs), "call")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        execute_until_finished(self.mvk, self.mvs)

        self.parse(self.value_first, result)

    def helper_primitives_3_params(self, operation, a, b, c, result):
        self.actual_arg_a = self.mvs.execute("CN", [])
        self.actual_arg_b = self.mvs.execute("CN", [])
        self.actual_arg_c = self.mvs.execute("CN", [])

        self.inst = self.mvs.execute("CNV", [{"value":"call"}])
        self.const_a = self.mvs.execute("CNV", [{"value":"constant"}])
        self.const_b = self.mvs.execute("CNV", [{"value":"constant"}])
        self.const_c = self.mvs.execute("CNV", [{"value":"constant"}])
        self.const_first = self.mvs.execute("CNV", [{"value":"constant"}])
        self.const_second = self.mvs.execute("CNV", [{"value":"constant"}])
        self.const_third = self.mvs.execute("CNV", [{"value":"constant"}])
        self.value_a = self.mvs.execute("CNV", ["a"])
        self.value_b = self.mvs.execute("CNV", ["b"])
        self.value_c = self.mvs.execute("CNV", ["c"])
        self.value_first = self.mvs.execute("CN", [])
        self.value_second = self.mvs.execute("CNV", [b])
        self.value_third = self.mvs.execute("CNV", [c])

        self.mvs.execute("CD", [self.task_frame, "IP", self.inst])
        self.const_func = self.mvs.execute("CNV", [{"value":"constant"}])
        self.mvs.execute("CD", [self.inst, "func", self.const_func])
        self.mvs.execute("CD", [self.const_func, "node", self.primitives[operation]])
        self.mvs.execute("CD", [self.inst, "next", self.const_c])
        self.mvs.execute("CD", [self.inst, "params", self.actual_arg_a])
        self.mvs.execute("CD", [self.actual_arg_a, "next_param", self.actual_arg_b])
        self.mvs.execute("CD", [self.actual_arg_b, "next_param", self.actual_arg_c])
        self.mvs.execute("CD", [self.inst, "last_param", self.actual_arg_c])
        self.mvs.execute("CD", [self.actual_arg_a, "value", self.const_first])
        self.mvs.execute("CD", [self.actual_arg_b, "value", self.const_second])
        self.mvs.execute("CD", [self.actual_arg_c, "value", self.const_third])
        self.mvs.execute("CD", [self.actual_arg_a, "name", self.value_a])
        self.mvs.execute("CD", [self.actual_arg_b, "name", self.value_b])
        self.mvs.execute("CD", [self.actual_arg_c, "name", self.value_c])
        self.mvs.execute("CD", [self.const_a, "node", self.value_a])
        self.mvs.execute("CD", [self.const_b, "node", self.value_b])
        self.mvs.execute("CD", [self.const_c, "node", self.value_c])
        self.mvs.execute("CD", [self.const_first, "node", self.value_first])
        self.mvs.execute("CD", [self.const_second, "node", self.value_second])
        self.mvs.execute("CD", [self.const_third, "node", self.value_third])

        self.construct(self.value_first, a)

        ### Execute rules
        self.assertEquals(get_inst(self.root, self.mvs), "call")
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "call")
        self.assertEquals(get_phase_ref(self.root, self.mvs), self.actual_arg_a)
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        self.assertEquals(get_returnvalue_ref(self.root, self.mvs), self.value_first)
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "call")
        self.assertEquals(get_phase_ref(self.root, self.mvs), self.actual_arg_b)
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        self.assertEquals(get_returnvalue(self.root, self.mvs), b)
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "call")
        self.assertEquals(get_phase_ref(self.root, self.mvs), self.actual_arg_c)
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "constant")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")
        self.assertEquals(get_returnvalue(self.root, self.mvs), c)
        execute_until_finished(self.mvk, self.mvs)

        self.assertEquals(get_inst(self.root, self.mvs), "call")
        self.assertEquals(get_phase(self.root, self.mvs), "call")
        execute_until_finished(self.mvk, self.mvs)

        # Body points to empty instruction, as it is a primitive
        self.assertEquals(get_phase(self.root, self.mvs), "init")
        execute_until_finished(self.mvk, self.mvs)

        # This will have triggered the primitive, so now we are back where we left off
        # Just check whether or not the correct value is in the returnvalue to be used
        self.assertEquals(get_inst(self.root, self.mvs), "call")
        self.assertEquals(get_phase(self.root, self.mvs), "finish")

        self.parse(self.value_first, result)
