using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;
using Telerik.WinControls.Primitives;
using System.Drawing.Imaging;
using Telerik.QuickStart.WinControls.Properties;

namespace Telerik.QuickStart.WinControls
{
    public partial class WelcomeScreenTop : UserControl
    {
        public WelcomeScreenTop()
        {
            InitializeComponent();

            this.titleLabel.Text = "Q1 2010 SP2";
            this.PrepareProductLineButtons();
        }

        private int intervalBetweenFrames = 33;
        private int numberFrames = 20;
        private int initialDelayMs = 1000;
        private int initialDelayBulletsMs = 500;

        private void PrepareProductLineButtons()
        {
            this.productLineButtonsHolder.ThemeClassName = "";
            this.wpfButton.ThemeClassName = "";
            this.openAccessButton.ThemeClassName = "";
            this.silverLightButton.ThemeClassName = "";
            this.webUiTestButton.ThemeClassName = "";
            this.aspButton.ThemeClassName = "";
            this.reportingButton.ThemeClassName = "";

            this.productLineButtonsHolder.BackColor = Color.Black;
            this.productLineButtonsHolder.PanelElement.Children[1].Visibility = ElementVisibility.Collapsed;
            this.productLineButtonsHolder.PanelElement.Children[2].Visibility = ElementVisibility.Collapsed;
            (this.productLineButtonsHolder.PanelElement.Children[0] as FillPrimitive).GradientStyle = GradientStyles.Solid;
            (this.productLineButtonsHolder.PanelElement.Children[0] as FillPrimitive).Opacity = 0.8F;
            (this.productLineButtonsHolder.PanelElement.Children[0] as FillPrimitive).BackColor = Color.Black;
            (this.productLineButtonsHolder.PanelElement.Children[0] as FillPrimitive).Margin = new Padding(-1);


            this.reportingButton.ButtonElement.Children[0].Visibility = ElementVisibility.Collapsed;
            this.reportingButton.ButtonElement.Children[1].Children[1].Visibility = ElementVisibility.Collapsed;
            this.reportingButton.ButtonElement.Children[2].Visibility = ElementVisibility.Collapsed;
            this.reportingButton.ButtonElement.Opacity = 0.4;
            this.reportingButton.Cursor = Cursors.Hand;
            this.reportingButton.Tag = @"http://demos.telerik.com/reporting";
            this.reportingButton.Click += new EventHandler(OnProductLineBtn_Click);
            this.reportingButton.MouseHover += new EventHandler(OnProductLineBtn_MouseHover);
            this.reportingButton.MouseLeave += new EventHandler(OnProductLineBtn_MouseLeave);

            this.openAccessButton.ButtonElement.Children[0].Visibility = ElementVisibility.Collapsed;
            this.openAccessButton.ButtonElement.Children[1].Children[1].Visibility = ElementVisibility.Collapsed;
            this.openAccessButton.ButtonElement.Children[2].Visibility = ElementVisibility.Collapsed;
            this.openAccessButton.ButtonElement.Opacity = 0.4;
            this.openAccessButton.Cursor = Cursors.Hand;
            this.openAccessButton.Tag = @"http://demos.telerik.com/orm";
            this.openAccessButton.Click += new EventHandler(OnProductLineBtn_Click);
            this.openAccessButton.MouseHover += new EventHandler(OnProductLineBtn_MouseHover);
            this.openAccessButton.MouseLeave += new EventHandler(OnProductLineBtn_MouseLeave);

            this.aspButton.ButtonElement.Children[0].Visibility = ElementVisibility.Collapsed;
            this.aspButton.ButtonElement.Children[1].Children[1].Visibility = ElementVisibility.Collapsed;
            this.aspButton.ButtonElement.Children[2].Visibility = ElementVisibility.Collapsed;
            this.aspButton.ButtonElement.Opacity = 0.4;
            this.aspButton.Cursor = Cursors.Hand;
            this.aspButton.Tag = @"http://demos.telerik.com/aspnet-ajax/";
            this.aspButton.Click += new EventHandler(OnProductLineBtn_Click);
            this.aspButton.MouseHover += new EventHandler(OnProductLineBtn_MouseHover);
            this.aspButton.MouseLeave += new EventHandler(OnProductLineBtn_MouseLeave);

            this.webUiTestButton.ButtonElement.Children[0].Visibility = ElementVisibility.Collapsed;
            this.webUiTestButton.ButtonElement.Children[1].Children[1].Visibility = ElementVisibility.Collapsed;
            this.webUiTestButton.ButtonElement.Children[2].Visibility = ElementVisibility.Collapsed;
            this.webUiTestButton.ButtonElement.Opacity = 0.4;
            this.webUiTestButton.Cursor = Cursors.Hand;
            this.webUiTestButton.Tag = @"http://www.telerik.com/products/web-ui-test-studio.aspx";
            this.webUiTestButton.Click += new EventHandler(OnProductLineBtn_Click);
            this.webUiTestButton.MouseHover += new EventHandler(OnProductLineBtn_MouseHover);
            this.webUiTestButton.MouseLeave += new EventHandler(OnProductLineBtn_MouseLeave);

            this.silverLightButton.ButtonElement.Children[0].Visibility = ElementVisibility.Collapsed;
            this.silverLightButton.ButtonElement.Children[1].Children[1].Visibility = ElementVisibility.Collapsed;
            this.silverLightButton.ButtonElement.Children[2].Visibility = ElementVisibility.Collapsed;
            this.silverLightButton.ButtonElement.Opacity = 0.4;
            this.silverLightButton.Cursor = Cursors.Hand;
            this.silverLightButton.Tag = @"http://demos.telerik.com/silverlight";
            this.silverLightButton.Click += new EventHandler(OnProductLineBtn_Click);
            this.silverLightButton.MouseHover += new EventHandler(OnProductLineBtn_MouseHover);
            this.silverLightButton.MouseLeave += new EventHandler(OnProductLineBtn_MouseLeave);

            this.wpfButton.ButtonElement.Children[0].Visibility = ElementVisibility.Collapsed;
            this.wpfButton.ButtonElement.Children[1].Children[1].Visibility = ElementVisibility.Collapsed;
            this.wpfButton.ButtonElement.Children[2].Visibility = ElementVisibility.Collapsed;
            this.wpfButton.ButtonElement.Opacity = 0.4;
            this.wpfButton.Cursor = Cursors.Hand;
            this.wpfButton.Tag = @"http://demos.telerik.com/wpf";
            this.wpfButton.Click += new EventHandler(OnProductLineBtn_Click);
            this.wpfButton.MouseHover += new EventHandler(OnProductLineBtn_MouseHover);
            this.wpfButton.MouseLeave += new EventHandler(OnProductLineBtn_MouseLeave);
        }

