using Bp.Sync.BackEnd.Infrastructure.ElasticDbContext;
using Bp.Sync.Domain.Elastics.Inventories.Bills;
using Bp.Sync.Dto.Models;
using Bp.Sync.Dto.Results;
using Bp.Sync.Dto.SmartTable;
using Microsoft.Extensions.Options;
using Nest;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Bp.Sync.BackEnd.Infrastructure.Repositories
{
    public interface IInboundBillRepository
    {
        Task<BaseResult> Add(InboundBillElastic InboundBill);

        Task<BaseResult> AddManyAsync(IEnumerable<InboundBillElastic> InboundBills);

        Task<BulkResponse> DeleteInboundBills(IEnumerable<InboundBillElastic> InboundBills);

        Task<DeleteByQueryResponse> DeleteInboundBillsByQuery(IEnumerable<long> ids);

        Task<SmartTableResult<BaseLong>> SearchIds(BoolQueryDescriptor<InboundBillElastic> query);

        Task<SmartTableResult<InboundBillElastic>> SearchForExcel(BoolQueryDescriptor<InboundBillElastic> query);

        Task<SmartTableResult<InboundBillElastic>> Search(BoolQueryDescriptor<InboundBillElastic> query, int count);

        Task<SmartTableResult<InboundBillElastic>> SearchBySort(BoolQueryDescriptor<InboundBillElastic> query, SortDescriptor<InboundBillElastic> searchDescriptor, int count);
    }

    public class InboundBillRepository : IInboundBillRepository
    {
        private readonly ElasticIndex<InboundBillElastic> _dbContext;

        public InboundBillRepository(IOptions<ElasticConnectionSettings> settings)
        {
            _dbContext = new ElasticIndex<InboundBillElastic>(settings.Value.ClusterUrl, "inventoryinboundbill");
        }

        #region search

        public async Task<SmartTableResult<InboundBillElastic>> SearchForExcel(BoolQueryDescriptor<InboundBillElastic> query)
        {
            return await _dbContext.Search(query);
        }

        #endregion search

        #region Add order

        public async Task<BaseResult> Add(InboundBillElastic InboundBill)
        {
            var result = new BaseResult() { Result = Dto.Results.Result.Success };
            var rs = await _dbContext.Insert(InboundBill);
            if (rs.IsValid)
            {
                result.Result = Dto.Results.Result.Success;
            }
            else
            {
                result.Result = Dto.Results.Result.SystemError;
                result.Message = "Không tạo được bản ghi";
            }
            return result;
        }

        public async Task<BaseResult> AddManyAsync(IEnumerable<InboundBillElastic> InboundBills)
        {
            var result = new BaseResult { Result = Dto.Results.Result.Success };
            try
            {
                var rs = await _dbContext.InsertMany(InboundBills);
                if (rs.IsValid) return result;
                else
                {
                    result.Result = Dto.Results.Result.Failed;
                    result.Message = rs.Errors.ToString();
                    return result;
                }
            }
            catch (Exception exx)
            {
                result.Result = Dto.Results.Result.Failed;
                result.Message = exx.Message.ToString();
                return result;
            }
        }

        public async Task<BulkResponse> DeleteInboundBills(IEnumerable<InboundBillElastic> InboundBills)
        {
            return await _dbContext.DeleteMany(InboundBills);
        }

        public async Task<DeleteByQueryResponse> DeleteInboundBillsByQuery(IEnumerable<long> ids)
        {
            return await _dbContext.DeleteByIds(ids);
        }

        public async Task<SmartTableResult<InboundBillElastic>> Search(BoolQueryDescriptor<InboundBillElastic> query, int count)
        {
            return await _dbContext.Search(query, count);
        }

        public async Task<SmartTableResult<BaseLong>> SearchIds(BoolQueryDescriptor<InboundBillElastic> query)
        {
            return await _dbContext.SearchIds(query);
        }

        public async Task<SmartTableResult<InboundBillElastic>> SearchBySort(BoolQueryDescriptor<InboundBillElastic> query, SortDescriptor<InboundBillElastic> searchDescriptor, int count)
        {
            return await _dbContext.SearchBySort(query, searchDescriptor, count);
        }

        #endregion Add order
    }
}