using System.Collections.Generic;
using System.Collections.ObjectModel;
using Valg.EVA.Skanning.ServicesModel.ElectionConfig.Model;

using Unity.Interception.Utilities;
using Valg.EVA.Skanning.UserInterfaceElements;
using Valg.EVA.Skanning.Verify.Startup.VerifyBallotPaper.VerifiableViewModelsCollection;
using Valg.EVA.Skanning.Verify.Startup.VerifyBallotPaper.PageXOfY;

namespace Valg.EVA.Skanning.Verify.Startup.VerifyBallotPaper.RenumberStrikeout
{
    public class RenumberStrikeoutPageViewModel : ViewModelBase, IPagedVerificationViewModel
    {
        private readonly Contest contest;
        private readonly Election election;
        private readonly Affiliation affiliation;
        private readonly ObservableCollection<RenumberStrikeoutViewModel> renumberStrikeoutsForPage;
        private int pageIndex;

        public RenumberStrikeoutPageViewModel(Contest contest, Election election, Affiliation affiliation, ObservableCollection<RenumberStrikeoutViewModel> renumberStrikeoutsForPage)
        {
            this.contest = contest;
            this.election = election;
            this.affiliation = affiliation;
            this.renumberStrikeoutsForPage = renumberStrikeoutsForPage;

            SetPageIndexForAllRenumberStrikeoutsOnPage();
        }

        private void SetPageIndexForAllRenumberStrikeoutsOnPage()
        {
            foreach (var renumberStrikeoutViewModel in RenumberStrikeoutsForPage)
            {
                renumberStrikeoutViewModel.PageIndex = PageIndex;
            }
        }

        public string ElectionListDisplayName
        {
            get { return string.Format("{0} - {1} ({2})", affiliation.RegisteredName, contest.Name, election.Name); }
        }

        public ObservableCollection<RenumberStrikeoutViewModel> RenumberStrikeoutsForPage
        {
            get { return renumberStrikeoutsForPage; }
        }

        public int PageIndex
        {
            get { return pageIndex; }
            set
            {
                pageIndex = value;
                renumberStrikeoutsForPage.ForEach(c => c.PageIndex = value);
            }
        }

        public IEnumerable<IViewModelWithVerifiableContent> GetVerificationFields()
        {
            return renumberStrikeoutsForPage;
        }
    }
}
