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

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

namespace Valg.EVA.Skanning.Verify.Startup.VerifyBallotPaper.Candidate
{
    public class CandidatePageViewModel : IPagedVerificationViewModel
    {
        private readonly Contest contest;
        private readonly Election election;
        private readonly Affiliation affiliation;
        private readonly ObservableCollection<CandidateViewModel> candidatesForPage;
        private int pageIndex;

        public CandidatePageViewModel(Contest contest, Election election, Affiliation affiliation, ObservableCollection<CandidateViewModel> candidatesForPage)
        {
            this.contest = contest;
            this.election = election;
            this.affiliation = affiliation;
            this.candidatesForPage = candidatesForPage;

            SetPageIndexForAllCandidatesOnPage();
        }

        private void SetPageIndexForAllCandidatesOnPage()
        {
            foreach (var candidateViewModel in CandidatesForPage)
            {
                candidateViewModel.PageIndex = PageIndex;
            }
        }

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

        public ObservableCollection<CandidateViewModel> CandidatesForPage
        {
            get { return candidatesForPage; }
        }

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

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