using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using Telerik.WinControls;
using System.Xml.Serialization;
using System.Xml;
using Telerik.WinControls.Themes.Design;
using Telerik.WinControls.UI;
using Telerik.WinControls.RadControlSpy;

namespace Telerik.QuickStart.WinControls
{
	public partial class DescriptionPanel : UserControl
	{
        Image headerImage;
        private string exampleTitle;

        [DefaultValue("")]
        public string ExampleTitle
        {
            get { return exampleTitle; }
            set { exampleTitle = value; }
        }


		public string InnerHtml
		{
			set
			{
                this.webBrowser1.Document.Body.InnerHtml = "<div style='font-size:20px;color:#b94600;margin-bottom:13px;'>"+this.ExampleTitle+"</div>" + value;
			}
		}

		public string CssStyle
		{
			set
			{
				this.webBrowser1.Document.Body.Style = value;
			}
		}

        string GetBackgroundImage()
        {
            string path = Path.Combine( Application.StartupPath , @"..\Images\DescriptionBackground.png");
            if( File.Exists( path ) )
                return path;
            return string.Empty;
        }

		public DescriptionPanel()
		{
			InitializeComponent();

			Assembly assembly = Assembly.GetAssembly(this.GetType());

			string headerImageResource = "Telerik.QuickStart.WinControls.Resources.Images.description.bmp";
			Stream stream = assembly.GetManifestResourceStream(headerImageResource);
			if (stream != null)
			{
				headerImage = new Bitmap(stream);
			}

			webBrowser1.Navigate("about:blank");
			webBrowser1.Document.Write("<body></body>");

			string descriptionResource = "Telerik.QuickStart.WinControls.Resources.Description.htm";
			stream = assembly.GetManifestResourceStream(descriptionResource);
			if (stream != null)
			{
				StreamReader streamReader = new StreamReader(stream);
				string html = streamReader.ReadToEnd();                
                //CssStyle = "font-family: Arial; font-size: 9pt; text-align: justify; background-color: #036EC3";// #EFFAFA";                
                //CssStyle = "font-family: Arial; font-size: 9pt; text-align: justify; background-image: url(\"" + GetBackgroundImage() + "\");";//background-repeat: repeat-x";
                CssStyle = "margin:0;line-height: 19px; padding:30px 115px 0 30px;font-size: 13px; font-family: Arial;color:#017db1;background: #d1fcff url('" + GetBackgroundImage() + "') no-repeat; background-attachment:fixed;height:162px;overflow-y:auto;";
				InnerHtml =  html;
			}
			
		}

		Size originalButton1Size = Size.Empty;
		Size originalButton2Size = Size.Empty;
		Size originalButton3Size = Size.Empty;
		
		

		protected override void OnResize(EventArgs e)
		{
			//CancelAnimations();

			//this.buttonSizesCached = false;
			base.OnResize(e);
		}

        //private void CancelAnimations()
        //{
        //    RadControl targetControl = this.radButton1;
        //    if (targetControl.Tag != null)
        //    {
        //        AnimatedPropertySetting[] animations = (AnimatedPropertySetting[])targetControl.Tag;
        //        animations[0].Cancel(targetControl.RootElement, false);
        //        animations[0].Cancel(targetControl.RootElement, false);
        //    }

        //    targetControl = this.radButton2;
        //    if (targetControl.Tag != null)
        //    {
        //        AnimatedPropertySetting[] animations = (AnimatedPropertySetting[])targetControl.Tag;
        //        animations[0].Cancel(targetControl.RootElement, false);
        //        animations[0].Cancel(targetControl.RootElement, false);
        //    }

        //    targetControl = this.radButton3;
        //    if (targetControl.Tag != null)
        //    {
        //        AnimatedPropertySetting[] animations = (AnimatedPropertySetting[])targetControl.Tag;
        //        animations[0].Cancel(targetControl.RootElement, false);
        //        animations[0].Cancel(targetControl.RootElement, false);
        //    }
        //}

