using System.Collections.Generic;
using System.Linq;
using detect.gui.Classes;
using ReactiveUI;

namespace detect.gui.Commons;

public static class RouterHelper
{
    public static void Init(RoutingState _router)
    {
        Router = _router;
        ViewModelList = new List<ViewModelItem>
        {
            new(Router) { ViewModelTypeName = "HomeViewModel", Text = "首页" },
            new(Router) { ViewModelTypeName = "DetectTaskViewModel", Text = "任务管理" },
            new(Router) { ViewModelTypeName = "DeviceViewModel", Text = "设备管理" },
            new(Router) { ViewModelTypeName = "LogViewModel", Text = "日志查看" },
            new(Router) { ViewModelTypeName = "UserViewModel", Text = "用户设置" }
        };
    }

    public static List<ViewModelItem> ViewModelList = null!;
    private static RoutingState Router = null!;
    
    public static IRoutableViewModel? GetViewModel(string viewModelTypeName)
    {
        var t = viewModelTypeName.GetTypeByName();
        return (IRoutableViewModel?)t?.Assembly.CreateInstance(t.FullName!);
    }
    public static ViewModelItem? FindViewModelItem(List<ViewModelItem>? viewModelList,string viewModelTypeName, ViewModelItem? result)
    {
        foreach (var item in viewModelList!)
        {
            if (item.ViewModelTypeName == viewModelTypeName)
            {
                result = item;
                break;
            }
            if (result == null && item.Children.Any())
            {
                result = FindViewModelItem(item.Children, viewModelTypeName, result);
            }
        }
            
        return result;
    }
}