using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Effects;
using Aitex.Core.Common.DeviceData;

namespace Aitex.Core.UI.DeviceControl
{
    public partial class AITSignalTower : UserControl
    {
        public static readonly DependencyProperty DeviceDataProperty = DependencyProperty.Register(
            nameof(DeviceData), typeof(AITSignalTowerData), typeof(AITSignalTower),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));

        public static readonly DependencyProperty BuzzingWarningConfigCommandProperty = DependencyProperty.Register(
            nameof(BuzzingWarningConfigCallback), typeof(Action), typeof(AITSignalTower),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.None));

        public Action BuzzingWarningConfigCallback
        {
            get => (Action)GetValue(BuzzingWarningConfigCommandProperty);
            set => SetValue(BuzzingWarningConfigCommandProperty, value);
        }

        public AITSignalTowerData DeviceData
        {
            get => (AITSignalTowerData)GetValue(DeviceDataProperty);
            set => SetValue(DeviceDataProperty, value);
        }

        private readonly DropShadowEffect _redLightEffect = new();
        private readonly DropShadowEffect _yellowLightEffect = new();
        private readonly DropShadowEffect _greenLightEffect = new();
        private readonly DropShadowEffect _blueLightEffect = new();
        private readonly DropShadowEffect _buzzerLightEffect = new();
        public double OutLights = 0.2;

        public AITSignalTower()
        {
            InitializeComponent();
            _redLightEffect.BlurRadius = 20;
            _redLightEffect.Opacity = 1;
            _redLightEffect.Color = Colors.Red;

            _yellowLightEffect.BlurRadius = 20;
            _yellowLightEffect.Opacity = 1;
            _yellowLightEffect.Color = Colors.Yellow;

            _greenLightEffect.BlurRadius = 20;
            _greenLightEffect.Opacity = 1;
            _greenLightEffect.Color = Colors.Green;

            _blueLightEffect.BlurRadius = 20;
            _blueLightEffect.Opacity = 1;
            _blueLightEffect.Color = Colors.Blue;

            _buzzerLightEffect.BlurRadius = 20;
            _buzzerLightEffect.Opacity = 1;
            _buzzerLightEffect.Color = Colors.White;
        }




        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);

            if (DeviceData == null)
                return;

            if (DeviceData.IsRedLightOn)
            {
                if (rectangle1.Opacity != 1)
                {
                    rectangle1.Opacity = 1;
                    rectangle1.Effect = _redLightEffect;
                }
            }
            else
            {
                if (rectangle1.Opacity != OutLights)
                {
                    rectangle1.Opacity = OutLights;
                    rectangle1.Effect = null;
                }
            }

            if (DeviceData.IsYellowLightOn)
            {
                if (rectangle2.Opacity != 1)
                {
                    rectangle2.Opacity = 1;
                    rectangle2.Effect = _yellowLightEffect;
                }
            }
            else
            {
                if (rectangle2.Opacity != OutLights)
                {
                    rectangle2.Opacity = OutLights;
                    rectangle2.Effect = null;
                }
            }

            if (DeviceData.IsGreenLightOn)
            {
                if (rectangle3.Opacity != 1)
                {
                    rectangle3.Opacity = 1;
                    rectangle3.Effect = _greenLightEffect;
                }
            }
            else
            {
                if (rectangle3.Opacity != OutLights)
                {
                    rectangle3.Opacity = OutLights;
                    rectangle3.Effect = null;
                }
            }

            if (DeviceData.IsBlueLightOn)
            {
                if (rectangle4.Opacity != 1)
                {
                    rectangle4.Opacity = 1;
                    rectangle4.Effect = _blueLightEffect;
                }
            }
            else
            {
                if (rectangle4.Opacity != OutLights)
                {
                    rectangle4.Opacity = OutLights;
                    rectangle4.Effect = null;
                }
            }

            if (DeviceData.IsBuzzerOn || DeviceData.IsBuzzer1On || DeviceData.IsBuzzer2On || DeviceData.IsBuzzer3On ||
                DeviceData.IsBuzzer4On)
            {
                if (rectangle5.Opacity != 1)
                {
                    rectangle5.Opacity = 1;
                    rectangle5.Effect = _buzzerLightEffect;
                }
            }
            else
            {
                if (rectangle5.Opacity != OutLights)
                {
                    rectangle5.Opacity = OutLights;
                    rectangle5.Effect = null;
                }
            }


        }

        private void rectangle2_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (BuzzingWarningConfigCallback != null)
            {
                BuzzingWarningConfigCallback.Invoke();
            }
        }

        private void TopGrid_OnMouseEnter(object sender, MouseEventArgs e)
        {
            TopGrid.ToolTip = $"Red: {DeviceData.RedLightEvent}\r\n" +
                              $"Yellow: {DeviceData.YellowLightEvent}\r\n" +
                              $"Green: {DeviceData.GreenLightEvent}\r\n" +
                              $"Buzzer: {DeviceData.BuzzerEvent}";
        }
    }
}
