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

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

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

        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_cast_i2f(self):
        self.helper_primitives_1_params("cast_i2f", 2, 2.0)

    def test_cast_i2s(self):
        self.helper_primitives_1_params("cast_i2s", 2, "2")

    def test_cast_i2b(self):
        self.helper_primitives_1_params("cast_i2b", 2, True)

    def test_cast_f2i(self):
        self.helper_primitives_1_params("cast_f2i", 2.4, 2)

    def test_cast_f2s(self):
        self.helper_primitives_1_params("cast_f2s", 2.4, "2.4")

    def test_cast_f2b(self):
        self.helper_primitives_1_params("cast_f2b", 2.4, True)

    def test_cast_s2i(self):
        self.helper_primitives_1_params("cast_s2i", "2", 2)

    def test_cast_s2f(self):
        self.helper_primitives_1_params("cast_s2f", "2.4", 2.4)

    def test_cast_s2b(self):
        self.helper_primitives_1_params("cast_s2b", "2.4", True)

    def test_cast_b2i_true(self):
        self.helper_primitives_1_params("cast_b2i", True, 1)

    def test_cast_b2f_true(self):
        self.helper_primitives_1_params("cast_b2f", True, 1.0)

    def test_cast_b2s_true(self):
        self.helper_primitives_1_params("cast_b2s", True, "True")

    def test_cast_b2i_false(self):
        self.helper_primitives_1_params("cast_b2i", False, 0)

    def test_cast_b2f_false(self):
        self.helper_primitives_1_params("cast_b2f", False, 0.0)

    def test_cast_b2s_false(self):
        self.helper_primitives_1_params("cast_b2s", False, "False")

    def test_cast_e2s_node(self):
        n = self.mvs.execute("CN", [])[0]
        self.helper_primitives_1_params("cast_e2s", n, "{ID: %s, value: None}" % n, t=True)

    def test_cast_e2s_edge(self):
        a = self.mvs.execute("CN", [])[0]
        b = self.mvs.execute("CN", [])[0]
        n = self.mvs.execute("CE", [a, b])[0]
        self.helper_primitives_1_params("cast_e2s", n, "{ID: %s, value: None}" % n, t=True)

    def test_cast_e2s_value(self):
        n = self.mvs.execute("CNV", [10])[0]
        self.helper_primitives_1_params("cast_e2s", n, "{ID: %s, value: 10}" % n, t=True)

    def test_cast_v2s_int(self):
        n = self.mvs.execute("CN", [])[0]
        self.helper_primitives_1_params("cast_v2s", n, "None", t=True)

    def test_cast_v2s_int(self):
        self.helper_primitives_1_params("cast_v2s", 2, "2")

    def test_cast_v2s_neg_int(self):
        self.helper_primitives_1_params("cast_v2s", -2, "-2")

    def test_cast_v2s_float(self):
        self.helper_primitives_1_params("cast_v2s", 2.0, "2.0")

    def test_cast_v2s_neg_float(self):
        self.helper_primitives_1_params("cast_v2s", -2.0, "-2.0")

    def test_cast_v2s_string(self):
        self.helper_primitives_1_params("cast_v2s", "abc", "\"abc\"")

    def test_cast_v2s_string_num(self):
        self.helper_primitives_1_params("cast_v2s", "3", "\"3\"")

    def test_cast_v2s_bool(self):
        self.helper_primitives_1_params("cast_v2s", True, "True")

    def test_cast_v2s_action(self):
        self.helper_primitives_1_params("cast_v2s", {"value": "call"}, "call")

    def test_cast_id2s_node(self):
        n = self.mvs.execute("CN", [])[0]
        self.helper_primitives_1_params("cast_id2s", n, str(n), t=True)

    def test_cast_id2s_value(self):
        n = self.mvs.execute("CNV", ["abc"])[0]
        self.helper_primitives_1_params("cast_id2s", n, str(n), t=True)

    def test_cast_id2s_edge(self):
        a = self.mvs.execute("CN", [])[0]
        b = self.mvs.execute("CN", [])[0]
        n = self.mvs.execute("CE", [a, b])[0]
        self.helper_primitives_1_params("cast_id2s", n, str(n), t=True)

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

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

        self.mvs.execute("CD", [self.task_frame, "IP", self.inst])
        self.const_func = self.mvs.execute("CNV", [{"value":"constant"}])[0]
        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])

        ### 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")
        if not t:
            self.assertEquals(get_returnvalue(self.root, self.mvs), a)
        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.assertEquals(get_returnvalue(self.root, self.mvs), result)
        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")
