using System.Collections.ObjectModel;
using System.Linq;
using Valg.EVA.Skanning.Entity.SerializedEntities.CountResultModel;
using Valg.EVA.Skanning.Entity.SqlConnection;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace Valg.EVA.Skanning.Repositories.Counting
{
    public class CountingResultRepository : ICountingResultRepository
    {
        private readonly ISqlConnectionFactory connectionFactory;

        public CountingResultRepository(ISqlConnectionFactory connectionFactory)
        {
            this.connectionFactory = connectionFactory;
        }

        public ReadOnlyCollection<CastVote> GetCastVotes(int countingId)
        {
            var allCountingResults =  connectionFactory.SP_GetList<Valg.EVA.Skanning.Entity.ReadModel.CountingResult>("CountingResult_GetCountingResults", new { CountingId = countingId }).ToList();

            var castVotes = new List<CastVote>();
            foreach (var countingResult in allCountingResults)
            {
                castVotes.Add(JsonConvert.DeserializeObject<CastVote>(countingResult.VoteInformation));
            }

            return castVotes.AsReadOnly();
        }
    }
}
