using System;
using System.IO;
using System.Reflection;
using Aitex.Core.Account;
using Aitex.Core.RT.Log;
using Caliburn.Micro;
using MECF.Framework.Common.Account.Extends;
using MECF.Framework.Common.Utilities;
using MECF.Framework.UI.Client.CenterViews.Configs.Accounts;
using MECF.Framework.UI.Client.CenterViews.Configs.Roles;
using MECF.Framework.UI.Core.Accounts;

namespace MECF.Framework.UI.Client.ClientBase
{
    public class BaseApp
    {

        #region Variables

        public ModuleDataMonitor _dataMonitor;

        #endregion

        #region Constructors

        public BaseApp()
        {
            UserMode = UserMode.None;
            Initialized = false;

            Configure();
        }

        #endregion

        #region Properites

        public LoginClientInfo ClientInfo { get; private set; }

        public UserContext UserContext { get; private set; }

        public MenuManager MenuManager { get; private set; }

        public UserMode UserMode { get; set; }

        public bool Initialized { get; private set; }

        public static BaseApp Instance { get; set; }

        #endregion

        #region Methods


        public void LoadSystemInfo()
        {
            var myInfo = new LoginClientInfo();
            myInfo.HostName = Environment.MachineName;
            myInfo.UserName = Environment.UserName;
            myInfo.OSVersion = Environment.OSVersion.ToString();
            try
            {
                myInfo.ComputerSystem = SystemInfoHelper.GetComputerSystem();
                myInfo.CpuInfo = SystemInfoHelper.GetCpuInfo();
                myInfo.LogicalDisk = SystemInfoHelper.GetLogicalDiskInfo();
            }
            catch (Exception ex)
            {
                LOG.Error("Unable to get system information.", ex);
            }
            finally
            {
                ClientInfo = myInfo;
            }
        }

        public void Initialize(bool force = false)
        {
            if (Initialized && !force)
                return;
            
            MenuManager = new MenuManager();
            UserContext = new UserContext();

            var file = $"{System.AppDomain.CurrentDomain.BaseDirectory}MECF.Framework.UI.Client.dll";
            if (File.Exists(file))
            {
                var assembly = Assembly.LoadFile(file);
                AssemblySource.Instance.Add(assembly);
            }

            OnInitialize();

            //must be called after specific project initialized
            _dataMonitor = new ModuleDataMonitor();
            Initialized = true;
        }

        public virtual void Dispose()
        {

        }

        protected void Configure()
        {
            //config skin/language...
            OnConfiguration();

        }

        protected virtual void OnInitialize()
        {
        }

        protected virtual void OnConfiguration()
        {
        }

        public virtual void SwitchPage(string mainMenu, string subMenu, object parameter)
        {
        }

        #endregion
    }
}