        private void OnProductLineBtn_Click(object sender, EventArgs e)
        {
            RadButton button = sender as RadButton;

            if (button != null)
            {
                System.Diagnostics.Process.Start(button.Tag.ToString());
            }
        }

        private void OnProductLineBtn_MouseHover(object sender, EventArgs e)
        {
            RadButton button = sender as RadButton;

            if (button != null)
            {
                AnimatedPropertySetting animation = new AnimatedPropertySetting();
                animation.ApplyEasingType = RadEasingType.OutCubic;
                animation.ApplyDelay = 0;
                animation.StartValue = (double)button.ButtonElement.Opacity;
                animation.EndValue = (double)1.0;
                animation.RemoveAfterApply = false;
                animation.Property = RadItem.OpacityProperty;
                animation.ApplyValue(button.ButtonElement);
            }
        }

        private void OnProductLineBtn_MouseLeave(object sender, EventArgs e)
        {
            RadButton button = sender as RadButton;

            if (button != null)
            {
                AnimatedPropertySetting animation = new AnimatedPropertySetting();
                animation.ApplyEasingType = RadEasingType.InCubic;
                animation.ApplyDelay = 0;
                animation.StartValue = (double)1.0;
                animation.EndValue = (double)0.4;
                animation.RemoveAfterApply = false;
                animation.Property = RadItem.OpacityProperty;
                animation.ApplyValue(button.ButtonElement);
            }
        }



        private RadLabelElement CloneLabelElement(RadLabel labelControl)
        {
            RadLabelElement res = new RadLabelElement();

            res.AutoSize = labelControl.AutoSize;
            res.Size = labelControl.Size;

            res.Image = labelControl.Image;
            res.Text = labelControl.Text;
            res.TextImageRelation = labelControl.TextImageRelation;

            res.TextAlignment = labelControl.TextAlignment;
            res.ImageAlignment = labelControl.TextAlignment;
            res.Font = labelControl.Font;
            res.ForeColor = labelControl.ForeColor;

            res.Children[2].Children[0].Margin =
                labelControl.LabelElement.Children[2].Children[0].Margin;

            return res;
        }

        private List<RadLabelElement> labelList = new List<RadLabelElement>();

