using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.Design;
using Telerik.WinControls.Layouts;
using Telerik.WinControls.Primitives;
using Telerik.WinControls.UI;

namespace Telerik.QuickStart.WinControls
{
    public class QsfSplitter: RadControl
    {
        private RadItem splitterThumb;

        
        private ArrowPrimitive splitterArrow;

        public QsfSplitter()
        {
            this.UseNewLayoutSystem = true;            
        }

        protected override void CreateChildItems(RadElement parent)
        {
            base.CreateChildItems(parent);

            FillPrimitive backgroundFill = new FillPrimitive();
            backgroundFill.Class = "SplitterFill";
            parent.Children.Add(backgroundFill);

            QsfSplitterLayout layout = new QsfSplitterLayout();
            parent.Children.Add(layout);

            splitterThumb = new RadItem();
            splitterThumb.Class = "SplitterThumb";
            layout.Children.Add(splitterThumb);

            FillPrimitive thumbFill = new FillPrimitive();
            thumbFill.Class = "SplitterThumbFill";
            splitterThumb.Children.Add(thumbFill);

            splitterArrow = new ArrowPrimitive();
            splitterArrow.Alignment = ContentAlignment.MiddleCenter;
            splitterArrow.Class = "SplitterThumbArrow";
            splitterThumb.Children.Add(splitterArrow);

            BorderPrimitive thumbBorder = new BorderPrimitive();
            thumbBorder.Class = "SplitterThumbBorder";
            splitterThumb.Children.Add(thumbBorder);

            BorderPrimitive border = new BorderPrimitive();
            border.Class = "SplitterBorder";
            parent.Children.Add(border);
        }

        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);

            this.splitterThumb.Text = this.Text;
        }

        protected override Size DefaultSize
        {
            get
            {
                return new Size(6, 200);
            }
        }

        [RadPropertyDefaultValue("ArrowDirection", typeof(ArrowPrimitive)), Category(RadDesignCategory.AppearanceCategory)]
        public Telerik.WinControls.ArrowDirection SplitterArrowDirection
        {
            get
            {
                return this.splitterArrow.Direction;
            }
            set
            {
                this.splitterArrow.Direction = value;
            }
        }
    }

    public class QsfSplitterLayout: LayoutPanel
    {
        protected override SizeF MeasureOverride(SizeF availableSize)
        {
            if (this.Children.Count == 0)
            {
                return base.MeasureOverride(availableSize);
            }

            int splitterThumbHeight = 55;

            if (this.Children[0].MinSize.Height != 0)
            {
                splitterThumbHeight = this.Children[0].MinSize.Height;
            }

            SizeF buttonSize = new SizeF(availableSize.Width, splitterThumbHeight);

            this.Children[0].Measure(buttonSize);

            return availableSize;
        }

        protected override SizeF ArrangeOverride(SizeF finalSize)
        {
            if (this.Children.Count == 0)
            {
                return base.ArrangeOverride(finalSize);
            }

            SizeF buttonSize = new SizeF(finalSize.Width, 55);

            this.Children[0].Arrange( new RectangleF( 0, (finalSize.Height - buttonSize.Height) / 2, buttonSize.Width, buttonSize.Height));

            return finalSize;
        }
    }
}
