using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using C1.Silverlight.Legacy;

namespace StockPortfolio
{
    public class ChartSlider : C1RangeSlider
    {
        Rectangle _box;
        ChartSliderThumb _cThumb, _cUpperThumb;

        public ChartSlider()
        {
            _box = (Rectangle) FindName("Box");
            
            // alext M4: add custom thumbs as children for the real thumbs
            _cThumb = new ChartSliderThumb();
            _cUpperThumb = new ChartSliderThumb();
            Thumb.Children.Add(_cThumb);
            UpperThumb.Children.Add(_cUpperThumb);
            Thumb.Height = _cThumb.Height;
            Thumb.Width = _cThumb.Width;
            UpperThumb.Height = _cUpperThumb.Height;
            UpperThumb.Width = _cUpperThumb.Width;
            //
        }

        protected override ResourceInfo ResourceName
        {
            get { return new ResourceInfo("ChartSlider.xaml", this.GetType().Assembly); }
        }

        #region overrides
        // the chart slider doesn't support clicking to change it's range
        protected override void OnTrackBarClicked(MouseEventArgs e)
        {
        }

        protected override void PerformLayout()
        {
            base.PerformLayout();

            // make sure the box is sized to the height of the control
            if (_box != null)
            {
                _box.Height = this.Height;
            }
        }
        
        protected override void UpdateThumb()
        {
            base.UpdateThumb();
            // now adjust the rectangle 

            if (_box != null)
            {
                // adjust the thumb top
                double delta = (_box.Height / 2) - (this.Thumb.Height / 2);
                this.Thumb.SetValue(Canvas.TopProperty, delta);
                this.UpperThumb.SetValue(Canvas.TopProperty, delta);
                // position the area
                double left = (double) this.Thumb.GetValue(Canvas.LeftProperty) + this.Thumb.Width/2;
                double right = (double)this.UpperThumb.GetValue(Canvas.LeftProperty) + this.Thumb.Width/2;
                _box.SetValue(Canvas.LeftProperty, left);
                _box.Width = right - left;
            }
        }
        #endregion
    }
}
