using System.Reflection;
using ZB.MOM.WW.OtOpcUa.AdminUI.Uns;
/// Per-circuit synthetic display state; no external service or database is configured.
public sealed class FixtureTreeState
{
public bool Fail, Populated, UnsFailAfterWrite;
public int Reads, Unexpected, CallerMutationRequests, UnsMutationRequests;
public Dictionary> CallerChildren=[];
public IReadOnlyList? UnsCallerRoots;
public void RenameUns(string id,string name)
{
UnsNode Clone(UnsNode n)
{
var copy=new UnsNode {Kind=n.Kind,Key=n.Key,EntityId=n.EntityId,ClusterId=n.ClusterId,DisplayName=n.EntityId==id?name:n.DisplayName,Expanded=n.Expanded,Loaded=n.Loaded,HasLazyChildren=n.HasLazyChildren,ChildCount=n.ChildCount};
copy.Children.AddRange(n.Children.Select(Clone));return copy;
}
if(UnsCallerRoots is not null)UnsCallerRoots=UnsCallerRoots.Select(Clone).ToArray();
}
}
public class FixtureTreeProxy:DispatchProxy
{
private FixtureTreeState _state=null!;
public static T Create(FixtureTreeState state) where T:class
{var p=Create();((FixtureTreeProxy)(object)p)._state=state;return p;}
protected override object? Invoke(MethodInfo? m,object?[]? a)
{
if(m?.Name=="LoadRootsAsync")
{_state.Reads++;return _state.Fail?Task.FromException>(new IOException("Synthetic local root read failure ")):Task.FromResult>([]);}
if(m?.Name=="LoadStructureAsync")
{
_state.Reads++;
return _state.Fail?Task.FromException>(new IOException("Synthetic local structure read failure "))
:Task.FromResult>(_state.UnsCallerRoots??(_state.Populated?[new UnsNode{Key="eq:local",Kind=UnsNodeKind.Equipment,EntityId="local",DisplayName="Synthetic press",HasLazyChildren=true}]:[]));
}
if(m?.Name=="LoadAreaAsync" && a?[0] is string area && area.StartsWith("caller-uns-",StringComparison.Ordinal))
return Task.FromResult(new(area,"local-area",null,"caller-uns-cluster",[1]));
if(m?.Name=="LoadLineAsync" && a?[0] is string line && line.StartsWith("caller-uns-",StringComparison.Ordinal))
return Task.FromResult(new(line,"caller-uns-area","local-line",null,[2]));
if((m?.Name=="UpdateAreaAsync"||m?.Name=="UpdateLineAsync") && a?[0] is string update && update.StartsWith("caller-uns-",StringComparison.Ordinal))
{
_state.UnsMutationRequests++;_state.RenameUns(update,(string)a[1]!);
if(_state.UnsFailAfterWrite)_state.Fail=true;
return Task.FromResult(new UnsMutationResult(true,null));
}
if(m?.Name=="DeleteClusterAsync" && a?[0] is string cluster && cluster.StartsWith("caller-uns-",StringComparison.Ordinal))
{_state.UnsMutationRequests++;return Task.FromResult(new UnsMutationResult(false,"Synthetic current cluster blocker ; no real deletion was attempted."));}
if(m?.Name=="LoadEquipmentPickContextAsync")return Task.FromResult(new EquipmentPickContext([],[]));
if(m?.Name=="LoadChildrenAsync" && a?[0] is RawNode node && _state.CallerChildren.TryGetValue(node.Key,out var children))return Task.FromResult(children);
if(m?.Name=="DeleteFolderAsync" && a?[0] is string deleteId && deleteId.StartsWith("caller-",StringComparison.Ordinal))
{_state.CallerMutationRequests++;return Task.FromResult(new UnsMutationResult(false,"Synthetic local blocker : this folder has references. No real deletion was attempted."));}
if(m?.Name=="RenameFolderAsync" && a?[0] is string renameId && renameId.StartsWith("caller-",StringComparison.Ordinal))
{_state.CallerMutationRequests++;return Task.FromResult(RawRenameResult.Success(["Synthetic local rename advisory ; no database configuration changed."]));}
_state.Unexpected++;throw new InvalidOperationException("Tree fixture refuses non-root operation: "+m?.Name);
}
}