using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.RadControlSpy;
using System.Runtime.InteropServices;
using Telerik.WinControls.UI.Docking;

namespace Telerik.QuickStart.WinControls
{
  
	public partial class ExamplesForm: UserControl
	{
        public ExamplesForm()
		{
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

            this.EnabledQSFButtons = QSFButtons.TransparentBackground | QSFButtons.ChangeTheme | QSFButtons.StyleBuilder | QSFButtons.ControlSpy;

			InitializeComponent();

            this.ForeColor = Color.Black;
            this.settingsPanel.ForeColor = Control.DefaultForeColor;

			this.Dock = DockStyle.Fill;
			this.AutoScroll = true;
            this.settingsPanel.AutoScroll = true;

            if (this.DesignMode)
            {
                this.settingsPanel.BackColor = Color.Gray;
            }

            RoundRectShape shape = new RoundRectShape(6);
		    shape.TopLeftRounded = false;
            shape.BottomLeftRounded = false;

		    this.settingsPanel.PanelElement.Shape = shape;
            this.AutoScaleMode = AutoScaleMode.None;

            this.settingsPanel.BackColor = Color.Transparent;
            this.settingsPanel.Scroll += new ScrollEventHandler(settingsPanel_Scroll);
            this.settingsPanel.ThemeClassName = "QsfPanel";

            this.settingsPanel.Layout += new LayoutEventHandler(settingsPanel_Layout);
		}

        void settingsPanel_Layout(object sender, LayoutEventArgs e)
        {
            const int margin = 10;

            foreach (RadControl control in settingsPanel.Controls)
            {
                control.Left = margin;

                control.Width = this.settingsPanel.ClientRectangle.Width - 2 * margin;
                control.Anchor = System.Windows.Forms.AnchorStyles.Top;
            }

            this.settingsPanel.Invalidate();
        }

        void settingsPanel_Scroll(object sender, ScrollEventArgs e)
        {
            this.settingsPanel.Invalidate();
        }

        protected override void OnLoad(EventArgs e)
        {
            ThemeResolutionService.ApplyThemeToControlTree(this.settingsPanel, MainForm.DefaultTheme);
            ThemeResolutionService.ApplyThemeToControlTree(this, this.GetExampleDefaultTheme());
            base.OnLoad(e);
        }

        protected virtual string GetExampleDefaultTheme()
        {
            return MainForm.DefaultTheme;
        }

        protected override void OnParentChanged(EventArgs e)
        {
            base.OnParentChanged(e);
            
            if (this.DesignMode)
            {
                return;
            }

            if (this.Parent == null)
            {
                this.Controls.Add(this.settingsPanel);
            }
            else
            {
                this.BackColor = Color.Transparent;
                //TODO: 11.Feb.09: added hack to reparent settingsPanel

                ((SplitContainer)this.Parent.Parent.Parent).Panel2.Controls.Add(this.settingsPanel);
                Size parentSize = this.settingsPanel.Parent.Size;                

                this.settingsPanel.Width = MainForm.settingsPanelWidth;
                this.settingsPanel.Dock = DockStyle.Right; //Bounds = new Rectangle(0, 15, parentSize.Width - 1, parentSize.Height - 30);
                this.settingsPanel.Visible = true;

                //offset cotnrols to make room for Spy button

                bool offset = false;

                foreach (RadControl control in settingsPanel.Controls)
                {
                    if (control.Top < 30)
                    {
                        offset = true;
                        break;
                    }
                }

                foreach (RadControl control in settingsPanel.Controls)
                {
                    if (offset && control != this.btnSpy)
                    {
                        control.Top += 35;
                    }
                }
            }
        }

		public RadControl SelectedControl;
        public QSFButtons EnabledQSFButtons;

        private void ExamplesForm_Layout(object sender, LayoutEventArgs e)
        {
            const int settingsPanelWidth = MainForm.settingsPanelWidth;
            //TODO: hide panel. it should be reparented in qfs later
            if (this.DesignMode)
            {
                this.settingsPanel.Visible = true;
            
                this.settingsPanel.Bounds = new Rectangle(
                    this.Size.Width - settingsPanelWidth - 1,
                    1,
                    settingsPanelWidth,
                    this.Size.Height - 2
                    );
            }
        }

	    private RadControlSpyForm mySpyForm;

        private void btnSpy_Click(object sender, EventArgs e)
        {
            if (mySpyForm == null || mySpyForm.Disposing)
            {
                mySpyForm = new RadControlSpyForm();
                mySpyForm.Disposed += delegate { this.mySpyForm = null; };
            }

            mySpyForm.Show();
            mySpyForm.BringToFront();
        }

        /// <summary>
        /// Notifies the example that it is not currently visible. This may happen when navigating to a root QSF node.
        /// </summary>
        public void Unload()
        {
            //TODO: This should be moved to the dock examples
            RadDock dock = ControlHelper.FindDescendant<RadDock>(this);
            if (dock == null)
            {
                return;
            }

            foreach (DockWindow window in dock.DockWindows)
            {
                if (window.DockState == DockState.Floating)
                {
                    window.AllowedDockState |= AllowedDockState.Hidden;
                    window.DockState = DockState.Hidden;
                }
            }
        }

	    protected void SetEnabledQsfbButton(QSFButtons value, bool enabled)
	    {
            this.EnabledQSFButtons = enabled ? (this.EnabledQSFButtons | value) : (this.EnabledQSFButtons & ~value);
	    }
	}
}
