using System.Windows.Input;
using Valg.EVA.Skanning.ContainerExtensions;
using Valg.EVA.Skanning.ServicesModel.ServiceContracts.Count.Read;
using Valg.EVA.Skanning.ServicesModel.ServiceContracts.Verification.Read;
using Valg.EVA.Skanning.UserInterfaceElements;
using Valg.EVA.Skanning.UserInterfaceElements.Commands;
using Valg.EVA.Skanning.Services.Count;
using Valg.EVA.Skanning.Verify.Startup.Filter;
using Valg.EVA.Skanning.Verify.Startup.VerifyBallotPaper;
using Valg.EVA.Skanning.Verify.Startup.VerifyBallotPaper.Messaging.Commands;

namespace Valg.EVA.Skanning.Verify.Startup.StartPage
{
    public class StartPageBoxVerifiedViewModel : ViewModelBase
    {
        private readonly ICommand openReviewOfVerifiedBallotsCommand;
        private readonly ICountingService countingService;
        private readonly ITypedFactory typedFactory;
        private BoxAggregatedStatusForVerifiedBoxes boxVerifiedStatus;
        private Counting counting;
        private MiniBoxCountingInformationViewModel boxCountingInformationViewModel;
        private bool isVisible;

        public StartPageBoxVerifiedViewModel(ICountingService countingService, ITypedFactory typedFactory)
        {
            this.countingService = countingService;
            this.typedFactory = typedFactory;
            isVisible = true;

            openReviewOfVerifiedBallotsCommand = new RelayCommand(p => OpenReviewVerifiedBallots());
        }

        protected override void SubscribeToViewModelSpecificEvents()
        {
            SubscribeToEvent<FilterChangedEvent, FilterChangedEventArgs>(FilterChanged);
        }

        private void FilterChanged(FilterChangedEventArgs args)
        {
            IsVisible = !BoxCountingInformationViewModel.ShouldHide(args.Filter);
        }
        
        public bool IsVisible
        {
            get { return isVisible; }
            set
            {
                if (value == isVisible)
                    return;

                isVisible = value;
                OnPropertyChanged();
            }
        }

        private void OpenReviewVerifiedBallots()
        {
            var boxEventArgs = new BoxEventArgs
            {
                BoxCountingId = boxVerifiedStatus.BoxCountingId,
                BoxNumber = boxVerifiedStatus.BoxNumber,
                OriginatingLocation = boxVerifiedStatus.OriginatingLocation,
                VotingCategoryName = VotingCategoryMapping.LocaleName(boxVerifiedStatus.VotingCategoryType),
                Counting = counting
            };
            
            PublishEvent<BoxIsSelectedForReviewEvent, BoxEventArgs>(boxEventArgs);
        }
        
        public string BoxDetailsLinkText
        {
            get { return string.Format(Valg.EVA.Skanning.Resources.Language.VerifyLanguageString.StartPageBoxViewModel_XBallots_Template, boxVerifiedStatus.NumberOfBallotPapersVerifiedByCurrentUser); }
        }
        
        public ICommand OpenReviewOfVerifiedBallotsCommand
        {
            get { return openReviewOfVerifiedBallotsCommand; }
        }

        public MiniBoxCountingInformationViewModel BoxCountingInformationViewModel
        {
            get { return boxCountingInformationViewModel; }
        }

        public void SetModel(BoxAggregatedStatusForVerifiedBoxes boxStatus)
        {
            boxVerifiedStatus = boxStatus;
            counting = countingService.GetCountingByBoxCountingId(boxStatus.BoxCountingId);
            boxCountingInformationViewModel = typedFactory.Create<MiniBoxCountingInformationViewModel>();
            BoxCountingInformationViewModel.SetValues(VotingCategoryMapping.LocaleName(boxStatus.VotingCategoryType), boxStatus.BoxNumber, boxStatus.OriginatingLocation, counting.CountingType, counting.CountNumber);
            OnPropertyChanged(null);
        }
    }
}
