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

namespace Service.Implement
{
    public class LightControlManager : ILightControlManager
    {
        public IRoomTypeSceneModalManager RoomTypeSceneModalManager { get; set; }
        public IDeviceControlReceiver DeviceControlReceiver { get; set; }
        /// <summary>
        /// 下发场景
        /// </summary>
        /// <param name="host">主机</param>
        /// <param name="scene">场景</param>
        /// <param name="takeInverse">是否取反</param>
        public void ApplyScene(Host host, RoomTypeScene scene, bool takeInverse = false)
        {
            if (scene.Type == SceneType.Custom)
            {
                var sceneModals = RoomTypeSceneModalManager.LoadAll(scene);
                if (sceneModals.Count > 0)
                {
                    var devices = new List<Device>();
                    foreach (var sceneModal in sceneModals)
                    {
                        Device device = new Device();
                        device.Address = sceneModal.Modal.ModalAddress;
                        device.AddressType = AddressType.DeviceAddress;
                        device.Type = sceneModal.Modal.Type;
                        if (takeInverse)
                        {
                            switch (sceneModal.Modal.Type)
                            {
                                case DeviceType.Curtain:
                                    if (sceneModal.Status == 1 || sceneModal.Status == 2)
                                    {
                                        device.Status = 0x06;
                                        device.Brightness = 0x00;
                                    }
                                    else
                                    {
                                        device.Status = (byte)sceneModal.Status;
                                        device.Brightness = (byte)sceneModal.Brightness;
                                    }
                                    break;
                                default:
                                    if (sceneModal.Status == 1)
                                    {
                                        device.Status = 0x02;
                                        device.Brightness = 0x00;
                                    }
                                    else
                                    {
                                        device.Status = (byte)sceneModal.Status;
                                        device.Brightness = (byte)sceneModal.Brightness;
                                    }
                                    break;
                            }
                        }
                        else
                        {
                            device.Status = (byte)sceneModal.Status;
                            device.Brightness = (byte)sceneModal.Brightness;
                            device.Temperature = (byte)sceneModal.CurrentTemp;
                            device.FanSpeed = (byte)sceneModal.FanSpeed;
                            device.Mode = (byte)sceneModal.Mode;
                            device.Valve = (byte)sceneModal.Valve;
                            device.AirExecMode = (sceneModal.Status << 14) + (sceneModal.Mode << 12) + (sceneModal.FanSpeed << 10) + (sceneModal.Valve << 8) + sceneModal.CurrentTemp;//空调执行方式和内容
                            device.FloorHotExecMode = (sceneModal.Status << 12) + (sceneModal.Mode << 8) + (sceneModal.Valve << 6) + sceneModal.CurrentTemp;//地暖执行方式和内容
                            device.MusicExecMode = sceneModal.Status + (sceneModal.Brightness << 12) + (sceneModal.Mode << 8);//背景音乐执行方式和内容
                        }
                        devices.Add(device);
                    }
                    DeviceControlReceiver.Send(host, devices);
                }
            }
            else if (scene.Type == SceneType.Command)
            {
                Device device = new Device();
                device.Address = scene.GroupAddress;
                device.AddressType = AddressType.GroupAddress;
                device.Status = (byte)(takeInverse ? 0x02 : 0x01);
                DeviceControlReceiver.Send(host, device);
            }
        }
        /// <summary>
        /// 控制所有灯开关和亮度
        /// </summary>
        /// <param name="host"></param>
        public void AllDeviceControl(Host host, List<Device> devices)
        {
            if (devices.Count > 0)
            {
                DeviceControlReceiver.Send(host, devices);
            }
        }

        /// <summary>
        /// 下发场景
        /// </summary>
        /// <param name="host">主机</param>
        /// <param name="scene">场景</param>
        /// <param name="takeInverse">是否取反</param>
        public void ApplyScene_Repeat(string Key,Host host, RoomTypeScene scene, bool takeInverse = false)
        {
            if (scene.Type == SceneType.Custom)
            {
                var sceneModals = RoomTypeSceneModalManager.LoadAll(scene);
                if (sceneModals.Count > 0)
                {
                    var devices = new List<Device>();
                    foreach (var sceneModal in sceneModals)
                    {
                        Device device = new Device();
                        device.Address = sceneModal.Modal.ModalAddress;
                        device.AddressType = AddressType.DeviceAddress;
                        device.Type = sceneModal.Modal.Type;
                        if (takeInverse)
                        {
                            switch (sceneModal.Modal.Type)
                            {
                                case DeviceType.Curtain:
                                    if (sceneModal.Status == 1 || sceneModal.Status == 2)
                                    {
                                        device.Status = 0x06;
                                        device.Brightness = 0x00;
                                    }
                                    else
                                    {
                                        device.Status = (byte)sceneModal.Status;
                                        device.Brightness = (byte)sceneModal.Brightness;
                                    }
                                    break;
                                default:
                                    if (sceneModal.Status == 1)
                                    {
                                        device.Status = 0x02;
                                        device.Brightness = 0x00;
                                    }
                                    else
                                    {
                                        device.Status = (byte)sceneModal.Status;
                                        device.Brightness = (byte)sceneModal.Brightness;
                                    }
                                    break;
                            }
                        }
                        else
                        {
                            device.Status = (byte)sceneModal.Status;
                            device.Brightness = (byte)sceneModal.Brightness;
                            device.Temperature = (byte)sceneModal.CurrentTemp;
                            device.FanSpeed = (byte)sceneModal.FanSpeed;
                            device.Mode = (byte)sceneModal.Mode;
                            device.Valve = (byte)sceneModal.Valve;
                            device.AirExecMode = (sceneModal.Status << 14) + (sceneModal.Mode << 12) + (sceneModal.FanSpeed << 10) + (sceneModal.Valve << 8) + sceneModal.CurrentTemp;//空调执行方式和内容
                            device.FloorHotExecMode = (sceneModal.Status << 12) + (sceneModal.Mode << 8) + (sceneModal.Valve << 6) + sceneModal.CurrentTemp;//地暖执行方式和内容
                            device.MusicExecMode = sceneModal.Status + (sceneModal.Brightness << 12) + (sceneModal.Mode << 8);//背景音乐执行方式和内容
                        }
                        devices.Add(device);
                    }
                    DeviceControlReceiver.Send_Repeat(Key,host, devices);
                }
            }
            else if (scene.Type == SceneType.Command)
            {
                Device device = new Device();
                device.Address = scene.GroupAddress;
                device.AddressType = AddressType.GroupAddress;
                device.Status = (byte)(takeInverse ? 0x02 : 0x01);
                DeviceControlReceiver.Send_Repeat(Key, host, device);
            }
        }
        
        /// <summary>
        /// 重复发送
        /// </summary>
        /// <param name="host"></param>
        /// <param name="devices"></param>
        /// <param name="Key"></param>
        public void AllDeviceControl_Repeat(string Key,Host host, List<Device> devices)
        {
            if (devices.Count > 0)
            {
                DeviceControlReceiver.Send_Repeat(Key,host, devices);
            }
        }
    }
}
