using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain;
using Dao;
using RCUHost;

namespace Service.Implement
{
    public class HostAirManager : GenericManagerBase<HostAir>, IHostAirManager
    {
        public IHostRepository HostRepository { get; set; }

        public IHotelSeasonRepository HotelSeasonRepository { get; set; }

        public IAirConditionStatusReceiver AirConditionStatusReceiver { get; set; }

        public IList<HostAir> LoadByHostID(int hostID)
        {
            return ((IHostAirRepository)CurrentRepository).LoadByHostID(hostID).OrderBy(r => r.Modal.Sort).ToList();
        }

        /// <summary>
        /// 空调设置整体下发
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="host"></param>
        public void ApplyAirConditionSetting(HostAir entity, Host host)
        {
            AirConditionStatusReceiver.SendAirConditionSetting(entity, host);
        }

        /// <summary>
        /// 空调单个属性下发
        /// </summary>
        /// <param name="host"></param>
        /// <param name="airNo"></param>
        /// <param name="property"></param>
        /// <param name="status"></param>
        /// <param name="seasonData">季节数据</param>
        public void SetAirProperty(Host host, int airNo, AirProperty property, int status, byte[] seasonData = null)
        {
            AirConditionStatusReceiver.SetAirProperty(host, airNo, property, status, seasonData);
            if (property == AirProperty.Season)
            {
                var result = HotelSeasonRepository.LoadByHotelID(host.SysHotel.ID);
                if (result == null)
                {
                    result = new HotelSeason();
                }
                result.SysHotel = host.SysHotel;
                result.Month1 = (int)seasonData[0];
                result.Month2 = (int)seasonData[1];
                result.Month3 = (int)seasonData[2];
                result.Month4 = (int)seasonData[3];
                result.Month5 = (int)seasonData[4];
                result.Month6 = (int)seasonData[5];
                result.Month7 = (int)seasonData[6];
                result.Month8 = (int)seasonData[7];
                result.Month9 = (int)seasonData[8];
                result.Month10 = (int)seasonData[9];
                result.Month11 = (int)seasonData[10];
                result.Month12 = (int)seasonData[11];
                HotelSeasonRepository.SaveOrUpdate(result);
            }
            else
            {
                var hostAir = Get(host.ID, airNo);
                if (hostAir != null)
                {
                    switch (property)
                    {
                        case AirProperty.ColdHotMode:
                            hostAir.ColdHotMode = status;
                            break;
                        case AirProperty.Mode:
                            hostAir.Mode = status;
                            break;
                        case AirProperty.SettingTemp:
                            hostAir.SettingTemp = status;
                            break;
                        case AirProperty.Speed:
                            hostAir.Speed = status;
                            break;
                        case AirProperty.KeepTemp:
                            hostAir.KeepTemp = status;
                            break;
                        case AirProperty.InitTemp:
                            hostAir.InitTemp = status;
                            break;
                        case AirProperty.DeadTemp:
                            hostAir.DeadTemp = status;
                            break;
                        case AirProperty.LowerTemp:
                            hostAir.LowerTemp = status;
                            break;
                        case AirProperty.HighTemp:
                            hostAir.HightTemp = status;
                            break;
                        case AirProperty.ColdDevition:
                            hostAir.ColdDevition = status;
                            break;
                        case AirProperty.HotDevition:
                            hostAir.HotDevition = status;
                            break;
                        case AirProperty.ColdHotSwitchDelayTime:
                            hostAir.ColdHotSwitchDelayTime = status;
                            break;
                        case AirProperty.WelcomeTime:
                            hostAir.WelcomeTime = status;
                            break;
                        case AirProperty.RelateDoorContact:
                            hostAir.RelateDoorContact = (status != 0);
                            break;
                        case AirProperty.RelateRoomStatus:
                            hostAir.RelateRoomStatus = (status != 0);
                            break;
                        case AirProperty.FanStop:
                            hostAir.FanStop = (status != 0);
                            break;
                        case AirProperty.DisableFanHighSpeed:
                            hostAir.DisableFanHighSpeed = (status != 0);
                            break;

                    }
                    CurrentRepository.Update(hostAir);
                }
            }
        }
        /// <summary>
        /// 设置空调补偿温度
        /// </summary>
        /// <param name="host"></param>
        /// <param name="airNo"></param>
        /// <param name="compensatoryTemp"></param>
        public void SetCompensatoryTemp(Host host, int airNo, float compensatoryTemp)
        {
            AirConditionStatusReceiver.SetCompensatoryTemp(host, airNo, compensatoryTemp);
            var hostAir = GetByModalTypeID(host.ID, airNo);
            if (hostAir != null)
            {
                hostAir.CompensatoryTemp = compensatoryTemp;
                CurrentRepository.Update(hostAir);
            }
        }

