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

namespace AsbCloudInfrastructure.Services.PlannedTrajectory
{
#nullable enable
    public class PlannedTrajectoryService : IPlannedTrajectoryService
    {
        private readonly IAsbCloudDbContext db;
        private readonly IWellService wellService;
        public PlannedTrajectoryService(IAsbCloudDbContext db, IWellService wellService)
        {
            this.db = db;
            this.wellService = wellService;
        }
        /// <inheritdoc/>
        public async Task<int> AddRangeAsync(IEnumerable<PlannedTrajectoryDto> plannedTrajectoryRows, CancellationToken token)
        {            
            var idWell = plannedTrajectoryRows.First().IdWell;
            if (!plannedTrajectoryRows.All(r => r.IdWell == idWell))
                throw new ArgumentInvalidException("Все строки должны относиться к одной скважине", nameof(plannedTrajectoryRows));
            var offsetHours = wellService.GetTimezone(idWell).Hours;
            var entities = plannedTrajectoryRows
                .Select(e => {
                    var entity = Convert(e, offsetHours);
                    entity.Id = 0;
                    return entity;});
            
            db.PlannedTrajectories.AddRange(entities);
            return await db.SaveChangesAsync(token)
                .ConfigureAwait(false);
        }

        /// <inheritdoc/>
        public async Task<int> AddAsync(PlannedTrajectoryDto plannedTrajectoryRow, CancellationToken token)
        {
            var offsetHours = wellService.GetTimezone(plannedTrajectoryRow.IdWell).Hours;
            var entity = Convert(plannedTrajectoryRow, offsetHours);
            entity.Id = 0;
            db.PlannedTrajectories.Add(entity);            
            return await db.SaveChangesAsync(token)
                .ConfigureAwait(false);
        }

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

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

        /// <inheritdoc/>
        public async Task<IEnumerable<PlannedTrajectoryDto>> GetAsync(int idWell, CancellationToken token)
        {
            var well = wellService.GetOrDefault(idWell);
            if (well is null || well.Timezone is null)
                throw new ArgumentInvalidException("idWell doesn`t exist", nameof(idWell));
            var offsetHours = well.Timezone.Hours;            
            var query = db.PlannedTrajectories
                .AsNoTracking()
                .Where(x => x.IdWell == idWell);                           
            var entities = await query
                .OrderBy(e => e.WellboreDepth)
                .ToListAsync(token);            
            var result = entities
                .Select(r => Convert(r, offsetHours));
            return result;
        }

        /// <inheritdoc/>
        public async Task<int> UpdateAsync(PlannedTrajectoryDto row, CancellationToken token)
        {
            var offsetHours = wellService.GetTimezone(row.IdWell).Hours;
            var entity = Convert(row, offsetHours);
            db.PlannedTrajectories.Update(entity);
            return await db.SaveChangesAsync(token)
                .ConfigureAwait(false);
        }

        private PlannedTrajectoryDto Convert(AsbCloudDb.Model.PlannedTrajectory entity, double offsetHours)
        {            
            var dto = entity.Adapt<PlannedTrajectoryDto>(); 
            dto.UpdateDate = entity.UpdateDate.ToRemoteDateTime(offsetHours);
            return dto;
        }

        private AsbCloudDb.Model.PlannedTrajectory Convert(PlannedTrajectoryDto dto, double offsetHours)
        {            
            var entity = dto.Adapt<AsbCloudDb.Model.PlannedTrajectory>();
            entity.UpdateDate = DateTime.Now.ToUtcDateTimeOffset(offsetHours);
            return entity;
        }       
    }
#nullable disable
}