using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Valg.EVA.Skanning.Contracts.CommonEnums;
using Valg.EVA.Skanning.Entity.ReadModel;
using Valg.EVA.Skanning.Entity.SqlConnection;

namespace Valg.EVA.Skanning.Repositories.BallotPaper
{
    public class BallotPaperRepository : IBallotPaperRepository
    {
        private readonly ISqlConnectionFactory connectionFactory;

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

        public Valg.EVA.Skanning.Entity.ReadModel.BallotPaper GetBallotPaper(int ballotPaperId)
        {
            return connectionFactory.SP_Get<Valg.EVA.Skanning.Entity.ReadModel.BallotPaper>("Verify_GetBallotPaper", new { BallotPaperId = ballotPaperId });
        }

        public List<BallotPaperFieldValue> GetFieldValues(int ballotPaperId, ValueSourceType valueSourceType)
        {
            return connectionFactory.SP_GetList<BallotPaperFieldValue>("Verify_GetFieldValuesByValueSourceType", new { BallotPaperId = ballotPaperId, ValueSourceType = valueSourceType }).ToList();
        }

        public ReadOnlyCollection<int> GetBallotPapersInCounting(int countingId)
        {
            return connectionFactory.SP_GetList<int>("BallotPaper_GetBallotPaperIdsInCounting", new { CountingId = countingId }).ToList().AsReadOnly();
        }

        public BallotPaperValues GetOriginalBallotPaperValues(int ballotPaperId)
        {
            return connectionFactory.SP_Get<BallotPaperValues>("BallotPaper_GetOriginalBallotPaperValues", new {BallotPaperId = ballotPaperId});
        }

        public BallotPaperValues GetCurrentBallotPaperValues(int ballotPaperId)
        {
            return connectionFactory.SP_Get<BallotPaperValues>("BallotPaper_GetCurrentBallotPaperValues", new { BallotPaperId = ballotPaperId });
        }

        public ReadOnlyCollection<BallotPaperFieldValue> GetCurrentFieldValues(int ballotPaperId)
        {
            return connectionFactory.SP_GetList<BallotPaperFieldValue>("Verify_GetCurrentFieldValues", new { BallotPaperId = ballotPaperId }).ToList().AsReadOnly();
        }

        public NextBallotPaper GetNextBallotPaperToVerify(int boxCountingId)
        {
            return connectionFactory.SP_Get<NextBallotPaper>("Verify_GetNextBallotPaperToVerify", new { BoxCountingId = boxCountingId, BoxCountingState = BoxCountingState.ReadyForVerify });
        }

        public int GetTotalBallotPapersForVerificationCount(int boxCountingId)
        {
            return connectionFactory.SP_Get<int>("Verify_GetTotalBallotPapersForVerification", new { BoxCountingId = boxCountingId });
        }

        public BallotPaperImages GetBallotPaperImages(int ballotPaperId)
        {
            return connectionFactory.SP_Get<BallotPaperImages>("Verify_GetBallotPaperImages", new { BallotPaperId = ballotPaperId });
        }

        public List<BallotPaperInBoxCounting> GetBallotPapersInBoxCounting(int boxCountingId)
        {
            var ballotPapers = connectionFactory.SP_GetList<BallotPaperInBoxCounting>("BallotPaper_GetBallotPapersInBoxCounting", new { BoxCountingId = boxCountingId });
            return ballotPapers.ToList();
        }

        public BallotPaperPositionInBox GetBallotPaperPositionInBox(int ballotPaperId, int boxCountingId)
        {
            return connectionFactory.SP_Get<BallotPaperPositionInBox>("Verify_GetBallotPaperPlacementInBox", new { BallotPaperId = ballotPaperId, BoxCountingId = boxCountingId });
        }
    }
}

