
import clr
import sys
import os
import System
from System import AppDomain
from System.Reflection import BindingFlags

def find_init():
    dll_dir = os.path.join(os.getcwd(), "libs")
    sys.path.append(dll_dir)
    clr.AddReference("rapiplus")
    
    assembly = None
    for a in AppDomain.CurrentDomain.GetAssemblies():
        if "rapiplus" in a.FullName.lower():
            assembly = a
            break
    if not assembly: return
    
    results = []
    
    # 1. REngineに関連するすべての静的メソッド
    results.append("--- Static methods in REngine ---")
    t_engine = assembly.GetType("com.omnesys.rapi.REngine")
    if t_engine:
        for m in t_engine.GetMethods(BindingFlags.Public | BindingFlags.Static):
            params = [f"{p.ParameterType.Name} {p.Name}" for p in m.GetParameters()]
            results.append(f"  {m.Name}({', '.join(params)}) returning {m.ReturnType.Name}")

    # 2. 型名に "Factory" または "Generator" または "Create" を含むものを探す
    results.append("\n--- Types that look like Factories ---")
    for t in assembly.GetTypes():
        if not t.IsPublic: continue
        if any(keyword in t.Name for keyword in ["Factory", "Generator", "Create", "Init"]):
            results.append(f"  {t.FullName} ({'Interface' if t.IsInterface else 'Class'})")
            for m in t.GetMethods(BindingFlags.Public | BindingFlags.Static):
                results.append(f"    Static Method: {m.Name} returns {m.ReturnType.Name}")

    # 3. AdmCallbacksのプロパティを再確認 (REngineParams以外にないか)
    results.append("\n--- Searching for AdmCallbacks properties across all types ---")
    for t in assembly.GetTypes():
        if not t.IsPublic: continue
        for p in t.GetProperties():
            if "AdmCallbacks" in p.PropertyType.Name:
                results.append(f"  Property: {t.FullName}.{p.Name} ({p.PropertyType.Name})")

    with open("init_patterns.txt", "w") as f:
        f.write("\n".join(results))
    print("Done. Results in init_patterns.txt")

if __name__ == "__main__":
    find_init()