        private void WelcomeScreen_Load(object sender, EventArgs e)
        {
            this.BackColor = Color.Transparent;
            this.topPanel.BackColor = Color.Transparent;

            for (int i = bulletsContainer.Controls.Count - 1; i >= 0; i--)
            {
                Control control = bulletsContainer.Controls[i];
                RadLabel label = control as RadLabel;
                if (label != null)
                {
                    RadLabelElement labelClone = this.CloneLabelElement(label);
                    this.bulletsContainer.PanelElement.Children.Add(labelClone);

                    labelClone.Location = label.Location;
                    labelList.Add(labelClone);
                }

                bulletsContainer.Controls.RemoveAt(i);
                control.Dispose();
            }

            bulletsContainer.Controls.Clear();

            AnimateFlyIn();
        }

        private bool animationStarted;
        private void SetProductLineButtonsInitialAnimationState()
        {
            this.productLineButtonsHolder.Visible = false;

            this.aspButton.ButtonElement.ResetValue(RadItem.PositionOffsetProperty, ValueResetFlags.Local);
            this.aspButton.ButtonElement.PositionOffset = new SizeF(0, -(this.aspButton.Height + this.aspButton.Location.Y));
            this.aspButton.Visible = false;

            this.silverLightButton.ButtonElement.ResetValue(RadItem.PositionOffsetProperty, ValueResetFlags.Local);
            this.silverLightButton.ButtonElement.PositionOffset = new SizeF(0, -(this.silverLightButton.Height + this.silverLightButton.Location.Y));
            this.silverLightButton.Visible = false;

            this.openAccessButton.ButtonElement.ResetValue(RadItem.PositionOffsetProperty, ValueResetFlags.Local);
            this.openAccessButton.ButtonElement.PositionOffset = new SizeF(0, -(this.openAccessButton.Height + this.openAccessButton.Location.Y));
            this.openAccessButton.Visible = false;

            this.wpfButton.ButtonElement.ResetValue(RadItem.PositionOffsetProperty, ValueResetFlags.Local);
            this.wpfButton.ButtonElement.PositionOffset = new SizeF(0, -(this.wpfButton.Height + this.wpfButton.Location.Y));
            this.wpfButton.Visible = false;

            this.reportingButton.ButtonElement.ResetValue(RadItem.PositionOffsetProperty, ValueResetFlags.Local);
            this.reportingButton.ButtonElement.PositionOffset = new SizeF(0, -(this.reportingButton.Height + this.reportingButton.Location.Y));
            this.reportingButton.Visible = false;

            this.webUiTestButton.ButtonElement.ResetValue(RadItem.PositionOffsetProperty, ValueResetFlags.Local);
            this.webUiTestButton.ButtonElement.PositionOffset = new SizeF(0, -(this.webUiTestButton.Height + this.webUiTestButton.Location.Y));
            this.webUiTestButton.Visible = false;
        }

