using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using Telerik.WinControls;
using Telerik.WinControls.Primitives;
using Telerik.WinControls.RadControlSpy;

namespace Telerik.WinControls.UI
{
    /// <summary>
    /// Custom extension of RadRotator
    /// Functionality to load from file is added
    /// </summary>
    public class RadQSFRotator : RadRotator
    {
        /// <summary>
        /// Stop rotator when the control is hidden and Start it when shown.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnVisibleChanged(EventArgs e)
        {
            base.OnVisibleChanged(e);

            if (this.Visible)
            {
                this.Start();
            }
            else
            {
                this.Stop();
            }
        }

        /// <summary>
        /// Load the <see cref="RadRotator.Items"/> from <see cref="XmlNode"/>
        /// </summary>
        /// <param name="reader">the XmlNode to load</param>
        /// <returns>true on success, false otherwise.</returns>
        public bool LoadFromXML(XmlNode reader)
        {
            if (reader == null)
                return false;

            XmlNodeList rotatorItems = reader.SelectNodes("Item");

            for (int i = 0; i < rotatorItems.Count; i++)
            {
                RadQSFRotatorFrame item = new RadQSFRotatorFrame();
                if (item.LoadXML(rotatorItems[i]))
                    this.Items.Add(item);
            }

            return true;
        }

        /// <summary>
        /// Load the <see cref="RadRotator.Items"/> from file
        /// </summary>
        /// <param name="fileName">the file to read from</param>
        /// <returns>true on success, false otherwise.</returns>
        public bool LoadFromXML(string fileName)
        {
            FileInfo file = new FileInfo(fileName);

            if (!file.Exists)
                return false;

            XmlDocument reader = new XmlDocument();
            reader.Load(file.FullName);

            XmlNode rotatorItems = reader.SelectSingleNode("//RotatorItems");

            return rotatorItems != null ? LoadFromXML(rotatorItems) : false;
        }


        protected override void CreateChildItems(RadElement parent)
        {
            base.CreateChildItems(parent);
            this.LoadFromXML(@"..\rotator.xml");
        }

    }
}
