using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace BeWo.Controls
{
    public class ShadowChrome : Decorator
    {
        private static Brush backgroundBrush;

        private static LinearGradientBrush bottomBrush;

        private static RadialGradientBrush bottomLeftBrush;

        private static RadialGradientBrush bottomRightBrush;

        private static LinearGradientBrush rightBrush;

        private static RadialGradientBrush topRightBrush;

        static ShadowChrome()
        {
            CreateBrushes();
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            try
            {
                double shadowSize = 10;

                if (shadowSize <= 0 || this.ActualWidth < shadowSize * 2 || this.ActualHeight < shadowSize * 2)
                {
                    return;
                }

                Rect topRightRect = new Rect(this.ActualWidth, shadowSize + 10, shadowSize, shadowSize);

                drawingContext.DrawRectangle(topRightBrush, null, topRightRect);

                Rect rightRect = new Rect(this.ActualWidth, shadowSize * 2 + 10, shadowSize, this.ActualHeight - shadowSize * 2 - 10);
                drawingContext.DrawRectangle(rightBrush, null, rightRect);

                Rect bottomRightRect = new Rect(this.ActualWidth, this.ActualHeight, shadowSize, shadowSize);
                drawingContext.DrawRectangle(bottomRightBrush, null, bottomRightRect);

                Rect bottomRect = new Rect(shadowSize * 2 - 10, this.ActualHeight, this.ActualWidth - shadowSize * 2 + 10, shadowSize);
                drawingContext.DrawRectangle(bottomBrush, null, bottomRect);

                Rect bottomLeftRect = new Rect(shadowSize - 10, this.ActualHeight, shadowSize, shadowSize);
                drawingContext.DrawRectangle(bottomLeftBrush, null, bottomLeftRect);
            }
            catch
            {
            }
        }

        private static void CreateBrushes()
        {
            GradientStopCollection gradient = new GradientStopCollection(2);
            gradient.Add(new GradientStop(Colors.Black, 0));
            gradient.Add(new GradientStop(Color.FromArgb(0, 0, 0, 0), 1));

            backgroundBrush = new LinearGradientBrush(gradient, new Point(0.0, 0.0), new Point(1.0, 0.0));

            rightBrush = new LinearGradientBrush(gradient, new Point(0.0, 0.0), new Point(1.0, 0.0));
            bottomBrush = new LinearGradientBrush(gradient, new Point(0.0, 0.0), new Point(0.0, 1.0));

            bottomRightBrush = new RadialGradientBrush(gradient);
            bottomRightBrush.GradientOrigin = new Point(0.0, 0.0);
            bottomRightBrush.Center = new Point(0.0, 0.0);
            bottomRightBrush.RadiusX = 1.0;
            bottomRightBrush.RadiusY = 1.0;

            topRightBrush = new RadialGradientBrush(gradient);
            topRightBrush.GradientOrigin = new Point(0.0, 1.0);
            topRightBrush.Center = new Point(0.0, 1.0);
            topRightBrush.RadiusX = 1.0;
            topRightBrush.RadiusY = 1.0;

            bottomLeftBrush = new RadialGradientBrush(gradient);
            bottomLeftBrush.GradientOrigin = new Point(1.0, 0.0);
            bottomLeftBrush.Center = new Point(1.0, 0.0);
            bottomLeftBrush.RadiusX = 1.0;
            bottomLeftBrush.RadiusY = 1.0;
        }
    }
}