using System;
using System.Collections.ObjectModel;
using Valg.EVA.Skanning.ContainerExtensions;
using Valg.EVA.Skanning.Identity;
using Valg.EVA.Skanning.Services.Verification;
using Valg.EVA.Skanning.ServicesModel.ServiceContracts.Verification.Read;
using System.Linq;
using Valg.EVA.Skanning.UserInterfaceElements;

namespace Valg.EVA.Skanning.Verify.Startup.StartPage
{
    public class VerifyStartPageViewModel : ViewModelBase
    {
        private readonly IVerificationService verificationService;
        private readonly ITypedFactory typedFactory;
        private readonly IIdentityProvider identityProvider;
        private readonly ObservableCollection<StartPageBoxToVerifyViewModel> boxesToVerify = new ObservableCollection<StartPageBoxToVerifyViewModel>();
        private readonly ObservableCollection<StartPageBoxVerifiedViewModel> boxesVerified = new ObservableCollection<StartPageBoxVerifiedViewModel>();
        private string lastUpdated;

        public VerifyStartPageViewModel(IVerificationService verificationService, ITypedFactory typedFactory, IIdentityProvider identityProvider)
        {
            this.verificationService = verificationService;
            this.typedFactory = typedFactory;
            this.identityProvider = identityProvider;
        }

        protected override void SubscribeToViewModelSpecificEvents()
        {
            SubscribeToEvent<VerifyStartPageDataWasRefreshedEvent, EventArgs>(e => LastUpdated = DateTime.Now.ToString());
        }

        public string VerifyStartPageHeaderText
        {
            get
            {
                var verificationTextBase = Resources.Language.VerifyLanguageString.Verification;

                var role = identityProvider.CurrentSelectedRole;
                if (role == null)
                {
                    return verificationTextBase;
                }

                if (role.IsSubArea)
                {
                    return $"{verificationTextBase} | {role.ElectionName} | {role.CountyArea.AreaName} | {role.Area.AreaName}";
                }
                else
                {
                    return $"{verificationTextBase} | {role.ElectionName} | {role.Area.AreaName}";
                }
            }
        }
        public string BallotPapersToVerifyHeaderText => Resources.Language.VerifyLanguageString.BallotPapersToVerifyHeaderText;
        public string BallotPapersVerifiedHeaderText => Resources.Language.VerifyLanguageString.BallotPapersVerifiedHeaderText;
        public ObservableCollection<StartPageBoxToVerifyViewModel> BoxesToVerify => boxesToVerify;
        public ObservableCollection<StartPageBoxVerifiedViewModel> BoxesVerified => boxesVerified;

        public string LastUpdated
        {
            get { return lastUpdated; }
            set
            {
                if (value == lastUpdated) return;

                lastUpdated = value;
                OnPropertyChanged();
            }
        }

        public void RefreshData()
        {
            BoxesToVerify.Clear();
            BoxesVerified.Clear();

            if (identityProvider.CurrentSelectedRole != null)
            {

                var serviceResult =
                    verificationService.GetBoxAggregatedStatusForVerify(new GetBoxAggregatedStatusForVerifyParameters
                    {
                        CurrentUserName = identityProvider.GetCurrentUserName(),
                        ContestId = identityProvider.CurrentSelectedRole.ContestId,
                        AreaContextFilter = identityProvider.CurrentSelectedRole.CountingEmlAreaContextId,
                        EmlAreaContextId = identityProvider.CurrentSelectedRole.Area.EmlAreaContextId,
                        ElectionEventId = identityProvider.CurrentSelectedRole.Area.ElectionEventId
                    });

                foreach (var serviceBox in serviceResult.BoxesNotCompletelyVerified.OrderBy(p=> p.VotingCategoryType).ThenBy(p=> p.OriginatingLocation))
                {
                    var viewModelBox = typedFactory.Create<StartPageBoxToVerifyViewModel>();
                    viewModelBox.SetModel(serviceBox);
                    boxesToVerify.Add(viewModelBox);
                }

                foreach (var serviceBoxVerified in serviceResult.BoxesPartiallyOrCompletelyVerified.OrderBy(p => p.VotingCategoryType).ThenBy(p=> p.OriginatingLocation))
                {
                    var viewModelBox = typedFactory.Create<StartPageBoxVerifiedViewModel>();
                    viewModelBox.SetModel(serviceBoxVerified);
                    boxesVerified.Add(viewModelBox);
                }
                
                PublishEvent<VerifyStartPageDataWasRefreshedEvent, EventArgs>(EventArgs.Empty);
            }
        }
    }
}
