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

using BeWo.ViewModel;
using BeWo.ViewModel.ListViewModel;

using BS.Shared;
using BS.Shared.DataContracts;

namespace BeWo.View
{
    /// <summary>
    /// Interaction logic for CostRatePeriodControl.xaml
    /// </summary>
    public partial class CostRatePeriodControl : UserControl
    {
        public static readonly DependencyProperty VMProperty = DependencyProperty.Register(
            "VM", 
            typeof(CostRatePeriodListVM), 
            typeof(CostRatePeriodControl), 
            new UIPropertyMetadata(
                (s, e) =>
                    {
                        CostRatePeriodControl crpc = s as CostRatePeriodControl;
                        if (crpc != null)
                        {
                            crpc.Refresh();
                            if (crpc.VM != null)
                            {
                                crpc.VM.VMList.ListChanged += (s1, e1) =>
                                    {
                                        if (e1.ListChangedType == ListChangedType.ItemChanged &&
                                            e1.PropertyDescriptor.Name.Equals(CostRatePeriodVM.PropertyName_EndDate))
                                        {
                                            crpc.SetBorder();
                                        }
                                    };
                            }
                        }
                    }));

        public static readonly DependencyProperty ValueListEntryProperty = DependencyProperty.Register("ValueListEntry", typeof(ValueListEntryDC), typeof(CostRatePeriodControl), 
            new UIPropertyMetadata(
                (s, e) => 
                    ((CostRatePeriodControl)s).Refresh()));

        private CostRatePeriodType? _Type;

        public CostRatePeriodControl()
        {
            this.InitializeComponent();
            this.lblValidUntil.Visibility = Visibility.Hidden;
        }

        public string ButtonTooltipText
        {
            get
            {
                return this.button_addCostRatePeriod.ToolTip as string;
            }

            set
            {
                this.button_addCostRatePeriod.ToolTip = value;
            }
        }

        public CostRatePeriodType? CostRateType
        {
            get
            {
                return this._Type;
            }

            set
            {
                this._Type = value;
                this.Refresh();
            }
        }

        public bool IsUnitNameEditingEnabled
        {
            get
            {
                return this.txtActualUnitName.Visibility == Visibility.Visible;
            }

            set
            {
                this.lblActualUnitName.Visibility = this.txtActualUnitName.Visibility = value ? Visibility.Visible : Visibility.Collapsed;
            }
        }

        public string UnitNameLabelText
        {
            get
            {
                return this.lblActualUnitName.Text;
            }

            set
            {
                this.lblActualUnitName.Text = value;
            }
        }

        public CostRatePeriodListVM VM
        {
            get
            {
                return (CostRatePeriodListVM)this.GetValue(VMProperty);
            }

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

        public string ValueLabelText
        {
            get
            {
                return this.lblActualValue.Text;
            }

            set
            {
                this.lblActualValue.Text = value;
            }
        }

        public ValueListEntryDC ValueListEntry
        {
            get
            {
                return (ValueListEntryDC)this.GetValue(ValueListEntryProperty);
            }

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

        private void Refresh()
        {
            if (this.VM != null && this.CostRateType.HasValue)
            {
                var act = this.VM.GetLatestRate(this.CostRateType.Value, this.ValueListEntry);
                if (act == null)
                {
                    this.VM.NewVM.CostRateType = this.CostRateType.Value;
                    act = this.VM.AddNewVMToList();
                }

                this.LayoutRoot.DataContext = act;
                this.listBoxOldValues.ItemsSource = this.VM.GetOldRates(this.CostRateType.Value, this.ValueListEntry);
                this.SetBorder();
            }
        }

        private void SetBorder()
        {
            var old = this.VM.GetOldRates(this.CostRateType.Value, this.ValueListEntry);
            if (old.Count == 0)
            {
                this.listBoxOldValues.Visibility = Visibility.Collapsed;
                this.lblValidUntil.Visibility = Visibility.Hidden;
            }
            else
            {
                this.listBoxOldValues.Visibility = Visibility.Visible;

                DateTime last = DateTime.MinValue;
                foreach (var vm in old)
                {
                    if (vm.EndDate != null && vm.EndDate.Value > last)
                    {
                        last = vm.EndDate.Value;
                    }
                }

                if (last > DateTime.MinValue)
                {
                    last = last.AddDays(1);
                    this.lblValidUntil.Text = "(gültig ab " + last.ToShortDateString() + ")";
                    this.lblValidUntil.Visibility = Visibility.Visible;
                }
            }
        }

        private void button_addCostRatePeriod_Click(object sender, RoutedEventArgs e)
        {
            var act = this.VM.GetLatestRate(this.CostRateType.Value, this.ValueListEntry);
            this.VM.NewVM.CostRateValue = act.CostRateValue;
            this.VM.NewVM.EndDate = DateTime.Now.AddDays(-1);
            this.VM.NewVM.CostRateType = this.CostRateType.Value;
            this.VM.NewVM.ValueListEntry = this.ValueListEntry;
            this.VM.NewVM.UnitName = act.UnitName;
            this.VM.AddNewVMToList();
            this.Refresh();
        }

        private void button_deleteCostRatePeriod_Click(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)sender;
            this.VM.VMList.Remove(btn.DataContext as CostRatePeriodVM);
            this.Refresh();
        }

    
    }
}