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.Messaging.Commands;

namespace Valg.EVA.Skanning.Verify.Startup.StartPage
{
    public class StartPageBoxToVerifyViewModel : ViewModelBase
    {
        private readonly ICommand goToVerifyWithThisBoxCommand;
        private readonly ICountingService countingService;
        private readonly ITypedFactory typedFactory;
        private MiniBoxCountingInformationViewModel boxCountingInformationViewModel;
        private BoxAggregatedStatusForVerify boxAggregatedStatus;
        private Counting counting;
        private bool isVisible;

        public StartPageBoxToVerifyViewModel(ICountingService countingService, ITypedFactory typedFactory)
        {
            this.countingService = countingService;
            this.typedFactory = typedFactory;
            isVisible = true;
            goToVerifyWithThisBoxCommand = new RelayCommand(o => GoToVerifyWithThisBox());
        }

        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)
                {
                    isVisible = value;
                    OnPropertyChanged();
                }
            }
        }

        public string BoxDetailsLinkText
        {
            get
            {
                return string.Format(Resources.Language.VerifyLanguageString.StartPageBoxViewModel_XBallots_Template, boxAggregatedStatus.NumberOfBallotPapers);
            }
        }


        public ICommand GoToVerifyWithThisBoxCommand
        {
            get { return goToVerifyWithThisBoxCommand; }
        }
        public MiniBoxCountingInformationViewModel BoxCountingInformationViewModel
        {
            get { return boxCountingInformationViewModel; }
        }

        private void GoToVerifyWithThisBox()
        {
            var boxEventArgs = new BoxEventArgs
            {
                BoxCountingId = boxAggregatedStatus.BoxCountingId,
                BoxNumber = boxAggregatedStatus.BoxNumber,
                OriginatingLocation = boxAggregatedStatus.OriginatingLocation,
                VotingCategoryName = VotingCategoryMapping.LocaleName(boxAggregatedStatus.VotingCategoryType),
                Counting = counting
            };

            PublishEvent<BoxIsSelectedForVerifyEvent, BoxEventArgs>(boxEventArgs);
        }

        public void SetModel(BoxAggregatedStatusForVerify boxStatus)
        {
            boxAggregatedStatus = 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);
        }
    }
}
