using System;
using System.Windows;
using System.Windows.Controls;
using Valg.EVA.Skanning.JobManagement.Startup.Data;
using Valg.EVA.Skanning.UserInterfaceElements;

namespace Valg.EVA.Skanning.JobManagement.Startup.UI
{
    public class ContextAwareControl : UserControl
    {
        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
            this.DataContextChanged += VoteCategoryAwareControl_DataContextChanged;
        }

        private void VoteCategoryAwareControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var pollingDistrictAwareViewModel = this.DataContext as IPollingDistrictAwareViewModel;
            if (pollingDistrictAwareViewModel != null)
            {
                pollingDistrictAwareViewModel.ActivePollingDistrict = this.PollingDistrict;
            }

            var votingCategoryAwareViewModel = this.DataContext as IVotingCategoryAwareViewModel;
            if (votingCategoryAwareViewModel != null)
            {
                votingCategoryAwareViewModel.ActiveVotingCategory = this.VotingCategoryType;
                votingCategoryAwareViewModel.HasOnlyOnePollingDistrict = this.HasOnlyOnePollingDistrict;
            }
        }

        public static readonly DependencyProperty VotingCategoryTypeProperty =
            DependencyProperty.Register(
                "VotingCategoryType",
                typeof(VotingCategoryType),
                typeof(ContextAwareControl),
                new UIPropertyMetadata(VotingCategoryType.Unknown, VotingCategoryTypeChanged));

        private static void VotingCategoryTypeChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            var control = dependencyObject as ContextAwareControl;
            if (control != null)
            {
                var dataContext = control.DataContext as IVotingCategoryAwareViewModel;
                if (dataContext != null)
                {
                    dataContext.ActiveVotingCategory = control.VotingCategoryType;
                }
            }
        }

        public VotingCategoryType VotingCategoryType
        {
            get
            {
                return (VotingCategoryType)this.GetValue(VotingCategoryTypeProperty);
            }
            set
            {
                this.SetValue(VotingCategoryTypeProperty, value);
            }
        }

        public static readonly DependencyProperty PollingDistrictProperty =
            DependencyProperty.Register(
                "PollingDistrict",
                typeof(PollingDistrictModel),
                typeof(ContextAwareControl),
                new UIPropertyMetadata(null, PollingDistrictChanged));

        private static void PollingDistrictChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            var control = dependencyObject as ContextAwareControl;
            if (control != null)
            {
                var dataContext = control.DataContext as IPollingDistrictAwareViewModel;
                if (dataContext != null)
                {
                    dataContext.ActivePollingDistrict = control.PollingDistrict;
                }
            }
        }

        public PollingDistrictModel PollingDistrict
        {
            get
            {
                return (PollingDistrictModel)this.GetValue(PollingDistrictProperty);
            }
            set
            {
                this.SetValue(PollingDistrictProperty, value);
            }
        }

        public static readonly DependencyProperty HasOnlyOnePollingDistrictProperty =
            DependencyProperty.Register(
                "HasOnlyOnePollingDistrict",
                typeof(bool),
                typeof(ContextAwareControl),
                new UIPropertyMetadata(false, HasOnlyOnePollingDistrictChanged));

        private static void HasOnlyOnePollingDistrictChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            var control = dependencyObject as ContextAwareControl;
            if (control != null)
            {
                var dataContext = control.DataContext as IVotingCategoryAwareViewModel;
                if (dataContext != null)
                {
                    dataContext.HasOnlyOnePollingDistrict = control.HasOnlyOnePollingDistrict;
                }
            }
        }

        public bool HasOnlyOnePollingDistrict
        {
            get
            {
                return (bool)this.GetValue(HasOnlyOnePollingDistrictProperty);
            }
            set
            {
                this.SetValue(HasOnlyOnePollingDistrictProperty, value);
            }
        }
    }
}
