using Valg.EVA.Skanning.Contracts.CommonEnums;
using Valg.EVA.Skanning.UserInterfaceElements.Counting;
using Valg.EVA.Skanning.UserInterfaceElements.Language;

namespace Valg.EVA.Skanning.UserInterfaceElements.TopBar
{
    public class BoxCountingInformationViewModel : ViewModelBase
    {
        private string areaDescription;
        private string boxDescription;
        private string countingDescription;
        private bool showAreaDescription;
        private string votingCategory;
        private bool isVisible;
        private bool alwaysVisible;

        protected override void SubscribeToViewModelSpecificEvents()
        {
            SubscribeToEvent<BoxCountingInformationViewSetDataEvent, BoxCountingInformationViewSetDataEventArgs>(args => SetValues(args.VotingCategoryName, args.BoxNumber, args.OriginatingLocation, args.CountingType, args.CountingNumber));
            SubscribeToEvent<BoxCountingInformationViewChangeVisibilityEvent, BoxCountingInformationViewChangeVisibilityEventArgs>(ShowOrHide);
            SubscribeToEvent<LanguageChangedEvent, LanguageChangedEventArgs>(NotifyLanguageChanged);
        }

        private void NotifyLanguageChanged(LanguageChangedEventArgs obj)
        {
            TranslateValues();
        }

        private void ShowOrHide(BoxCountingInformationViewChangeVisibilityEventArgs obj)
        {
            if (obj.Show)
            {
                ShowValues();
            }
            else
            {
                RemoveValues();
            }
        }

        private void ShowValues()
        {
            IsVisible = true;
        }

        private void RemoveValues()
        {
            IsVisible = false;
        }

        public string VotingCategory
        {
            get { return votingCategory; }
            private set
            {
                votingCategory = value;
                OnPropertyChanged();
            }
        }

        public bool ShowAreaDescription
        {
            get { return showAreaDescription; }
            private set
            {
                showAreaDescription = value;
                OnPropertyChanged();
            }
        }

        public string BoxDescription
        {
            get { return boxDescription; }
            private set
            {
                boxDescription = value;
                OnPropertyChanged();
            }
        }

        public string CountingDescription
        {
            get { return countingDescription; }
            private set
            {
                countingDescription = value;
                OnPropertyChanged();
            }
        }

        public string AreaDescription
        {
            get { return areaDescription; }
            private set
            {
                areaDescription = value;
                OnPropertyChanged();
            }
        }

        public bool IsVisible
        {
            get
            {
                return alwaysVisible || isVisible;
            }
            set
            {
                isVisible = alwaysVisible || value;

                OnPropertyChanged();
            }
        }

        private int CountingNumber { get; set; }
        private int BoxNumber { get; set; }
        private CountingType CurrentCountingType { get; set; }

        public void SetAlwaysVisible()
        {
            alwaysVisible = true;
            IsVisible = true;
        }

        private void SetValues(string votingCategoryName, int boxNumber, string originatingLocation, CountingType countingType, int countingNumber)
        {
            IsVisible = true;
            VotingCategory = votingCategoryName;
            BoxDescription = $"{Resources.Language.VerifyLanguageString.Box} {boxNumber}";
            BoxNumber = boxNumber;
            AreaDescription = originatingLocation;
            ShowAreaDescription = !string.IsNullOrWhiteSpace(originatingLocation);
            CurrentCountingType = countingType;
            CountingNumber = countingNumber;
            CountingDescription = countingType.GetCountingDescription(countingNumber, true);
        }

        private void TranslateValues()
        {
            VotingCategory = votingCategory;
            BoxDescription = $"{Resources.Language.VerifyLanguageString.Box} {BoxNumber}";
            CountingDescription = CurrentCountingType.GetCountingDescription(CountingNumber, true);
        }
    }
}
