using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using Dao;
using Domain;
using RCUHost;
using RCUHost.Protocols;

namespace Service.Implement
{
	public class RoomServiceManager : GenericManagerBase<RoomService>, IRoomServiceManager
	{
		#region Fields

		public ISysUserRepository SysUserRepository { get; set; }        
		public IGroupRepository GroupRepository { get; set; }
		public IHostRepository HostRepository { get; set; }
		public IAlarmSettingRepository AlarmSettingRepository { get; set; }
		public IRoomTypeModalRepository RoomTypeModalRepository { get; set; }
		public IServiceReceiver ServiceReceiver { get; set; }
        public IHostFaultsRepository HostFaultsRepository { get; set; }

		#endregion

		public IList<RoomService> LoadAllByPage(out long total, int page, int rows, string order, string sort, string roomNumber, string startTime, string endTime, string[] services)
		{
			return ((Dao.IRoomServiceRepository)CurrentRepository).LoadAllByPage(out total, page, rows, order, sort, roomNumber, startTime, endTime, services);
		}

		#region 服务

		/// <summary>
		/// 加载客房当前服务
		/// </summary>
		/// <param name="hostId"></param>
		/// <returns></returns>
		public IList<RoomService> LoadCurrentRoomServices(int hostId)
		{
			return CurrentRepository.LoadAll().Where(r => r.HostID == hostId && r.AlarmType == 'B' && r.Status).ToList();
		}

        private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(RoomServiceManager));
        public DataTable LoadCurrentRoomServicesCount(int hotelID, string user)
        {
            CommonEntity.DataTongJi.InvokeCount("QueryCurrentRoomServiceStatistics_");
            SysUsers sysUser = SysUserRepository.Get(user);
            if (sysUser == null || sysUser.Group == null)
            {
                return null;
            }
            //logger.Error("hotelID is: "+hotelID+" groupid: "+sysUser.Group.ID+" "+DeviceType.ServiceInfo.ToString());
            if (sysUser.SysHotelGroup == null)
            {
                
                return ((Dao.IRoomServiceRepository)CurrentRepository).LoadCurrentRoomServicesOrAbnormitiesCount(hotelID, sysUser.Group.ID, DeviceType.ServiceInfo);
            }
            else
            {
                return ((Dao.IRoomServiceRepository)CurrentRepository).LoadCurrentRoomServicesOrAbnormitiesCount(hotelID, 0, DeviceType.ServiceInfo);//组用户获取本酒店所有
            }
        }

        //public DataTable LoadCurrentRoomAbnormitiesCount(int hotelID, string user)
        //{
        //    return ((Dao.IRoomServiceRepository)CurrentRepository).LoadCurrentRoomServicesOrAbnormitiesCount(hotelID, DeviceType.ServiceInfo);
        //}

        public IList<object[]> LoadCurrentRoomServices(int hotelID, string user)
		{
			SysUsers sysUser = SysUserRepository.Get(user);
			if (sysUser == null || sysUser.Group == null)
			{
				return new List<object[]>();
            }
            //var sysUserHotel = SysUserHotelRepository.Get(sysUser.ID, hotelID);//获取当前用户所在酒店
            var currentGroup = GroupRepository.LoadAll().FirstOrDefault(r => r.HotelID == hotelID);//Get(sysUserHotel != null ? sysUserHotel.GroupID : 0);//获取当前用户所在酒店的楼层

            IList<int> groupIDs = GroupRepository.GetGroupList(currentGroup).Select(r => r.ID).ToArray();
			if (groupIDs.Count == 0) 
			{
				return new List<object[]>();
			}

			return ((Dao.IRoomServiceRepository)CurrentRepository).LoadCurrentRoomServices(hotelID, groupIDs);
		}

