using System;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
using Telerik.WinControls;
using Telerik.WinControls.RadControlSpy;
using Telerik.WinControls.Themes.Design;
using Telerik.WinControls.UI;

namespace Telerik.QuickStart.WinControls
{
    public partial class ProductivityToolsPanel : UserControl
    {
        RadControlSpyForm spyForm;

        public ProductivityToolsPanel()
        {
            InitializeComponent();

            this.radButton1.Click += new EventHandler(radButton1_Click);
            this.radButton2.Click += new EventHandler(radButton2_Click);
            this.radButton3.Click += new EventHandler(radButton3_Click);
        }

        private void radButton1_Click(object sender, EventArgs e)
        {
            MainForm form = (MainForm)this.FindForm();
            if (form.Example != null)
            {
                try
                {
                    OpenFileDialog dialog = new OpenFileDialog();

                    dialog.Filter = "Xml Files(*.xml)|*.xml|All Files(*.*)|*.*";
                    dialog.Title = "Load Theme";
                    XmlSerializer ser = new XmlSerializer(typeof(XmlTheme));
                    if (dialog.ShowDialog() == DialogResult.OK)
                    {
                        using (XmlReader reader = XmlReader.Create(dialog.FileName))
                        {
                            XmlTheme xmlTheme = (XmlTheme)ser.Deserialize(reader);
                            Theme.Deserialize(xmlTheme);
                        }

                        //form.UpdateThemes();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }

        private void radButton2_Click(object sender, EventArgs e)
        {
            MainForm form = (MainForm)this.FindForm();
            if (form.Example != null)
            {
                RadControl selectedControl = form.Example.SelectedControl;
                if (selectedControl == null)
                {
                    selectedControl = MainForm.FindRadControl(form.Example);
                }

                if (selectedControl != null)
                {
                    IThemeBuilder builder = ThemeDesignerService.CreateDesignerInstance();
                    builder.InitializeThemeDesignerForControl(selectedControl);
                    builder.ShowDialog();
                }
            }
        }

        private void radButton3_Click(object sender, EventArgs e)
        {
            MainForm form = (MainForm)this.FindForm();
            if (form.Example != null)
            {
                if (spyForm == null || spyForm.IsDisposed)
                {
                    spyForm = new RadControlSpyForm();
                    spyForm.ThemeName = "RadTitleBarExamples";
                }

                spyForm.Show();
                spyForm.Activate();
            }
        }

        public void EnableButtons(bool enableLoadTheme, bool enableStyleBuilder, bool enableControlSpy)
        {
            this.radButton1.Enabled = enableLoadTheme;
            this.radButton2.Enabled = enableStyleBuilder;
            this.radButton3.Enabled = enableControlSpy;
        }

        private void radButton1_EnabledChanged(object sender, EventArgs e)
        {
            RadButton button = ((RadButton) sender);
            if (button.Enabled)
                button.ForeColor = Color.FromArgb(132, 132, 132);
            else
                button.ForeColor = Color.FromArgb(52, 52, 52);
        }
    }
}