        private void AnimateProductLineButtons()
        {
            ImageAttributes attr = new ImageAttributes();
            
            Image backgroundImage = MainForm.ImageFromResource("Telerik.QuickStart.WinControls.Resources.Images.WelcomeMainBack.png");
            if (backgroundImage != null && this.productLineButtonsHolder.BackgroundImage == null)
            {
                
                Bitmap productLineHolderBackground = new Bitmap(this.productLineButtonsHolder.Width, this.productLineButtonsHolder.Height);
                
                Graphics bitmapGraphics = Graphics.FromImage(productLineHolderBackground);
                bitmapGraphics.Clear((backgroundImage as Bitmap).GetPixel(backgroundImage.Width - 1, 0));
                
                Point screenPoint = this.productLineButtonsHolder.PointToScreen(Point.Empty);
                Form parentForm = this.FindForm();
                screenPoint = parentForm.PointToClient(screenPoint);
                bitmapGraphics.DrawImage(backgroundImage, new Rectangle(0, 0, this.productLineButtonsHolder.Width, this.productLineButtonsHolder.Height), screenPoint.X, screenPoint.Y, this.productLineButtonsHolder.Width, this.productLineButtonsHolder.Height, GraphicsUnit.Pixel);

                this.productLineButtonsHolder.BackgroundImage = productLineHolderBackground;
                bitmapGraphics.Dispose();

            }
            this.productLineButtonsHolder.Visible = true;

            AnimatedPropertySetting aspAnimation = new AnimatedPropertySetting();
            aspAnimation.Property = RadItem.PositionOffsetProperty;
            aspAnimation.StartValue = new SizeF(0, this.aspButton.ButtonElement.PositionOffset.Height);
            aspAnimation.EndValue = new SizeF(0, 0);
            aspAnimation.ApplyEasingType = RadEasingType.OutExponential;
            aspAnimation.ApplyDelay = 150;
            this.aspButton.Visible = true;
            aspAnimation.ApplyValue(this.aspButton.ButtonElement);

            AnimatedPropertySetting silverLightAnimation = new AnimatedPropertySetting();
            silverLightAnimation.Property = RadItem.PositionOffsetProperty;
            silverLightAnimation.StartValue = new SizeF(0, this.silverLightButton.ButtonElement.PositionOffset.Height);
            silverLightAnimation.EndValue = new SizeF(0, 0);
            silverLightAnimation.ApplyEasingType = RadEasingType.OutExponential;
            silverLightAnimation.ApplyDelay = 150 + aspAnimation.ApplyDelay;
            this.silverLightButton.Visible = true;
            silverLightAnimation.ApplyValue(this.silverLightButton.ButtonElement);

            AnimatedPropertySetting openAccessAnimation = new AnimatedPropertySetting();
            openAccessAnimation.Property = RadItem.PositionOffsetProperty;
            openAccessAnimation.StartValue = new SizeF(0, this.openAccessButton.ButtonElement.PositionOffset.Height);
            openAccessAnimation.EndValue = new SizeF(0, 0);
            openAccessAnimation.ApplyEasingType = RadEasingType.OutExponential;
            openAccessAnimation.ApplyDelay = 150 + silverLightAnimation.ApplyDelay;
            this.openAccessButton.Visible = true;
            openAccessAnimation.ApplyValue(this.openAccessButton.ButtonElement);

            AnimatedPropertySetting wpfAnimation = new AnimatedPropertySetting();
            wpfAnimation.Property = RadItem.PositionOffsetProperty;
            wpfAnimation.StartValue = new SizeF(0, this.wpfButton.ButtonElement.PositionOffset.Height);
            wpfAnimation.EndValue = new SizeF(0, 0);
            wpfAnimation.ApplyEasingType = RadEasingType.OutExponential;
            wpfAnimation.ApplyDelay = 150 + openAccessAnimation.ApplyDelay;
            this.wpfButton.Visible = true;
            wpfAnimation.ApplyValue(this.wpfButton.ButtonElement);


            AnimatedPropertySetting reportingAnimation = new AnimatedPropertySetting();

            reportingAnimation.Property = RadItem.PositionOffsetProperty;
            reportingAnimation.StartValue = new SizeF(0, this.reportingButton.ButtonElement.PositionOffset.Height);
            reportingAnimation.EndValue = new SizeF(0, 0);
            reportingAnimation.ApplyEasingType = RadEasingType.OutExponential;
            reportingAnimation.ApplyDelay = 150 + wpfAnimation.ApplyDelay;
            this.reportingButton.Visible = true;
            reportingAnimation.ApplyValue(this.reportingButton.ButtonElement);

            AnimatedPropertySetting webUiAnimation = new AnimatedPropertySetting();
            webUiAnimation.Property = RadItem.PositionOffsetProperty;
            webUiAnimation.StartValue = new SizeF(0, this.webUiTestButton.ButtonElement.PositionOffset.Height);
            webUiAnimation.EndValue = new SizeF(0, 0);
            webUiAnimation.ApplyEasingType = RadEasingType.OutExponential;
            webUiAnimation.ApplyDelay = 150 + reportingAnimation.ApplyDelay;
            this.webUiTestButton.Visible = true;
            webUiAnimation.ApplyValue(this.webUiTestButton.ButtonElement);
        }

        public void AnimateFlyIn()
        {
            if (animationStarted)
            {
                return;
            }

            this.SetProductLineButtonsInitialAnimationState();

            animationStarted = true;

            AnimatedPropertySetting backgroundAnimation = new AnimatedPropertySetting(
                    VisualElement.BackColorProperty,
                    Color.FromArgb(20, 0, 0, 0),
                    Color.FromArgb(178, 0, 0, 0),
                    numberFrames,
                    intervalBetweenFrames);

            backgroundAnimation.ApplyDelay = 10;
            backgroundAnimation.ApplyValue(this.topPanel.PanelElement.Children[0]);

            AnimateImage();
            AnimateTitle();
            AnimatePurchase();

            //Bullets animation
            for (int i = 0; i < labelList.Count; i++)
            {
                RadLabelElement labelElement = labelList[i];
                AnimatedPropertySetting setting = new AnimatedPropertySetting(
                    RadElement.MarginProperty,
                    new Padding(50, 0, 0, 0),
                    new Padding(0, 0, 0, 0),
                    numberFrames,
                    intervalBetweenFrames);

                AnimatedPropertySetting setting1 = new AnimatedPropertySetting(
                    VisualElement.OpacityProperty,
                    0d,
                    1d,
                    numberFrames,
                    intervalBetweenFrames);

                int delay = 2 * initialDelayMs + 500 + i * initialDelayBulletsMs;

                setting.ApplyDelay = delay;
                setting1.ApplyDelay = delay;

                setting.ApplyValue(labelElement);
                setting1.ApplyValue(labelElement);

                //last animation
                if (i == labelList.Count - 1)
                {
                    setting1.AnimationFinished +=
                        delegate
                            {
                                animationStarted = false;
                                this.AnimateProductLineButtons();
                            };

                }
            }
        }

