using DeviceRepair.Models;
using DeviceRepair.Models.SFC;
using System;
using System.Collections.Generic;
using System.Linq;
using TsSFCDevice.Client.Biz.Impl;

namespace TsSFCDevice.Client.Biz.Base.Utils
{
    public class SysRuntimeInfo
    {
        public readonly string DEVICE_OEM = "BIZ_EQUIP_70";
        public readonly string DEVICE_KH = "BIZ_EQUIP_69";

        /// <summary>
        /// 当前机器名
        /// </summary>
        public string ComputerName { get; set; }

        #region 当前登陆用户信息
        private TsSFCUserInfo m_CurrentUser;
        public TsSFCUserInfo CurrentUser
        {
            get
            {
                return m_CurrentUser;
            }
            set
            {
                m_CurrentUser = value;
            }
        }

        private IList<TsSFCAuths> m_CurrentAuths;
        public IList<TsSFCAuths> CurrentAuths
        {
            get
            {
                return m_CurrentAuths;
            }
            set
            {
                m_CurrentAuths = value;
            }
        }
        #endregion

        #region 各种缓存

        private IList<DeviceInformationInfo> m_CurrentDeviceCaches;
        /// <summary>
        /// 设备信息缓存
        /// </summary>
        public IList<DeviceInformationInfo> CurrentDeviceCaches
        {
            get
            {
                if (m_CurrentDeviceCaches == null)
                {
                    List<string> auths = new List<string>();
                    if (AuthValidate(DEVICE_OEM))
                        auths.Add("OEM");
                    if (AuthValidate(DEVICE_KH))
                        auths.Add("KH");

                    DevRepository devDa = new DevRepository();
                    m_CurrentDeviceCaches = devDa.GetDatas(auths);
                }
                return m_CurrentDeviceCaches;
            }
            set
            {
                m_CurrentDeviceCaches = value;
            }
        }

        public int[] CurrentDeviceAutoIDCaches
        {
            get
            {
                return CurrentDeviceCaches.Select(x => x.AutoID).ToArray();
            }
        }

        private IList<TsSFCUserInfo> m_CurrentUsersCaches;
        /// <summary>
        /// 当前用户信息缓存
        /// </summary>
        public IList<TsSFCUserInfo> CurrentUsersCaches
        {
            get
            {
                if (m_CurrentUsersCaches == null)
                {
                    UserRepository userDa = new UserRepository();
                    m_CurrentUsersCaches = userDa.GetDatas();
                }
                return m_CurrentUsersCaches;
            }
            set
            {
                m_CurrentUsersCaches = value;
            }
        }

        private IList<DeviceRouteInfo> m_CurrentDevRootCaches;

        public IList<DeviceRouteInfo> CurrentDevRootCaches
        {
            get
            {
                if (m_CurrentDevRootCaches == null)
                {
                    List<string> auths = new List<string>();
                    if (AuthValidate(DEVICE_OEM))
                        auths.Add("OEM");
                    if (AuthValidate(DEVICE_KH))
                        auths.Add("KH");

                    m_CurrentDevRootCaches = DevRepository.Instance.Get_DEVICE_Route(auths);
                }
                return m_CurrentDevRootCaches;
            }
            set
            {
                m_CurrentDevRootCaches = value;
            }
        }

        #endregion


        #region Methods

        /// <summary>
        /// 校验用户权限
        /// </summary>
        /// <param name="vAuthCode"></param>
        /// <returns></returns>
        public bool AuthValidate(string vAuthCode)
        {
            return CurrentAuths?.FirstOrDefault(A => { return A.AuthCode == vAuthCode; }) != null;
        }
        public bool AuthStartWithValidate(string vAuthCode)
        {
            return CurrentAuths?.FirstOrDefault(A => { return A.AuthCode.StartsWith(vAuthCode, StringComparison.OrdinalIgnoreCase); }) != null;
        }
        #endregion
    }
}