        //public void EnableButtons(QSFButtons enabled, bool animate)
        //{
        //    bool enableLoadTheme = (enabled & QSFButtons.LoadTheme) == QSFButtons.LoadTheme;
        //    bool enableStyleBuilder = (enabled & QSFButtons.StyleBuilder) == QSFButtons.StyleBuilder;
        //    bool enableControlSpy = (enabled & QSFButtons.ControlSpy) == QSFButtons.ControlSpy;

        //    this.radButton1.Enabled = enableLoadTheme;
        //    this.radButton2.Enabled = enableStyleBuilder;
        //    this.radButton3.Enabled = enableControlSpy;

        //    if (!buttonSizesCached)
        //    {
        //        originalButton1Size = this.radButton1.Size;
        //        originalButton2Size = this.radButton2.Size;
        //        originalButton3Size = this.radButton3.Size;

        //        buttonSizesCached = true;
        //    }

        //    if (enabled != QSFButtons.None && animate)
        //    {
        //        if (enableLoadTheme)
        //            this.AnimateBubble(this.radButton1, 1200, -30);
        //        if (enableStyleBuilder)
        //            this.AnimateBubble(this.radButton2, 1500, 0);
        //        if (enableControlSpy)
        //            this.AnimateBubble(this.radButton3, 1800, 30);
        //    }
        //    else
        //    {
        //        CancelAnimations();
        //    }
        //}

		private void AnimateBubble(RadControl targetControl, int delay, int horizOffset)
		{
			if (targetControl.Tag != null)
			{
				AnimatedPropertySetting[] animations = (AnimatedPropertySetting[])targetControl.Tag;
				animations[0].Cancel(targetControl.RootElement, false);
				animations[0].Cancel(targetControl.RootElement, false);
			}

			float scaleF = 1.7f;

			Point endLocation = targetControl.Location;
			Point startLocation =
				new Point(endLocation.X - (int)((targetControl.Size.Width * scaleF - targetControl.Size.Width) / 2) + horizOffset,
						  endLocation.Y - (int)((targetControl.Size.Height * scaleF - targetControl.Size.Height) / 2));
			
			Size startSize =
				new Size((int)(targetControl.Size.Width * scaleF),
						 (int)(targetControl.Size.Height * scaleF));

			AnimatedPropertySetting setting1 = new AnimatedPropertySetting(
				RootRadElement.ControlBoundsProperty, //target property
				new Rectangle(startLocation, startSize), //initial value
				new Rectangle(endLocation, targetControl.Size), //end value
				40, //interval
				6 // number of repetitions
				);

			//setting1.ResetValueAfterRemove = true;
			setting1.ApplyDelay = delay;
			setting1.ApplyEasingType = RadEasingType.OutBounce;
			setting1.RemoveAfterApply = true;
			setting1.SkipToEndValueOnReplace = true;
			setting1.ApplyValue(targetControl.RootElement);
			
			AnimatedPropertySetting setting2 = new AnimatedPropertySetting(
				VisualElement.OpacityProperty,
				0.2d, //initial value
				1d, //end value
				40, //interval
				6 //number of repetitions
				);

			//setting2.ResetValueAfterRemove = true;
			setting2.ApplyDelay = delay;
			setting2.ApplyEasingType = RadEasingType.OutBounce;
			setting2.RemoveAfterApply = true;
			setting2.SkipToEndValueOnReplace = true;
			setting2.ApplyValue(targetControl.RootElement);

			targetControl.Tag = new AnimatedPropertySetting[] { setting1, setting2 };
		}

        //protected override void OnPaint(PaintEventArgs e)
        //{
        //    base.OnPaint(e);

        //    for (int i = 0; i <= this.Right; i += this.headerImage.Width)
        //    {
        //        e.Graphics.DrawImage(this.headerImage, i, 0);
        //    }
        //}

		
	}
}
