using System;
using System.Windows;
using System.Windows.Controls;

namespace BeWo.View
{
    public partial class PercentageDisplay : UserControl
    {
        public static readonly DependencyProperty PercentageProperty = DependencyProperty.Register("Percentage", typeof(decimal), typeof(PercentageDisplay), new UIPropertyMetadata(0m, OnPercentageChanged));

        public PercentageDisplay()
        {
            this.InitializeComponent();

            this.Loaded += (s, e) => this.Refresh(this.Percentage);
        }

        public decimal Percentage
        {
            get
            {
                return (decimal)this.GetValue(PercentageProperty);
            }

            set
            {
                this.SetValue(PercentageProperty, value);
            }
        }

        private static void OnPercentageChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            PercentageDisplay lControl = sender as PercentageDisplay;

            if (lControl.IsLoaded)
            {
                lControl.Refresh((decimal)e.NewValue);
            }
        }

        private void Refresh(decimal lValue)
        {
            if (lValue > 100)
            {
                this.bar_to100.Visibility = Visibility.Collapsed;
                this.grid_twoBars.ColumnDefinitions[1].Width = new GridLength(Convert.ToDouble(lValue - 100), GridUnitType.Star);
                this.grid_twoBars.Children.Clear();
                this.grid_twoBars.Children.Add(this.leftBar);
                this.grid_twoBars.Children.Add(this.rightBar);
            }
            else
            {
                this.bar_to100.Visibility = Visibility.Visible;
                this.bar_to100.Value = Convert.ToDouble(lValue);
            }

            this.textblock_percent.Text = string.Format("{0:0.00}%", lValue);
        }
    }
}