using Valg.EVA.Skanning.Contracts.CommonEnums;
using Valg.EVA.Skanning.Entity.SqlConnection;
using Valg.EVA.Skanning.Entity.WriteModel;
using System;

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

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

        public int InsertCounting(CountingInsert counting)
        {
            return (int)connectionFactory.SP_Execute<decimal>("Counting_InsertCounting", counting);
        }

        public void InsertBoxCounting(BoxCountingInsert boxCountingInsert)
        {
            connectionFactory.SP_ExecuteVoid("Counting_InsertBoxCounting", boxCountingInsert);
        }

        public void UpdateBoxCountingState(string barcodeLong, string barcodeShort, int countingId, BoxCountingState boxCountingState, DateTime lastModified, BoxCountingState onlyAllowedPrecedingState, string changedBy)
        {
            connectionFactory.SP_ExecuteVoid("BoxCounting_UpdateBoxCountingState", new
            {
                BoxCountingState = boxCountingState,
                BarcodeLong = barcodeLong,
                BarcodeShort = barcodeShort,
                LastModified = lastModified,
                CountingId = countingId,
                ModifiedBy = changedBy,
                PreviousBoxCountingState = onlyAllowedPrecedingState
            });
        }
        
        public void UpdateCountingState(int countingId, CountingState countingState, DateTime lastModified, CountingState onlyAllowedPrecedingState, string modifiedBy)
        {
            connectionFactory.SP_ExecuteVoid("Counting_UpdateCountingState",
                new
                {
                    CountingId = countingId,
                    CountingState = countingState,
                    LastModified = lastModified,
                    PreviousCountingState = onlyAllowedPrecedingState,
                    ModifiedBy = modifiedBy
                });
        }
        
        public void MarkBoxCountingAsDeleted(int countingId, string barcodeLong, string barcodeShort, DateTime lastModified)
        {
            connectionFactory.SP_ExecuteVoid(
                "Counting_MarkBoxCountingAsDeleted",
                new
                {
                    CountingId = countingId,
                    BarcodeLong = barcodeLong,
                    BarcodeShort = barcodeShort,
                    LastModified = lastModified
                });
        }

        public void UpdateDuplicatesStateInformation(int boxCountingId, string modifiedBy, DateTime lastModified)
        {
            connectionFactory.SP_ExecuteVoid(
                "BoxCounting_UpdateDuplicatesStateInformation",
                new
                {
                    BoxCountingId = boxCountingId,
                    ModifiedBy = modifiedBy,
                    LastModified = lastModified
                });
        }

        public void MarkCountingAsDeleted(int countingId, DateTime lastModified, string modifiedBy)
        {
            connectionFactory.SP_ExecuteVoid(
                "Counting_MarkCountingAsDeleted",
                new
                {
                    CountingId = countingId,
                    LastModified = lastModified,
                    ModifiedBy = modifiedBy
                });
        }

        public void RegisterBoxAsEmpty(int boxCountingId, DateTime lastModified, string modifiedBy)
        {
            connectionFactory.SP_ExecuteVoid(
                "Counting_RegisterBoxAsEmpty",
                new
                {
                    BoxCountingId = boxCountingId,
                    LastModified = lastModified,
                    ModifiedBy = modifiedBy
                });
        }

        public void MarkCountingAsProcessedDuringEmergencMode(int countingId)
        {
            connectionFactory.SP_ExecuteVoid("Counting_MarkCountingAsProcessedDuringEmergencMode", new { CountingId = countingId });
        }

        public void UpdateBoxCountingWithOriginatingInfo(int boxCountingId, string modifiedBy, string originatingScannerComputer)
        {
            connectionFactory.SP_ExecuteVoid(
                "Counting_UpdateBoxCountingWithOriginatingInfo",
                new
                {
                    BoxCountingId = boxCountingId,
                    FormDefinitionManifest = String.Empty,
                    OriginatingScannerComputer = originatingScannerComputer,
                    ModifiedBy = modifiedBy
                });
        }
    }
}