        /// <summary>
        /// 设置空调锁定温度
        /// </summary>
        /// <param name="host"></param>
        /// <param name="airNo"></param>
        /// <param name="isLock"></param>
        /// <param name="lockTemp"></param>
        public void SetLockTemp(Host host, int airNo, bool isLock, int lockTemp)
        {
            AirConditionStatusReceiver.SetLockTemp(host, airNo, isLock, lockTemp);
            var hostAir = GetByModalTypeID(host.ID, airNo);
            if (hostAir != null)
            {
                hostAir.IsLockTemp = isLock;
                hostAir.LockTemp = lockTemp;
                CurrentRepository.Update(hostAir);
            }
        }

        /// <summary>
        /// 设置空调睡眠模式
        /// </summary>
        /// <param name="host"></param>
        /// <param name="airNo"></param>
        /// <param name="sleepFlag"></param>
        /// <param name="SleepDevition"></param>
        /// <param name="sleepStartTime"></param>
        /// <param name="sleepEndTime"></param>
        public void SetSleepMode(Host host, int airNo, bool sleepFlag, int sleepDevition, string sleepStartTime, string sleepEndTime)
        {
            AirConditionStatusReceiver.SetSleepMode(host, airNo, sleepFlag, sleepDevition, sleepStartTime, sleepEndTime);
            var hostAir = GetByModalTypeID(host.ID, airNo);
            if (hostAir != null)
            {
                hostAir.SleepFlag = sleepFlag;
                hostAir.SleepDevition = sleepDevition;
                hostAir.SleepStartTime = sleepStartTime;
                hostAir.SleepEndTime = sleepEndTime;
                CurrentRepository.Update(hostAir);
            }
        }

        /// <summary>
        /// 设置空调定时设置
        /// </summary>
        /// <param name="host"></param>
        /// <param name="airNo"></param>
        /// <param name="timeFlag"></param>
        /// <param name="startTime1"></param>
        /// <param name="endTime1"></param>
        /// <param name="startTime2"></param>
        /// <param name="endTime2"></param>
        /// <param name="startTime3"></param>
        /// <param name="endTime3"></param>
        public void SetTimeSetting(Host host, int airNo, bool timeFlag, string startTime1, string endTime1, string startTime2, string endTime2, string startTime3, string endTime3)
        {
            AirConditionStatusReceiver.SetTimeSetting(host, airNo, timeFlag, startTime1, endTime1, startTime2, endTime2, startTime3, endTime3);
            var hostAir = GetByModalTypeID(host.ID, airNo);
            if (hostAir != null)
            {
                hostAir.TimeFlag = timeFlag;
                hostAir.TimeStartTime1 = startTime1;
                hostAir.TimeEndTime1 = endTime1;
                hostAir.TimeStartTime2 = startTime2;
                hostAir.TimeEndTime2 = endTime2;
                hostAir.TimeStartTime3 = startTime3;
                hostAir.TimeEndTime3 = endTime3;
                CurrentRepository.Update(hostAir);
            }
        }

        #region Override Methods

        public override void Update(HostAir entity)
        {
            base.Update(entity);

            AirConditionStatusReceiver.SendAirConditionSetting(entity, HostRepository.Get(entity.HostID));
        }

        #endregion

        public HostAir Get(int hostID, int roomTypeAirId)
        {
            return CurrentRepository.LoadAll().OrderBy(r => r.Modal.Sort).FirstOrDefault(r => r.HostID == hostID && r.Modal.ID == roomTypeAirId);
        }

        public HostAir GetByModalTypeID(int hostID, int modalTypeId)
        {
            return CurrentRepository.LoadAll().OrderBy(r => r.Modal.Sort).FirstOrDefault(r => r.HostID == hostID && r.Modal.ModalType.ID == modalTypeId);
        }

        public void Delete(int hostID)
        {
            ((IHostAirRepository)CurrentRepository).DeteleByHostID(hostID);
        }

        public void Delete(int hostID, int roomTypeAirID)
        {
            ((IHostAirRepository)CurrentRepository).DeteleByRoomTypeAirID(hostID, roomTypeAirID);
        }
    }
}
