using Valg.EVA.Skanning.UserInterfaceElements;
using Prism.Commands;

namespace Valg.EVA.Skanning.JobManagement.Startup.UI.Filter
{
    public class FilterViewModel : ReactiveViewModel, IVotingCategoryAwareViewModel
    {
        private string _filterText;

        private VotingCategoryType activeVotingCategory;

        public FilterViewModel()
        {
            ClearTextFieldCommand = new DelegateCommand(ClearTextField);
        }
       
        public string FilterText
        {
            get { return _filterText; }
            set
            {
                _filterText = value;
                RaiseEvent();
                OnPropertyChanged();
                OnPropertyChanged(nameof(IsFilterActive));
            }
        }

        public bool IsFilterActive => !string.IsNullOrWhiteSpace(FilterText) && FilterText.Length > 0;

        public VotingCategoryType ActiveVotingCategory
        {
            get { return activeVotingCategory; }
            set
            {
                activeVotingCategory = value;
                OnPropertyChanged();                
            }
        }

        public bool HasOnlyOnePollingDistrict { get; set; }

        public DelegateCommand ClearTextFieldCommand { get; private set; }

        private void ClearTextField()
        {
            FilterText = string.Empty;
        }

        private void RaiseEvent()
        {
            PublishEvent<FilterChangedEvent, FilterChangedEventArgs>(new FilterChangedEventArgs { Filter = FilterText, ActiveVotingCategory = ActiveVotingCategory });
        }
    }
}
