namespace OpenWarehouse.warehouse.api.Common.CustimIdentifyManager;

public static class ServicePrivilageClass
{
    private static readonly string PrivilagePrefix = "WarehouseApi";
    private static readonly Dictionary<ServicePrivilage, string> PrivilageNameMap = new()
    {
        { ServicePrivilage.Admin, "Admin" },
        { ServicePrivilage.ServiceAdmin, $"{PrivilagePrefix}-Admin" },
        { ServicePrivilage.ManageWarehouse, $"{PrivilagePrefix}-Warehouse-Manage" },
        { ServicePrivilage.EditWarehouse, $"{PrivilagePrefix}-Warehouse-Edit" },
        { ServicePrivilage.AddWarehouse, $"{PrivilagePrefix}-Warehouse-Add" },
        { ServicePrivilage.DeleteWarehouse, $"{PrivilagePrefix}-Warehouse-Delete" },
        { ServicePrivilage.ManageItems, $"{PrivilagePrefix}-Items-Manage" },
        { ServicePrivilage.EditItems, $"{PrivilagePrefix}-Items-Edit" },
        { ServicePrivilage.AddItems, $"{PrivilagePrefix}-Items-Add" },
        { ServicePrivilage.DeleteItems, $"{PrivilagePrefix}-Items-Delete" },
        { ServicePrivilage.ManageInvetory, $"{PrivilagePrefix}-Inventory-mange"},
        { ServicePrivilage.EditInventory, $"{PrivilagePrefix}-Inventory-Edit"},
        { ServicePrivilage.AddItemToInventory, $"{PrivilagePrefix}-Inventory-Add-Item"},
        { ServicePrivilage.SeeInventory, $"{PrivilagePrefix}-Inventory-See"},
        { ServicePrivilage.ServiceUser, $"{PrivilagePrefix}-User"}
    };

    private static readonly Dictionary<ServicePrivilage, List<ServicePrivilage>> PrivilageMap = new()
    {
        { ServicePrivilage.Admin, [ServicePrivilage.Admin] },
        { ServicePrivilage.ServiceAdmin, [ServicePrivilage.Admin, ServicePrivilage.ServiceAdmin] },
        {
            ServicePrivilage.ManageWarehouse,
            [ServicePrivilage.Admin, ServicePrivilage.ServiceAdmin, ServicePrivilage.ManageWarehouse]
        },
        {
            ServicePrivilage.EditWarehouse,
            [
                ServicePrivilage.Admin, ServicePrivilage.ServiceAdmin, ServicePrivilage.ManageWarehouse,
                ServicePrivilage.EditWarehouse
            ]
        },
        {
            ServicePrivilage.AddWarehouse,
            [
                ServicePrivilage.Admin, ServicePrivilage.ServiceAdmin, ServicePrivilage.ManageWarehouse,
                ServicePrivilage.AddWarehouse
            ]
        },
        {
            ServicePrivilage.DeleteWarehouse,
            [
                ServicePrivilage.Admin, ServicePrivilage.ServiceAdmin, ServicePrivilage.ManageWarehouse,
                ServicePrivilage.DeleteWarehouse
            ]
        },
        {
            ServicePrivilage.ManageItems,
            [ServicePrivilage.Admin, ServicePrivilage.ServiceAdmin, ServicePrivilage.ManageItems]
        },
        {
            ServicePrivilage.EditItems,
            [
                ServicePrivilage.Admin, ServicePrivilage.ServiceAdmin, ServicePrivilage.ManageItems,
                ServicePrivilage.EditItems
            ]
        },
        {
            ServicePrivilage.AddItems,
            [
                ServicePrivilage.Admin, ServicePrivilage.ServiceAdmin, ServicePrivilage.ManageItems,
                ServicePrivilage.AddItems
            ]
        },
        {
            ServicePrivilage.DeleteItems,
            [
                ServicePrivilage.Admin, ServicePrivilage.ServiceAdmin, ServicePrivilage.ManageItems,
                ServicePrivilage.DeleteItems
            ]
        },
        {
            ServicePrivilage.ManageInvetory,
            [ServicePrivilage.Admin, ServicePrivilage.ServiceAdmin, ServicePrivilage.ManageInvetory]
        },
        {
            ServicePrivilage.EditInventory,
            [
                ServicePrivilage.Admin, ServicePrivilage.ServiceAdmin, ServicePrivilage.ManageInvetory,
                ServicePrivilage.EditInventory
            ]
        },
        {
            ServicePrivilage.AddItemToInventory,
            [
                ServicePrivilage.Admin, ServicePrivilage.ServiceAdmin, ServicePrivilage.ManageInvetory,
                ServicePrivilage.AddItemToInventory
            ]
        },
        {
            ServicePrivilage.SeeInventory,
            [
                ServicePrivilage.Admin, ServicePrivilage.ServiceAdmin, ServicePrivilage.ManageInvetory,
                ServicePrivilage.SeeInventory
            ]
        },
        { ServicePrivilage.ServiceUser, GetAllPrivilageList() }
    };

    public static List<ServicePrivilage> GetAllPrivilageList()
    {
        var list = PrivilageNameMap.Select(pair => pair.Key).ToList();
        return list;
    }
    
    public static List<ServicePrivilage> GetServicePrivilageList()
    {
        var list = PrivilageNameMap.Where(pair => pair.Value.Contains(PrivilagePrefix))
            .Select(pair => pair.Key).ToList();
        return list;
    }
    
    public static List<string> GetServicePrivilageNameList()
    {
        var list = PrivilageNameMap.Where(pair => pair.Value.Contains(PrivilagePrefix))
            .Select(pair => pair.Value).ToList();
        return list;
    }

    public static string GetName(this ServicePrivilage servicePrivilage)
    {
        var result = PrivilageNameMap.TryGetValue(servicePrivilage, out var name);
        return (result ? name : $"Uknow-{servicePrivilage}") ?? $"Role {servicePrivilage} Uknow";
    }
}