using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain;
using Dao;
using RCUHost;
using RCUHost.Protocols;
using System.Threading.Tasks;

namespace Service.Implement
{
    public class HostModalManager : GenericManagerBase<HostModal>, IHostModalManager
    {
        public IDeviceControlReceiver DeviceControlReceiver { get; set; }
        public IHostRegisterReceiver  HostRegisterReceiver { get; set; }

        public HostModal Get(int hostID, int roomTypeModalId)
        {
            var QQQ = ((IHostModalRepository)CurrentRepository).Get(hostID, roomTypeModalId);
            if (QQQ != null)
            {
                StatusTran(hostID, QQQ.Modal.ModalAddress, QQQ);
            }
            return QQQ;
        }

        public HostModal GetByModalAddress(int hostID, string modalAddress)
        {
            var QQQ = ((IHostModalRepository)CurrentRepository).Get(hostID, modalAddress);
            StatusTran(hostID, modalAddress, QQQ);
            return QQQ;
        }

        public HostModal GetByModalName(int hostID, string name)
        {
            var QQQ = ((IHostModalRepository)CurrentRepository).LoadAll().Where(r => r.HostID == hostID && r.Modal != null && r.Modal.Name == name).FirstOrDefault();
            if (QQQ != null)
            {
                StatusTran(hostID, QQQ.Modal.ModalAddress, QQQ);
            }
            return QQQ;
        }

        public IList<HostModal> Load(int hostID, DeviceType type)
        {
            var LLL = ((IHostModalRepository)CurrentRepository).LoadAll().Where(r => r.HostID == hostID && r.Modal != null && r.Modal.Type == type).ToList();
            foreach (var QQQ in LLL)
            {
                if (QQQ != null)
                {
                    StatusTran(hostID, QQQ.Modal.ModalAddress, QQQ);
                }
            }
            return LLL;
        }

        public IList<HostModal> LoadByHostID(int hostID)
        {
            var LLL = ((IHostModalRepository)CurrentRepository).LoadByHostID(hostID).ToList();
            foreach (var QQQ in LLL)
            {
                if (QQQ != null)
                {
                    StatusTran(hostID, QQQ.Modal.ModalAddress, QQQ);
                }
            }
            return LLL;
        }

        public IList<HostModal> LoadAllLight(int hostID)
        {
            var LLL = ((IHostModalRepository)CurrentRepository).LoadAll().Where(r => r.HostID == hostID && r.Modal != null && r.Modal.Name.Contains("灯")).OrderBy(r => r.Modal.Sort).ToList();
            foreach (var QQQ in LLL)
            {
                if (QQQ != null)
                {
                    StatusTran(hostID, QQQ.Modal.ModalAddress, QQQ);
                }
            }
            return LLL;
        }

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

        public void DeteleByRoomTypeModal(int hostID, int roomTypeModalID)
        {
            ((IHostModalRepository)CurrentRepository).DeteleByRoomTypeModal(hostID, roomTypeModalID);
        }

        /// <summary>
        /// 向某个结点发送数据
        /// </summary>
        /// <param name="hostnumber"></param>
        /// <param name="mac"></param>
        /// <param name="Data"></param>
        public void SendData(string hostnumber, string mac, byte[] Data)
        {
            Tuple<string, string> t = new Tuple<string, string>(hostnumber, mac);
            System.Threading.Tasks.Task.Factory.StartNew((state) =>
            {
                Tuple<string, string> t1 = state as Tuple<string, string>;
                var host1 = t1.Item1;
                var mac1 = t1.Item2;
                HostRegisterReceiver.Send(hostnumber, mac);//发送命令
            }, t, System.Threading.CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default);
        }

