using AsbCloudApp.Data.Trajectory;
using AsbCloudApp.Exceptions;
using AsbCloudApp.Repositories;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudDb.Model.Trajectory;
using Mapster;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace AsbCloudInfrastructure.Repository
{
    /// <summary>
    /// CRUD-репозиторий для работы с траекториями (плановыми и фактическими)
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <typeparam name="Tdto"></typeparam>
    public class TrajectoryEditableRepository<TEntity, Tdto> : ITrajectoryEditableRepository<Tdto>
        where TEntity : Trajectory
        where Tdto : TrajectoryGeoDto
    {
        private readonly IAsbCloudDbContext db;
        private readonly IWellService wellService;
        public TrajectoryEditableRepository(IAsbCloudDbContext db, IWellService wellService)
        {
            this.db = db;
            this.wellService = wellService;
        }

        public async Task<int> AddRangeAsync(IEnumerable<Tdto> trajectoryRows, CancellationToken token)
        {
            var idWell = trajectoryRows.First().IdWell;
            if (!trajectoryRows.All(r => r.IdWell == idWell))
                throw new ArgumentInvalidException(nameof(trajectoryRows), "Все строки должны относиться к одной скважине");

            var entities = trajectoryRows
                .Select(e =>
                {
                    var entity = Convert(e);
                    entity.Id = 0;
                    return entity;
                });

            db.Set<TEntity>().AddRange(entities);
            return await db.SaveChangesAsync(token);
        }

        public async Task<int> AddAsync(Tdto trajectoryRow, CancellationToken token)
        {
            var entity = Convert(trajectoryRow);
            entity.Id = 0;
            db.Set<TEntity>().Add(entity);
            return await db.SaveChangesAsync(token)
                .ConfigureAwait(false);
        }

        public async Task<int> DeleteRangeAsync(IEnumerable<int> ids, CancellationToken token)
        {
            var query = db.Set<TEntity>()
                .Where(e => ids.Contains(e.Id));
            db.Set<TEntity>().RemoveRange(query);
            return await db.SaveChangesAsync(token)
                .ConfigureAwait(false);
        }

        public async Task<int> DeleteByIdWellAsync(int idWell, CancellationToken token)
        {
            var query = db.Set<TEntity>()
                .Where(e => e.IdWell == idWell);
            db.Set<TEntity>().RemoveRange(query);
            return await db.SaveChangesAsync(token)
                .ConfigureAwait(false);
        }

        public async Task<IEnumerable<Tdto>> GetAsync(int idWell, CancellationToken token)
        {
            var well = wellService.GetOrDefault(idWell)
                ?? throw new ArgumentInvalidException(nameof(idWell), "idWell doesn`t exist");

            var offsetHours = well.Timezone.Hours;
            var query = db.Set<TEntity>()
                .AsNoTracking()
                .Where(x => x.IdWell == well.Id);
            var entities = await query
                .OrderBy(e => e.WellboreDepth)
                .ToArrayAsync(token);

            var result = entities
                .Select(r => Convert(r, offsetHours));
            return result;
        }

        public async Task<int> UpdateAsync(Tdto row, CancellationToken token)
        {
            var entity = Convert(row);
            db.Set<TEntity>().Update(entity);
            return await db.SaveChangesAsync(token)
                .ConfigureAwait(false);
        }

        private static Tdto Convert(TEntity entity, double offsetHours)
        {
            var dto = entity.Adapt<Tdto>();
            dto.UpdateDate = entity.UpdateDate.ToOffset(TimeSpan.FromHours(offsetHours));
            return dto;
        }

        private static TEntity Convert(Tdto dto)
        {
            var entity = dto.Adapt<TEntity>();
            entity.UpdateDate = DateTimeOffset.UtcNow;
            return entity;
        }
    }
}