using System;
using System.Collections.Generic;
using System.Linq;
using FSI.Lib.Helpers;

namespace FSI.BT.Tools.Global.Utilities
{
    internal class Admin
    {
        public static bool CheckSuperAdminRight()
        {
            if (Vars.GlobalSettings.SuperAdmin == null)
            {
                return false;
            }

            System.Security.Principal.WindowsIdentity windowsIdentity = System.Security.Principal.WindowsIdentity.GetCurrent();

            if (string.Equals(Lib.DeEncryptString.DeEncrypt.DecryptString(Lib.DeEncryptString.DeEncrypt.DecryptString(Vars.GlobalSettings.SuperAdmin, AppDomain.CurrentDomain.FriendlyName), AppDomain.CurrentDomain.FriendlyName), windowsIdentity.ShortName(), StringComparison.OrdinalIgnoreCase))
                return true;

            return false;
        }

        public static bool CheckAdminRight()
        {
            if (Vars.GlobalSettings.Admins == null)
            {
                return false;
            }

            List<Settings.StringValue.IStringValue> users = Vars.GlobalSettings.Admins.ToList();

            System.Security.Principal.WindowsIdentity windowsIdentity = System.Security.Principal.WindowsIdentity.GetCurrent();

            foreach (var user in users)
            {
                if (string.Equals(Lib.DeEncryptString.DeEncrypt.DecryptString(user.Value, AppDomain.CurrentDomain.FriendlyName), windowsIdentity.ShortName(), StringComparison.OrdinalIgnoreCase))
                    return true;
            }

            return false;
        }

        public static bool CheckUserRight()
        {
            if (Vars.GlobalSettings.Users == null)
            {
                return false;
            }

            List<Settings.StringValue.IStringValue> users = Vars.GlobalSettings.Users.ToList();

            System.Security.Principal.WindowsIdentity windowsIdentity = System.Security.Principal.WindowsIdentity.GetCurrent();

            foreach (var user in users)
            {
                if (string.Equals(Lib.DeEncryptString.DeEncrypt.DecryptString(user.Value, AppDomain.CurrentDomain.FriendlyName), windowsIdentity.ShortName(), StringComparison.OrdinalIgnoreCase))
                    return true;
            }

            return false;
        }
    }
}
