using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using Common;
using Dao;
using Domain;

namespace RCUHost.Implement
{
    public class AbnormalAlarmHandler
    {
        public IAlarmSettingRepository AlarmSettingRepository { get; set; }
        public IRoomServiceRepository RoomServiceRepository { get; set; }
        public IRoomServiceRecordRepository RoomServiceRecordRepository { get; set; }

        /// <summary>
        /// 处理“非客人在房门关”异常
        /// </summary>
        /// <param name="host"></param>
        public void FKRZFMG(Host host)
        {
            // "非客人在房门关报警" 设置
            var abnormitySetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'A', "A05");
            if (abnormitySetting != null)
            {
                bool isUpdated = false;
                var abnormity = RoomServiceRepository.Get(host.ID, "C06");
                if (abnormity != null && host.RoomCard != null && host.RoomCard.RoomCardType != null && host.RoomCard.RoomCardType.ID != 0x20 && !host.DoorLockStatus)
                {
                    isUpdated = true;
                    var delaySetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'A', "A06");
                    if (delaySetting == null || Convert.ToInt16(delaySetting.Value) == 0)//不延时报警
                    {
                        if (!abnormity.Status)
                        {
                            abnormity.Status = true;
                            abnormity.StartTime = DateTime.Now;
                            RoomServiceRepository.Update(abnormity);
                            // “非客人在房门关”异常 记录
                            var abnormityRecordSetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'C', "C06");
                            var abnormityRecord = RoomServiceRecordRepository.Get(host.ID, "C06", true);
                            if (abnormityRecordSetting != null && abnormityRecord == null)
                            {
                                abnormityRecord = RoomServiceRecordRepository.CreateServiceRecord(host, abnormityRecordSetting);
                                RoomServiceRecordRepository.Save(abnormityRecord);
                            }
                        }
                    }
                    else
                    {
                        //启用线程执行延时报警
                        Tools.SetTimeout(Convert.ToInt16(delaySetting.Value) * 1000, delegate
                        {
                            if (!abnormity.Status)
                            {
                                abnormity.Status = true;
                                abnormity.StartTime = DateTime.Now;
                                RoomServiceRepository.Update(abnormity);
                                // “非客人在房门关”异常 记录
                                var abnormityRecordSetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'C', "C06");
                                var abnormityRecord = RoomServiceRecordRepository.Get(host.ID, "C06", true);
                                if (abnormityRecordSetting != null && abnormityRecord == null)
                                {
                                    abnormityRecord = RoomServiceRecordRepository.CreateServiceRecord(host, abnormityRecordSetting);
                                    RoomServiceRecordRepository.Save(abnormityRecord);
                                }
                            }
                        });
                    }
                }
                if (!isUpdated)
                {
                    if (abnormity != null && abnormity.Status)
                    {
                        abnormity.Status = false;
                        abnormity.StartTime = DateTime.Now;
                        RoomServiceRepository.Update(abnormity);
                        // “非客人在房门关 记录
                        var abnormityRecord = RoomServiceRecordRepository.Get(host.ID, "C06", true);
                        if (abnormityRecord != null)
                        {
                            abnormityRecord.Status = false;
                            abnormityRecord.EndTime = DateTime.Now;
                            RoomServiceRecordRepository.Update(abnormityRecord);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 处理“客人在房门开”异常
        /// </summary>
        /// <param name="host"></param>
        public void KRZFMK(Host host)
        {
            // "客人在房门开报警" 设置
            var abnormitySetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'A', "A01");
            if (abnormitySetting != null)
            {
                if (host.RoomCard != null && host.RoomCard.RoomCardType != null && host.RoomCard.RoomCardType.ID == 0x20 && host.DoorLockStatus)
                {
                    //报警延时设置
                    var delaySetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'A', "A02");
                    if (delaySetting == null || Convert.ToInt16(delaySetting.Value) == 0)//不延时报警
                    {
                        var abnormity = RoomServiceRepository.Get(host.ID, "C01");
                        if (abnormity != null && !abnormity.Status)
                        {
                            abnormity.Status = true;
                            abnormity.StartTime = DateTime.Now;
                            RoomServiceRepository.Update(abnormity);
                            // “客人在房门开”异常 记录
                            var abnormityRecordSetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'C', "C01");
                            var abnormityRecord = RoomServiceRecordRepository.Get(host.ID, "C01", true);
                            if (abnormityRecordSetting != null && abnormityRecord == null)
                            {
                                abnormityRecord = RoomServiceRecordRepository.CreateServiceRecord(host, abnormityRecordSetting);
                                RoomServiceRecordRepository.Save(abnormityRecord);
                            }
                        }
                    }
                    else
                    {
                        //启用线程执行延时报警
                        Tools.SetTimeout(Convert.ToInt16(delaySetting.Value) * 1000, delegate
                        {
                            var abnormity = RoomServiceRepository.Get(host.ID, "C01");
                            if (abnormity != null && !abnormity.Status)
                            {
                                abnormity.Status = true;
                                abnormity.StartTime = DateTime.Now;
                                RoomServiceRepository.Update(abnormity);
                                // “客人在房门开”异常 记录
                                var abnormityRecordSetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'C', "C01");
                                var abnormityRecord = RoomServiceRecordRepository.Get(host.ID, "C01", true);
                                if (abnormityRecordSetting != null && abnormityRecord == null)
                                {
                                    abnormityRecord = RoomServiceRecordRepository.CreateServiceRecord(host, abnormityRecordSetting);
                                    RoomServiceRecordRepository.Save(abnormityRecord);
                                }
                            }
                        });
                    }
                }
                else
                {
                    var abnormity = RoomServiceRepository.Get(host.ID, "C01");
                    if (abnormity != null && abnormity.Status)
                    {
                        abnormity.Status = false;
                        abnormity.StartTime = DateTime.Now;
                        RoomServiceRepository.Update(abnormity);
                        // “客人在房门开”异常 记录
                        var abnormityRecord = RoomServiceRecordRepository.Get(host.ID, "C01", true);
                        if (abnormityRecord != null)
                        {
                            abnormityRecord.EndTime = DateTime.Now;
                            abnormityRecord.Status = false;
                            RoomServiceRecordRepository.Update(abnormityRecord);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 处理“无人房门开”异常
        /// </summary>
        /// <param name="host"></param>
        public void WRFMK(Host host)
        {
            // "无人在房门开报警" 设置
            var abnormitySetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'A', "A03");
            if (abnormitySetting != null)
            {
                if (host.RoomCard == null && host.DoorLockStatus)
                {
                    //报警延时设置
                    var delaySetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'A', "A04");
                    if (delaySetting == null || Convert.ToInt16(delaySetting.Value) == 0)//不延时报警
                    {
                        var abnormity = RoomServiceRepository.Get(host.ID, "C02");
                        if (abnormity != null && !abnormity.Status)
                        {
                            abnormity.Status = true;
                            abnormity.StartTime = DateTime.Now;
                            RoomServiceRepository.Update(abnormity);
                            // “无人房门开”异常 记录
                            var abnormityRecordSetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'C', "C02");
                            var abnormityRecord = RoomServiceRecordRepository.Get(host.ID, "C02", true);
                            if (abnormityRecordSetting != null && abnormityRecord == null)
                            {
                                abnormityRecord = RoomServiceRecordRepository.CreateServiceRecord(host, abnormityRecordSetting);
                                RoomServiceRecordRepository.Save(abnormityRecord);
                            }
                        }
                    }
                    else
                    {
                        //启用线程执行延时报警
                        Tools.SetTimeout(Convert.ToInt16(delaySetting.Value) * 1000, delegate
                        {
                            var abnormity = RoomServiceRepository.Get(host.ID, "C02");
                            if (abnormity != null && !abnormity.Status)
                            {
                                abnormity.Status = true;
                                abnormity.StartTime = DateTime.Now;
                                RoomServiceRepository.Update(abnormity);
                                // “无人房门开”异常 记录
                                var abnormityRecordSetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'C', "C02");
                                var abnormityRecord = RoomServiceRecordRepository.Get(host.ID, "C02", true);
                                if (abnormityRecordSetting != null && abnormityRecord == null)
                                {
                                    abnormityRecord = RoomServiceRecordRepository.CreateServiceRecord(host, abnormityRecordSetting);
                                    RoomServiceRecordRepository.Save(abnormityRecord);
                                }
                            }
                        });
                    }
                }
                else
                {
                    var abnormity = RoomServiceRepository.Get(host.ID, "C02");
                    if (abnormity != null && abnormity.Status)
                    {
                        abnormity.Status = false;
                        abnormity.StartTime = DateTime.Now;
                        RoomServiceRepository.Update(abnormity);
                        // “无人房门开”异常 记录
                        var abnormityRecord = RoomServiceRecordRepository.Get(host.ID, "C02", true);
                        if (abnormityRecord != null)
                        {
                            abnormityRecord.EndTime = DateTime.Now;
                            abnormityRecord.Status = false;
                            RoomServiceRecordRepository.Update(abnormityRecord);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 处理“非客人在保险箱开”异常
        /// </summary>
        /// <param name="host"></param>
        public void FKRZBXXK(Host host)
        {
            var abnormitySetting = AlarmSettingRepository.Get(host.SysHotel.ID, 'C', "C03");
            var abnormity = RoomServiceRepository.Get(host.ID, abnormitySetting.Code);
            if (abnormitySetting != null && abnormity != null)
            {
                bool isUpdated = false;
                //如果非客人，且已出租，且保险箱开，则报警
                if (host.RoomCard != null && host.RoomCard.RoomCardType != null && host.RoomCard.RoomCardType.ID != 0x20 &&
                    host.RoomStatus != null && host.RoomStatus.ID == 0x02 && host.SafeStatus == 1)
                {
                    isUpdated = true;
                    if (!abnormity.Status)
                    {
                        abnormity.Status = true;
                        abnormity.StartTime = DateTime.Now;
                        RoomServiceRepository.Update(abnormity);

                        var abnormityRecord = RoomServiceRecordRepository.Get(host.ID, "C03", true);
                        if (abnormityRecord == null)
                        {
                            abnormityRecord = RoomServiceRecordRepository.CreateServiceRecord(host, abnormitySetting);
                            RoomServiceRecordRepository.Save(abnormityRecord);
                        }
                    }
                }
                if (!isUpdated)//关掉报警
                {
                    if (abnormity.Status)
                    {
                        abnormity.Status = false;
                        abnormity.StartTime = DateTime.Now;
                        RoomServiceRepository.Update(abnormity);

                        var abnormityRecord = RoomServiceRecordRepository.Get(host.ID, "C03", true);
                        if (abnormityRecord != null)
                        {
                            abnormityRecord.Status = false;
                            abnormityRecord.EndTime = DateTime.Now;
                            RoomServiceRecordRepository.Update(abnormityRecord);
                        }
                    }
                }
            }
        }
    }
}