        public void SetDevice(Host host, HostModal hostModal, int status, int brightness, int temperature, int fanSpeed, int mode, int valve)
        {
            Device device = new Device();
            device.Address = hostModal.Modal.ModalAddress;
            device.AddressType = AddressType.DeviceAddress;
            device.Type = hostModal.Modal.Type;
            device.Status = (byte)status;
            device.Brightness = (byte)brightness;
            device.Temperature = (byte)temperature;
            device.FanSpeed = (byte)fanSpeed;
            device.Mode = (byte)mode;
            device.Valve = (byte)valve;
            device.AirExecMode = (status << 14) + (mode << 12) + (fanSpeed << 10) + (valve << 8) + temperature;//空调执行方式和内容
            device.FloorHotExecMode = (status << 12) + (mode << 8) + (valve << 6) + temperature;//地暖执行方式和内容
            device.MusicExecMode = status + (brightness << 12) + (mode << 8);//背景音乐执行方式和内容
            //device.ColorTempExecMode = status + (brightness << 12) + (temperature << 8);//色温执行方式和内容

            var t = new Tuple<Host, Device>(host, device);
            System.Threading.Tasks.Task.Factory.StartNew((state) =>
            {
                var t1 = state as Tuple<Host, Device>;
                var host1 = t1.Item1;
                var device1 = t1.Item2;
                DeviceControlReceiver.Send(host1, device1);//发送命令
            }, t, System.Threading.CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default);
        }


        public void SetDevice_Repeat(string SingleKey, Host host, HostModal hostModal, int status, int brightness, int temperature, int fanSpeed, int mode, int valve)
        {
            Device device = new Device();
            device.Address = hostModal.Modal.ModalAddress;
            device.AddressType = AddressType.DeviceAddress;
            device.Type = hostModal.Modal.Type;
            device.Status = (byte)status;
            device.Brightness = (byte)brightness;
            device.Temperature = (byte)temperature;
            device.FanSpeed = (byte)fanSpeed;
            device.Mode = (byte)mode;
            device.Valve = (byte)valve;
            device.AirExecMode = (status << 14) + (mode << 12) + (fanSpeed << 10) + (valve << 8) + temperature;//空调执行方式和内容
            device.FloorHotExecMode = (status << 12) + (mode << 8) + (valve << 6) + temperature;//地暖执行方式和内容
            device.MusicExecMode = status + (brightness << 12) + (mode << 8);//背景音乐执行方式和内容
            //device.ColorTempExecMode = status + (brightness << 12) + (temperature << 8);//色温执行方式和内容

            var t = new Tuple<Host, Device, string>(host, device, SingleKey);
            System.Threading.Tasks.Task.Factory.StartNew((state) =>
            {
                var t1 = state as Tuple<Host, Device, string>;
                var host1 = t1.Item1;
                var device1 = t1.Item2;
                var key = t1.Item3;
                DeviceControlReceiver.Send_Repeat(key, host1, device1);//发送命令
            }, t);
        }

        public void UpdateHostModalStatus(Host host1, string modalAddress1, int status1)
        {
            var t = new Tuple<Host, string, int>(host1, modalAddress1, status1);
            System.Threading.Tasks.Task.Factory.StartNew((state) =>
            {
                var t1 = state as Tuple<Host, string, int>;
                var host = t1.Item1;
                var modalAddress = t1.Item2;
                var status = t1.Item3;
                HostModal hostModal = ((IHostModalRepository)CurrentRepository).Get(host.ID, modalAddress);
                if (hostModal != null)
                {
                    Device device = new Device();
                    device.Address = modalAddress;
                    device.AddressType = AddressType.DeviceAddress;
                    device.Type = hostModal.Modal.Type;
                    device.Status = (byte)status;

                    DeviceControlReceiver.Send(host, device);
                }
            }, t, System.Threading.CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default);
        }


        private static void StatusTran(int hostID, string modalAddress, HostModal QQQ)
        {
            string KKey = CommonEntity.CacheKey.HostModalStatus_Prefix + "_" + hostID + "_" + modalAddress;
            var obj = Common.CSRedisCacheHelper.Get_Partition<CommonEntity.HostModal_Cache>(KKey);
            if (obj != null)
            {
                CommonEntity.HostModal_Cache hhh = obj as CommonEntity.HostModal_Cache;
                if (QQQ != null)
                {
                    QQQ.Status = hhh.Status;
                    QQQ.Brightness = hhh.Brightness;
                    var H = hhh.AirConditionData;
                    QQQ.CurrentTemp = H.CurrentTemp;
                    QQQ.SettingTemp = H.SettingTemp;
                    QQQ.FanSpeed = H.FanSpeed;
                    QQQ.Valve = H.Valve;
                    QQQ.Mode = H.Mode;
                    QQQ.UpdateTime = H.UpdateTime;
                }
            }
        }
    }
}