        public IList<object[]> LoadCurrentHostFaultStatistics(int hotelID, string user, int type)
        {
            SysUsers sysUser = SysUserRepository.Get(user);
            if (sysUser == null || sysUser.Group == null)
            {
                return new List<object[]>();
            }
            //var sysUserHotel = SysUserHotelRepository.Get(sysUser.ID, hotelID);//获取当前用户所在酒店
            var currentGroup = GroupRepository.LoadAll().FirstOrDefault(r => r.Parent == null && r.HotelID == hotelID);//Get(sysUserHotel != null ? sysUserHotel.GroupID : 0);//获取当前用户所在酒店的楼层
            IList<int> groupIDs = GroupRepository.GetGroupList(currentGroup).Select(r => r.ID).ToArray();
            if (groupIDs.Count == 0)
            {
                return new List<object[]>();
            }
            return HostFaultsRepository.LoadCurrentHostFaultStatistics(hotelID, groupIDs, type);
        }
		#endregion

		#region 异常

		/// <summary>
		/// 加载客房当前异常
		/// </summary>
		/// <param name="hostId"></param>
		/// <returns></returns>
		public IList<RoomService> LoadCurrentRoomAbnormities(int hostId)
		{
			return CurrentRepository.LoadAll().Where(r => r.HostID == hostId && r.AlarmType == 'C' && r.Status).ToList();
		}

        public IList<object[]> LoadCurrentRoomAbnormities(int hotelID, string user)
		{
			SysUsers sysUser = SysUserRepository.Get(user);
			if (sysUser == null || sysUser.Group == null)
			{
				return new List<object[]>();
			}

			IList<int> groupIDs = GroupRepository.GetGroupList(sysUser.Group).Select(r => r.ID).ToArray();
			if (groupIDs.Count == 0)
			{
				return new List<object[]>();
			}

			return ((Dao.IRoomServiceRepository)CurrentRepository).LoadCurrentRoomAbnormities(hotelID, groupIDs);
		}

		#endregion

		public void SetService(Host host, string alarmCode, int status)
		{
            var serviceSetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'B', alarmCode);

			if (serviceSetting != null)
			{
				ServiceReceiver.SetService(host, serviceSetting.ModalTypeID, status);

				var service = ((Dao.IRoomServiceRepository)CurrentRepository).Get(host.ID, alarmCode);
				if (service != null)
				{
                    service.Status = (status == 0 ? false : true);
                    service.StartTime = DateTime.Now;
					CurrentRepository.Update(service);
				}

				//设置连通房服务
                //if (host.IsConnectingRoom)
                //{
                //    var hostList = HostRepository.GetConnectRoomHosts(host);

                //    foreach (var item in hostList)
                //    {
                //        ServiceReceiver.SetService(item, serviceSetting.ModalTypeID, status);

                //        service = ((Dao.IRoomServiceRepository)CurrentRepository).Get(host.ID, alarmCode);
                //        if (service != null)
                //        {
                //            service.Status = (status == 0 ? false : true);
                //            CurrentRepository.Update(service);
                //        }
                //    }
                //}
			}
		}

		/// <summary>
		/// 清除服务
		/// </summary>
		/// <param name="host"></param>
		/// <param name="service"></param>
		/// <param name="user"></param>
		public void ClearRoomService(Host host, RoomService service, string user)
		{
			var serviceSetting = AlarmSettingRepository.LoadAll().FirstOrDefault(r => r.Code == service.AlarmCode);

			if (serviceSetting != null)
			{
				ServiceReceiver.SetService(host, serviceSetting.ModalTypeID, 0);

				//清除连通房服务
                //if (host.IsConnectingRoom)
                //{
                //    var hostList = HostRepository.GetConnectRoomHosts(host);

                //    foreach (var item in hostList)
                //    {
                //        ServiceReceiver.SetService(item, serviceSetting.ModalTypeID, 0);
                //    }
                //}
			}

			service.Status = false;
			CurrentRepository.Update(service);
		}

		public RoomService Get(int hostID, string alarmCode)
		{
			return ((Dao.IRoomServiceRepository)CurrentRepository).Get(hostID, alarmCode);
		}
	}
}
