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

namespace Valg.EVA.Skanning.Repositories.Box
{
    public class BoxRepository : IBoxRepository
    {
        private readonly ISqlConnectionFactory connectionFactory;

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

        public IList<BoxWithAggregatedToVerifyStatus> GetBoxesWithAggregatedToVerifyStatus(string contestId, string areaContextFilter, string electionEventId)
        {
            return connectionFactory.SP_GetList<BoxWithAggregatedToVerifyStatus>("Verify_GetBoxesWithAggregatedToVerifyStatus",
                new
                {
                    BoxCountingState = BoxCountingState.ReadyForVerify,
                    ContestId = contestId,
                    AreaContextFilter = areaContextFilter,
                    CountingType = CountingType.Final,
                    ElectionEventId = electionEventId
                }).ToList();
        }

        public IList<BoxWithAggregatedPartiallyOrCompletelyVerifiedStatus> GetBoxesWithAggregatedPartiallyOrCompletelyVerifiedStatus(bool ignoreCreatedBy, string createdBy, string contestId, string areaContextFilter, string electionEventId)
        {
            return connectionFactory.SP_GetList<BoxWithAggregatedPartiallyOrCompletelyVerifiedStatus>("Verify_GetBoxesWithAggregatedPartiallyOrCompletelyVerifiedStatus",
                new
                {
                    BoxCountingState = BoxCountingState.ReadyForVerify,
                    CreatedBy = createdBy,
                    IgnoreCreatedBy = ignoreCreatedBy,
                    ContestId = contestId,
                    AreaContextFilter = areaContextFilter,
                    ElectionEventId = electionEventId
                }).ToList();
        }

        public IList<LockedBallotPaper> GetLockedBallotPapersInBoxCounting(int boxCountingId)
        {
            return connectionFactory.SP_GetList<LockedBallotPaper>("BallotPaper_GetLockedBallotPapersInBoxCounting",
                new
                {
                    BoxCountingId = boxCountingId,
                }).ToList();
        }

        public int[] GetBallotPapersVerifiedByUserAndInBox(int boxCountingId, string verifiedBy)
        {
            return connectionFactory.SP_GetList<int>("Verify_GetBallotPapersVerifiedByUserAndInBox", new { BoxCountingState = BoxCountingState.ReadyForVerify, CreatedBy = verifiedBy, BoxCountingId = boxCountingId }).ToArray();
        }

        public BoxCountingState GetBoxCountingStateByBallotPaperId(int ballotPaperId)
        {
            return connectionFactory.SP_Get<BoxCountingState>("Verify_GetBoxCountingStateByBallotPaperId", new { BallotPaperId = ballotPaperId });
        }
    }
}
