using DeviceRepair.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using TsSFCDeivceClient.Model;

namespace TsSFCDeivceClient
{
    public static class Runtime
    {
        public static string inParams;

        public static UserInfo _CurrentUser;
        /// <summary>
        /// TsSFC用户信息
        /// </summary>
        public static UserInfo CurrentUser
        {
            get { return _CurrentUser; }
            set { _UserAuths = null; _CurrentUser = value; }
        }


        private static List<string> _UserAuths;
        /// <summary>
        /// TsSFC用户权限
        /// </summary>
        public static List<string> UserAuths
        {
            get
            {
                if (_UserAuths == null)
                {
                    if (CurrentUser != null || CurrentUser.GUID != Guid.Empty)
                    {
                        APIResponseData apiResponseData = Biz.CommonApi.Instance.GetTsSFCUserAuths(CurrentUser.GUID);
                        if (!apiResponseData.IsSuccess)
                        {
                            throw new Exception(apiResponseData.Message);
                        }
                        _UserAuths = apiResponseData.ToDeserializeObject<List<string>>();
                    }
                    else
                    {
                        _UserAuths = new List<string>();
                    }
                }
                return _UserAuths;
            }
        }

        /// <summary>
        /// 是否包含指定权限
        /// </summary>
        /// <param name="AuthCode"></param>
        /// <returns></returns>
        public static bool UserHasAuth(string AuthCode)
        {
            return UserAuths.Any(x => x.Contains(AuthCode));
        }
    }
}
