using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Data;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace GeoDashboard
{
    public partial class LinearGauge : UserControl
    {
        Binding _binding;
        int _currentIndex;

        public LinearGauge()
        {
            InitializeComponent();
            LayoutUpdated += LinearGauge_LayoutUpdated;
        }
        /// <summary>
        /// Gets or sets the index value shown on the LinearGauge.
        /// </summary>
        public Binding IndexValue
        {
            get { return _binding; }
            set 
            {
                _binding = value;
                _tbMarker.SetBinding(TextBlock.TextProperty, value);
            }
        }

        // position marker within the control
        void LinearGauge_LayoutUpdated(object sender, EventArgs e)
        {
            int index;
            if (int.TryParse(_tbMarker.Text, out index))
            {
                if (index != _currentIndex)
                {
                    // save new index
                    _currentIndex = index;

                    // use log to convert index into factor between -1.5 and +1.5
                    var val = Math.Log(index / 100.0);
                    val = Math.Max(Math.Min(val, 1.5), -1.5);

                    // adjust factor to current width
                    val = (this.ActualWidth / 2 / 1.5) * val;

                    // apply translate transform
                    var tt = _bdrMarker.RenderTransform as TranslateTransform;
                    tt.X = val;
                }
            }
        }
    }
}