        private void AnimateTitle()
        {
            //Title animation
            AnimatedPropertySetting animationSetting = new AnimatedPropertySetting(
                RadElement.MarginProperty,
                new Padding(0, -100, 0, 0),
                new Padding(0, 0, 0, 0),
                numberFrames,
                intervalBetweenFrames);

            AnimatedPropertySetting animationSetting1 = new AnimatedPropertySetting(
                VisualElement.OpacityProperty,
                0d,
                1d,
                numberFrames,
                intervalBetweenFrames);

            AnimatedPropertySetting animationSetting2 = new AnimatedPropertySetting(
                VisualElement.ScaleTransformProperty,
                new SizeF(0.3f , 0.3f),
                new SizeF(1, 1),
                numberFrames,
                intervalBetweenFrames);

            animationSetting.ApplyDelay = initialDelayMs + 250;
            animationSetting1.ApplyDelay = initialDelayMs + 250;
            animationSetting2.ApplyDelay = initialDelayMs + 250;

            animationSetting.ApplyValue(this.titleLabel.LabelElement);
            animationSetting1.ApplyValue(this.titleLabel.LabelElement);
            animationSetting2.ApplyValue(this.titleLabel.LabelElement);
            
        }

        private void AnimateImage()
        {
            //Title animation
            AnimatedPropertySetting animationSetting = new AnimatedPropertySetting(
                RadElement.MarginProperty,
                new Padding(-100, 0, 0, 0),
                new Padding(0, 0, 0, 0),
                numberFrames,
                intervalBetweenFrames);

            AnimatedPropertySetting animationSetting1 = new AnimatedPropertySetting(
                VisualElement.OpacityProperty,
                0d,
                1d,
                numberFrames,
                intervalBetweenFrames);

            animationSetting.ApplyDelay = initialDelayMs;
            animationSetting1.ApplyDelay = initialDelayMs;
            animationSetting.ApplyValue(this.imageLabel.LabelElement);
            animationSetting1.ApplyValue(this.imageLabel.LabelElement);
        }

        private void AnimatePurchase()
        {
            //Title animation
            AnimatedPropertySetting animationSetting = new AnimatedPropertySetting(
                RadElement.MarginProperty,
                new Padding(0, -100, 0, 0),
                new Padding(0, 0, 0, 0),
                numberFrames,
                intervalBetweenFrames);

            AnimatedPropertySetting animationSetting1 = new AnimatedPropertySetting(
                VisualElement.OpacityProperty,
                0d,
                1d,
                numberFrames,
                intervalBetweenFrames);

            AnimatedPropertySetting animationSetting2 = new AnimatedPropertySetting(
                VisualElement.ScaleTransformProperty,
                new SizeF(0.3f, 0.3f),
                new SizeF(1, 1),
                numberFrames,
                intervalBetweenFrames);

            animationSetting.ApplyDelay = initialDelayMs + 5000;
            animationSetting1.ApplyDelay = initialDelayMs + 5000;
            animationSetting2.ApplyDelay = initialDelayMs + 5000;

            animationSetting.ApplyValue(this.btnPurchase.ButtonElement);
            animationSetting1.ApplyValue(this.btnPurchase.ButtonElement);
            animationSetting2.ApplyValue(this.btnPurchase.ButtonElement);

        }


        private void WelcomeScreen_DoubleClick(object sender, EventArgs e)
        {
            this.AnimateFlyIn();
        }

        private void radMenuItem1_Click(object sender, EventArgs e)
        {
            this.AnimateFlyIn();
        }

        private void topPanel_Paint(object sender, PaintEventArgs e)
        {

        }

        private void btnPurchase_Click(object sender, EventArgs e)
        {
            Process.Start("http://www.telerik.com/purchase");
        }

        private void radLabel4_Click(object sender, EventArgs e)
        {

        }
    }
}
