import stdio
import t, c
import namespace_defs
from namespace_defs import Widget, Gadget, PlainStruct


# ============================================================
# 命名空间隔离测试
#
# 验证跨模块类访问必须通过 import：
# - Test 1: from X import Class → 裸名 Class() 构造
# - Test 2: import X → 模块限定 X.Class() 构造
# - Test 3: 类型注解使用导入的类
# - Test 4: 继承类的虚分派（跨模块继承）
# ============================================================


def namespace_test() -> int:
    stdio.printf("nsisol: === Test Start ===\n")

    # ============================================================
    # Test 1: from X import Class → 裸名 Class() 构造
    # ============================================================
    stdio.printf("nsisol: === Test 1: from-import bare constructor ===\n")

    w: Widget = Widget(42)
    wid: int = w.GetId()
    stdio.printf("nsisol: w.GetId()=%d (expected 42)\n", wid)
    if wid != 42:
        stdio.printf("[FAIL] w.GetId()=%d expected 42\n", wid)
        return 1

    wrender: int = w.Render()
    stdio.printf("nsisol: w.Render()=%d (expected 100)\n", wrender)
    if wrender != 100:
        stdio.printf("[FAIL] w.Render()=%d expected 100\n", wrender)
        return 1

    g: Gadget = Gadget(7, 9)
    grender: int = g.Render()
    stdio.printf("nsisol: g.Render()=%d (expected 200)\n", grender)
    if grender != 200:
        stdio.printf("[FAIL] g.Render()=%d expected 200\n", grender)
        return 1

    gextra: int = g.GetExtra()
    stdio.printf("nsisol: g.GetExtra()=%d (expected 9)\n", gextra)
    if gextra != 9:
        stdio.printf("[FAIL] g.GetExtra()=%d expected 9\n", gextra)
        return 1

    # ============================================================
    # Test 2: import X → 模块限定 X.Class() 构造
    # ============================================================
    stdio.printf("nsisol: === Test 2: module-qualified constructor ===\n")

    ps: PlainStruct = PlainStruct(10, 20)
    psum: int = ps.Sum()
    stdio.printf("nsisol: ps.Sum()=%d (expected 30)\n", psum)
    if psum != 30:
        stdio.printf("[FAIL] ps.Sum()=%d expected 30\n", psum)
        return 1

    ps2: namespace_defs.PlainStruct = namespace_defs.PlainStruct(5, 6)
    psum2: int = ps2.Sum()
    stdio.printf("nsisol: ps2.Sum()=%d (expected 11)\n", psum2)
    if psum2 != 11:
        stdio.printf("[FAIL] ps2.Sum()=%d expected 11\n", psum2)
        return 1

    # ============================================================
    # Test 3: 类型注解使用导入的类
    # ============================================================
    stdio.printf("nsisol: === Test 3: type annotation with imported class ===\n")

    annot_w: Widget = Widget(99)
    annot_id: int = annot_w.GetId()
    stdio.printf("nsisol: annot_w.GetId()=%d (expected 99)\n", annot_id)
    if annot_id != 99:
        stdio.printf("[FAIL] annot_w.GetId()=%d expected 99\n", annot_id)
        return 1

    # ============================================================
    # Test 4: 跨模块继承的虚分派
    # ============================================================
    stdio.printf("nsisol: === Test 4: cross-module inheritance vtable ===\n")

    # Gadget 继承 Widget，覆盖 Render
    # Widget.Render()=100, Gadget.Render()=200
    base: Widget = Widget(1)
    derived: Gadget = Gadget(2, 3)
    base_r: int = base.Render()
    deriv_r: int = derived.Render()
    stdio.printf("nsisol: base.Render()=%d (expected 100)\n", base_r)
    if base_r != 100:
        stdio.printf("[FAIL] base.Render()=%d expected 100\n", base_r)
        return 1
    stdio.printf("nsisol: derived.Render()=%d (expected 200)\n", deriv_r)
    if deriv_r != 200:
        stdio.printf("[FAIL] derived.Render()=%d expected 200\n", deriv_r)
        return 1

    # 继承的方法：Gadget 继承 Widget.GetId
    gname: int = derived.GetId()
    stdio.printf("nsisol: derived.GetId()=%d (expected 2)\n", gname)
    if gname != 2:
        stdio.printf("[FAIL] derived.GetId()=%d expected 2\n", gname)
        return 1

    stdio.printf("nsisol: === All Tests Passed ===\n")
    return 0
