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

namespace Telerik.WinControls.UI
{
    /// <summary>
    /// Custom Item used in RadQSFRotator containing Image and Text.
    /// </summary>
    public class RadQSFRotatorFrame : RadItem
    {
        private ImageAndTextLayoutPanel itlp;
        private ImagePrimitive image;
        private TextPrimitive text;

        private string executionPath = string.Empty;

        /// <summary>
        /// Gets or Sets the Application's execution path.
        /// </summary>
        public string ExecutionPath
        {
            get { return executionPath; }
            set { executionPath = value; }
        }

        /// <summary>
        /// Default constructor
        /// </summary>
        public RadQSFRotatorFrame()
        {
            this.Alignment = ContentAlignment.TopCenter;

            this.StretchVertically = true;
            this.StretchHorizontally = false;

            this.MaxSize = new Size(120, 0);

            this.NotifyParentOnMouseInput = true;
        }

        protected override void CreateChildElements()
        {
            itlp = new ImageAndTextLayoutPanel();

            itlp.UseNewLayoutSystem = true;

            image = new ImagePrimitive();
            text = new TextPrimitive();

            image.SetValue(ImageAndTextLayoutPanel.IsImagePrimitiveProperty, true);
            text.SetValue(ImageAndTextLayoutPanel.IsTextPrimitiveProperty, true);

            image.Margin = new Padding(2, 2, 2, 4);

            itlp.Children.Add(image);
            itlp.Children.Add(text);

            itlp.DisplayStyle = DisplayStyle.ImageAndText;
            itlp.TextImageRelation = TextImageRelation.ImageAboveText;
            itlp.TextAlignment = ContentAlignment.TopCenter;
            itlp.ImageAlignment = ContentAlignment.TopCenter;

            text.Font = new Font("Microsoft San Serif", 10, 0, GraphicsUnit.Point);
            text.StringAlignment = StringAlignment.Center;

            this.Children.Add(itlp);
        }

        /// <summary>
        /// Loads item's contents from XmlNode
        /// </summary>
        /// <param name="reader">the XML reader</param>
        /// <returns>true on success</returns>
        public bool LoadXML(XmlNode reader)
        {
            if (reader.Attributes["name"] == null)
                return false;

            this.Name = reader.Attributes["name"].Value;

            if (reader.Attributes["execute"] != null)
                this.executionPath = reader.Attributes["execute"].Value;

            XmlNode imageNode = reader.SelectSingleNode("image");

            if (imageNode == null || imageNode.Attributes["src"] == null)
                return false;

            string imgFile = imageNode.Attributes["src"].Value;
            
            FileInfo f = new FileInfo(Path.Combine(@"..\", imgFile));

            if (!f.Exists)
                return false;

            image.Image = new Bitmap(new FileStream(f.FullName, FileMode.Open, FileAccess.Read));

            XmlNodeList lines = reader.SelectNodes("text");

            string formatString = "{0}";
            StringBuilder strBuilder = new StringBuilder();

            for (int i = 0; i < lines.Count; i++)
            {
                strBuilder.AppendFormat(formatString, lines[i].InnerText);
                formatString = "\n{0}";
            }

            text.Text = strBuilder.ToString();

            return true;
        }
    }
}
