using Valg.EVA.Skanning.Entity.SqlConnection;
using Valg.EVA.Skanning.Entity.WriteModel;
using Newtonsoft.Json;

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

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

        public int InsertBallotPaper(BallotPaperInsert ballotPaperInsert)
        {
            return connectionFactory.SP_Execute<int>("Scan_InsertBallotPaper", ballotPaperInsert);
        }

        public void InsertBallotPaperField(BallotPaperFieldInsert ballotPaperFieldInsert)
        {
            connectionFactory.SP_ExecuteVoid("Scan_InsertBallotPaperField", ballotPaperFieldInsert);
        }

        public void InsertBallotPaperFieldValue(BallotPaperFieldValueInsert ballotPaperFieldValueInsert)
        {
            connectionFactory.SP_ExecuteVoid("Verify_InsertBallotPaperFieldValue", ballotPaperFieldValueInsert);
        }

        public void InsertWriteInBallotPaperFieldValue(WriteInBallotPaperFieldValueInsert writeInBallotPaperFieldValueInsert)
        {
            var internalInsert = new BallotPaperFieldValueInsert
            {
                BallotPaperFieldId = writeInBallotPaperFieldValueInsert.BallotPaperFieldId,
                CreatedBy = writeInBallotPaperFieldValueInsert.CreatedBy,
                NeedsVerification = writeInBallotPaperFieldValueInsert.NeedsVerification,
                ValueSourceTypeId = writeInBallotPaperFieldValueInsert.ValueSourceTypeId,
                Value = JsonConvert.SerializeObject(writeInBallotPaperFieldValueInsert.WriteInData)
            };

            InsertBallotPaperFieldValue(internalInsert);
        }

        public void InsertBallotPaperValues(BallotPaperValuesInsert newValuesInsert)
        {
            connectionFactory.SP_ExecuteVoid("BallotPaper_InsertBallotPaperValues", newValuesInsert);
        }

        public void InsertBallotPaperImages(BallotPaperImageInsert ballotPaperImageInsert)
        {
            connectionFactory.SP_ExecuteVoid("Scan_InsertBallotPaperImages", ballotPaperImageInsert);
        }

        public void UpdateBallotPaperCombinedImage(BallotPaperCombinedImageInsert ballotPaperCombinedImageInsert)
        {
            connectionFactory.SP_ExecuteVoid("Verify_UpdateBallotPaperCombinedImage", ballotPaperCombinedImageInsert);
        }

        public void ResetBallotPaperFieldValue(BallotPaperFieldValueReset ballotPaperFieldValueReset)
        {
            connectionFactory.SP_ExecuteVoid("BallotPaper_ResetBallotPaperFieldValue", ballotPaperFieldValueReset);
        }
    }
}
