using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;
using Philisense.Congress.Common.View;
using Microsoft.Practices.Prism.Modularity;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Microsoft.Practices.Unity; 
using Philisense.Congress.EFDatabase;
using Philisense.Congress.Common.Enum;
using System.Windows.Threading;
using Newtonsoft.Json;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;  

namespace Philisense.Congress.ViewModel
{
    public class MainWindowViewModel : BaseViewModel
    {
        #region 属性
        public IUnityContainer Container { get; private set; }

        private IRegionManager regionManager;
        Philisense.Congress.Collection.BLL.Sys.UserInfoManager userInfoManager = new Collection.BLL.Sys.UserInfoManager();

        private T_UserInfo userInfo;
        /// <summary>
        /// 登录用户
        /// </summary>
        public T_UserInfo UserInfo
        {
            get { return userInfo; }
            set { userInfo = value; }
        }

        List<T_RoleBlock> listRoleBlock;
        /// <summary>
        /// 模块权限集合
        /// </summary>
        public List<T_RoleBlock> ListRoleBlock
        {
            get { return listRoleBlock; }
            set { listRoleBlock = value; NotifyPropertyChanged("ListRoleBlock"); }
        }

        private Visibility loadingStatues;
        /// <summary>
        /// loading动态
        /// </summary>
        public Visibility LoadingStatues
        {
            get { return loadingStatues; }
            set { loadingStatues = value; NotifyPropertyChanged("LoadingStatues"); }
        }
        private Philisense.Congress.Model.RoleBlock roleBlock;

        public Philisense.Congress.Model.RoleBlock RoleBlock
        {
            get { return roleBlock; }
            set { roleBlock = value; NotifyPropertyChanged("RoleBlock"); }
        }

        private string softVersion;

        public string SoftVersion
        {
            get { return softVersion; }
            set { softVersion = value; NotifyPropertyChanged("SoftVersion"); }
        }

        private string localIP;

        public string LocalIP
        {
            get { return localIP; }
            set { localIP = value; NotifyPropertyChanged("LocalIP"); }
        }
        private DispatcherTimer ShowTimer;
        private string timerStr;

        public string TimerStr
        {
            get { return timerStr; }
            set { timerStr = value; NotifyPropertyChanged("TimerStr"); }
        }
        #endregion

        #region 构造

        public MainWindowViewModel(IUnityContainer container, IRegionManager regionManager)
        {
            //Philisense.Congress.CollectionUI.ViewModel.UCControl.Staff.StaffLibViewModel model = new CollectionUI.ViewModel.UCControl.Staff.StaffLibViewModel();
            //return;
            Philisense.Congress.CollectionUI.CommonCenter.ControlCenter.Instance.OnLoadingWaitEvent += Instance_OnLoadingWaitEvent;
            Philisense.Congress.CollectionUI.CommonCenter.ControlCenter.Instance.LoadingWait(Visibility.Collapsed, "");
            Container = container;
            this.regionManager = regionManager;
            SoftVersion = System.Configuration.ConfigurationManager.AppSettings["Version"];
            //加载权限
            LoadPowerBlock();

            LocalIP = Common.ToolsHelper.GetLocalIP();
           // new Philisense.Congress.ControlUI.ViewModel.UCControl.Congress.CongressChoseViewModel().OnLoadEvent += MainWindowViewModel_OnLoadEvent;
            ShowTimer = new System.Windows.Threading.DispatcherTimer();
            ShowTimer.Tick += new EventHandler(ShowCurTimer);//起个Timer一直获取当前时间
            ShowTimer.Interval = new TimeSpan(0, 0, 0, 1, 0);
            ShowTimer.Start();
        }

        void MainWindowViewModel_OnLoadEvent(System.Windows.Visibility vis, string msg)
        {
            loadingStatues = vis;
        }
        /// <summary>
        /// loading 动画
        /// </summary>
        /// <param name="vis">显示，隐藏</param>
        /// <param name="msg">加载消息内容</param>
        void Instance_OnLoadingWaitEvent(Visibility vis, string msg)
        {
            loadingStatues = vis;
            //消息内容待扩展
        }
        #endregion


