using System;
using System.Drawing;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using MediaColor = System.Windows.Media.Color;
using Point2D = System.Windows.Point;

namespace AvengersUtd.Test
{
    class Cover : ModelVisual3D
    {
        private readonly Model3DGroup modelGroup;
        private readonly AxisAngleRotation3D rotation;
        private readonly TranslateTransform3D translation;
        private readonly int pos;
        
        private static ImageSource LoadImageSource(string imagePath)
        {
            Image thumb = Image.FromFile(imagePath);
            return new BitmapImage(new Uri(imagePath, UriKind.RelativeOrAbsolute));
        }

        private static Material LoadImage(ImageSource imSrc)
        {
            return new DiffuseMaterial(new ImageBrush(imSrc));
        }

        private static Material LoadImageMirror(ImageSource imSrc)
        {
            System.Windows.Controls.Image image = new System.Windows.Controls.Image { Source = imSrc };
            MediaColor startColor = MediaColor.FromArgb(127, 255, 255, 255);
            MediaColor endColor = MediaColor.FromArgb(127, 255, 255, 255);
            image.OpacityMask = new LinearGradientBrush(startColor, endColor, 90.0);
            VisualBrush brush = new VisualBrush(image);
            return new DiffuseMaterial(brush);
        }


        private double RotationAngle(int index)
        {
            return Math.Sign(pos - index) * -90;
        }
        private double TranslationX(int index)
        {
            return pos * .4 + Math.Sign(pos - index) * 1.6;
        }
        private double TranslationZ(int index)
        {
            return pos == index ? 1 : 0;
        }

        public Cover(string imagePath, int pos)
        {
            this.pos = pos;
            rotation = new AxisAngleRotation3D(new Vector3D(0, 1, 0), RotationAngle(0));
            translation = new TranslateTransform3D(TranslationX(0), 0, TranslationZ(0));
            ImageSource imSrc = LoadImageSource(imagePath); 

            modelGroup = new Model3DGroup();
            modelGroup.Children.Add(new GeometryModel3D(MathHelper.Tessellate(), LoadImage(imSrc)));
            modelGroup.Children.Add(new GeometryModel3D(MathHelper.TessellateMirror(), LoadImageMirror(imSrc)));

            Transform3DGroup transformGroup = new Transform3DGroup();
            transformGroup.Children.Add(new RotateTransform3D(rotation));
            transformGroup.Children.Add(translation);
            modelGroup.Transform = transformGroup;
            
            Content = modelGroup;
        }
        public void Animate(int index)
        {
            // *** OLD
            //rotation.Angle = RotationAngle(index);
            //translation.OffsetX = TranslationX(index);
            //translation.OffsetZ = TranslationZ(index);

            TimeSpan duration = TimeSpan.FromMilliseconds(500);
            DoubleAnimation rotateAnimation = new DoubleAnimation(RotationAngle(index), duration);
            DoubleAnimation xAnimation = new DoubleAnimation(TranslationX(index), duration);
            DoubleAnimation zAnimation = new DoubleAnimation(TranslationZ(index), duration);
            rotation.BeginAnimation(AxisAngleRotation3D.AngleProperty, rotateAnimation);
            translation.BeginAnimation(TranslateTransform3D.OffsetXProperty, xAnimation);
            translation.BeginAnimation(TranslateTransform3D.OffsetZProperty, zAnimation);
        }
    }
}