        #region 事件Event
        //主窗体加载
        public ICommand LoadCommand
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    Philisense.Congress.CollectionUI.CommonCenter.ControlCenter.Instance.LoadingWait(Visibility.Collapsed, "");
                    Philisense.Congress.Collection.BLL.Staff.StaffInfoManager staffInfo = new Collection.BLL.Staff.StaffInfoManager();
                    staffInfo.InfoList();
                });
            }
        }
        /// <summary>
        /// 按钮菜单点击选择MeetingCBRegion/TopicDetailView
        /// </summary>
        public ICommand ButtonCommand
        {
            get
            {
                return new DelegateCommand<string>((btn_type) =>
                {
                    if ("MeetingCBRegion_TopicDetailView"== btn_type)
                    {
                        System.Console.Write(" 1---------" + DateTime.Now.ToString("mm-ss-fff") + "\r\n\t");
                    }
                    string[] Parameter = btn_type.Split('_');
                    regionManager.RequestNavigate(Parameter[0], Parameter[1]);
                    if ("MeetingCBRegion_TopicDetailView" == btn_type)
                    {
                        System.Console.Write(" 9---------" + DateTime.Now.ToString("mm-ss-fff") + "\r\n\t");
                    }
                });
            }
        }

        /// <summary>
        /// 初始化权限模块显示
        /// </summary>
        public void LoadPowerBlock()
        {
            System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();
            xmlDocument.Load("Xmls\\LoginConfig.xml");
            System.Xml.XmlNodeList nodeList = xmlDocument.SelectSingleNode("data").ChildNodes;//获取Employees节点的所有子节点
            string userName = string.Empty;
            string userPwd = string.Empty;
            foreach (System.Xml.XmlNode xn in nodeList)//遍历
            {
                System.Xml.XmlElement xe = (System.Xml.XmlElement)xn;
                if (xe.GetAttribute("Name") == "登录名")
                {
                    userName = xe.InnerText;
                }
                if (xe.GetAttribute("Name") == "登录密码")
                {
                    userPwd = xe.InnerText;
                }
            }
            string OutMsg = "";
            UserInfo = userInfoManager.GetUserInfoByName_Pwd(userName, Common.ToolsHelper.MD5Encrypt(userPwd),out OutMsg);
            ListRoleBlock = UserInfo.T_Role.T_RoleBlock.ToList();

           

            Philisense.Congress.Model.RoleBlock _roleBlock = new Model.RoleBlock();
            for (int i = 0; i < ListRoleBlock.Count; i++)
            {
                switch (ListRoleBlock[i].T_Block.Bl_Code)
                {
                    case (int)RoleBlockEnum.BasicPreparation: _roleBlock.BasicPreparation = "Visible"; break;
                    case (int)RoleBlockEnum.MeetingPreparation: _roleBlock.MeetingPreparation = "Visible"; break;
                    case (int)RoleBlockEnum.PostManagement: _roleBlock.PostManagement = "Visible"; break;
                    case (int)RoleBlockEnum.SystemControl: _roleBlock.SystemControl = "Visible"; break;
                    case (int)RoleBlockEnum.MeetingControl: _roleBlock.MeetingControl = "Visible"; break;
                    case (int)RoleBlockEnum.DeviceManagement: _roleBlock.DeviceManagement = "Visible"; break;
                    case (int)RoleBlockEnum.BasePerson: _roleBlock.BasePerson = "Visible"; break;
                    case (int)RoleBlockEnum.RoomManagement: _roleBlock.RoomManagement = "Visible"; break;
                    case (int)RoleBlockEnum.ScreenManagement: _roleBlock.ScreenManagement = "Visible"; break;
                    case (int)RoleBlockEnum.DoorManagement: _roleBlock.DoorManagement = "Visible"; break;
                    case (int)RoleBlockEnum.SeatManagement: _roleBlock.SeatManagement = "Visible"; break;
                    case (int)RoleBlockEnum.MeetingManagement: _roleBlock.MeetingManagement = "Visible"; break;
                    case (int)RoleBlockEnum.TopicManagement: _roleBlock.TopicManagement = "Visible"; break;
                    case (int)RoleBlockEnum.StaffManagement: _roleBlock.StaffManagement = "Visible"; break;
                    case (int)RoleBlockEnum.SeatDistribution: _roleBlock.SeatDistribution = "Visible"; break;
                    case (int)RoleBlockEnum.FileManagement: _roleBlock.FileManagement = "Visible"; break;
                    case (int)RoleBlockEnum.CardManagement: _roleBlock.CardManagement = "Visible"; break;
                    case (int)RoleBlockEnum.DeskCardManagement: _roleBlock.DeskCardManagement = "Visible"; break;
                    case (int)RoleBlockEnum.ReportInquire: _roleBlock.ReportInquire = "Visible"; break;
                    case (int)RoleBlockEnum.MeetingArchiving: _roleBlock.MeetingArchiving = "Visible"; break;
                    case (int)RoleBlockEnum.HotStandby: _roleBlock.HotStandby = "Visible"; break;
                    case (int)RoleBlockEnum.PersonFileDown: _roleBlock.PersonFileDown = "Visible"; break;
                    case (int)RoleBlockEnum.DataBaseBackUp: _roleBlock.DataBaseBackUp = "Visible"; break;
                    case (int)RoleBlockEnum.SystemUser: _roleBlock.SystemUser = "Visible"; break;
                    case (int)RoleBlockEnum.BlockManagement: _roleBlock.BlockManagement = "Visible"; break;
                    case (int)RoleBlockEnum.DataConfiger: _roleBlock.DataConfiger = "Visible"; break;
                    case (int)RoleBlockEnum.DictionaryManagement: _roleBlock.DictionaryManagement = "Visible"; break;
                    case (int)RoleBlockEnum.Help: _roleBlock.Help = "Visible"; break;
                    case (int)RoleBlockEnum.MC_CheckControl: _roleBlock.MC_CheckControl = "Visible"; break;
                    case (int)RoleBlockEnum.MC_PinCheck: _roleBlock.MC_PinCheck = "Visible"; break;
                    case (int)RoleBlockEnum.MC_Discuss: _roleBlock.MC_Discuss = "Visible"; break;
                    case (int)RoleBlockEnum.MC_SpeakControl: _roleBlock.MC_SpeakControl = "Visible"; break;
                    case (int)RoleBlockEnum.MC_VoteControl: _roleBlock.MC_VoteControl = "Visible"; break;
                    case (int)RoleBlockEnum.MC_CurrentOrder: _roleBlock.MC_CurrentOrder = "Visible"; break;
                    case (int)RoleBlockEnum.MC_ChangeScreen: _roleBlock.MC_ChangeScreen = "Visible"; break;
                    case (int)RoleBlockEnum.MC_DefaultScreen: _roleBlock.MC_DefaultScreen = "Visible"; break;
                    case (int)RoleBlockEnum.MC_ChangeHost: _roleBlock.MC_ChangeHost = "Visible"; break;
                    case (int)RoleBlockEnum.MC_CallControl: _roleBlock.MC_CallControl = "Visible"; break;
                    case (int)RoleBlockEnum.MC_ClientCtrol: _roleBlock.MC_ClientCtrol = "Visible"; break;
                    case (int)RoleBlockEnum.MC_FileShow: _roleBlock.MC_FileShow = "Visible"; break;
                    case (int)RoleBlockEnum.MC_PlayMusic: _roleBlock.MC_PlayMusic = "Visible"; break;
                    case (int)RoleBlockEnum.MC_Tools: _roleBlock.MC_Tools = "Visible"; break;
                    case (int)RoleBlockEnum.MC_Help: _roleBlock.MC_Help = "Visible"; break;
                    case (int)RoleBlockEnum.MC_Monitor: _roleBlock.MC_Monitor = "Visible"; break;
                    case (int)RoleBlockEnum.MC_Status: _roleBlock.MC_Status = "Visible"; break;
                    case (int)RoleBlockEnum.MC_SysInfo: _roleBlock.MC_SysInfo = "Visible"; break;
                    case (int)RoleBlockEnum.MC_MeetingInfo: _roleBlock.MC_MeetingInfo = "Visible"; break;
                    case (int)RoleBlockEnum.VersionControl: _roleBlock.VersionControl = "Visible"; break;
                    default:
                        break;

                }
            }
            RoleBlock = _roleBlock;
           
        }

        //show timer by_songgp
        public void ShowCurTimer(object sender, EventArgs e)
        {
            //"星期"+DateTime.Now.DayOfWeek.ToString(("d"))

            //获得星期几
            //this.TimerStr = DateTime.Now.ToString("dddd", new System.Globalization.CultureInfo("zh-cn"));
            //this.TimerStr += " ";
            //获得年月日
            this.TimerStr = DateTime.Now.ToString("yyyy年MM月dd日");   //yyyy年MM月dd日
            this.TimerStr += " ";
            //获得时分秒
            this.TimerStr += DateTime.Now.ToString("HH:mm:ss");
            //System.Diagnostics.Debug.Print("this.ShowCurrentTime {0}", this.ShowCurrentTime);
        }
        #endregion
    }
}
