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

namespace AsbCloudInfrastructure.Services
{
    public class MeasureService : IMeasureService
    {
        private readonly IAsbCloudDbContext db;
        private readonly IWellService wellService;
        private readonly CacheTable<MeasureCategory> cacheCategories;

        public MeasureService(IAsbCloudDbContext db, Cache.CacheDb cacheDb, IWellService wellService)
        {
            this.db = db;
            this.wellService = wellService;
            cacheCategories = cacheDb.GetCachedTable<MeasureCategory>((DbContext)db);
        }

        public async Task<Dictionary<int, string>> GetCategoriesAsync(CancellationToken token)
        {
            var entities = await cacheCategories.WhereAsync(token).ConfigureAwait(false);
            var dto = entities.ToDictionary(e => e.Id, e => e.Name);
            return dto;
        }

        public async Task<MeasureDto> GetLastAsync(int idWell, int idCategory, CancellationToken token)
        {
            var query = db.Measures
                .Include(m => m.Category)
                .Where(m => m.IdWell == idWell && m.IdCategory == idCategory && !m.IsDeleted)
                .OrderByDescending(m => m.Timestamp)
                .Take(1);

            var entity = await query
                .AsNoTracking()
                .FirstOrDefaultAsync(token)
                .ConfigureAwait(false);

            var timezone = wellService.GetTimezone(idWell);
            var dto = entity?.Adapt<MeasureDto, Measure>((d, s) =>
            {
                d.CategoryName = s.Category?.Name;
                d.Timestamp = s.Timestamp.ToRemoteDateTime(timezone.Hours);
            });
            return dto;
        }

        public async Task<IEnumerable<MeasureDto>> GetHisoryAsync(int idWell, int? idCategory = null, CancellationToken token = default)
        {
            var query = db.Measures.Include(m => m.Category)
                .Where(m => m.IdWell == idWell && !m.IsDeleted);

            if (idCategory is not null)
                query = query.Where(m => m.IdCategory == idCategory);

            var entities = await query
                .OrderBy(m => m.Timestamp)
                .AsNoTracking()
                .ToListAsync(token)
                .ConfigureAwait(false);

            var timezone = wellService.GetTimezone(idWell);
            var dtos = entities.Adapt<MeasureDto, Measure>((d, s) =>
            {
                d.CategoryName = s.Category?.Name;
                d.Timestamp = s.Timestamp.ToRemoteDateTime(timezone.Hours);
            });
            return dtos;
        }

        public Task<int> InsertAsync(int idWell, MeasureDto dto, CancellationToken token)
        {
            if (dto.IdCategory < 1)
                throw new ArgumentInvalidException("wrong idCategory", nameof(dto));
            if (dto.Data is null)
                throw new ArgumentInvalidException("data.data is not optional", nameof(dto));
            var timezone = wellService.GetTimezone(idWell);
            var entity = dto.Adapt<Measure>();
            entity.IdWell = idWell;
            entity.Timestamp = dto.Timestamp.ToUtcDateTimeOffset(timezone.Hours);
            db.Measures.Add(entity);
            return db.SaveChangesAsync(token);
        }

        public async Task<int> UpdateAsync(int idWell, MeasureDto dto, CancellationToken token)
        {
            if (dto.Id < 1)
                throw new ArgumentInvalidException("wrong id", nameof(dto));
            if (dto.IdCategory < 1)
                throw new ArgumentInvalidException("wrong idCategory", nameof(dto));
            if (dto.Data is null)
                throw new ArgumentInvalidException("data.data is not optional", nameof(dto));

            var entity = await db.Measures
                .Where(m => m.Id == dto.Id && !m.IsDeleted)
                .FirstOrDefaultAsync(token)
                .ConfigureAwait(false);

            if (entity is null)
                throw new ArgumentInvalidException("id doesn't exist", nameof(dto));

            var timezone = wellService.GetTimezone(idWell);

            entity.IdWell = idWell;
            entity.Timestamp = dto.Timestamp.ToUtcDateTimeOffset(timezone.Hours);
            entity.Data = (RawData)dto.Data;

            return await db.SaveChangesAsync(token).ConfigureAwait(false);
        }

        public async Task<int> MarkAsDeleteAsync(int idWell, int idData, CancellationToken token)
        {
            if (idData < 1)
                throw new ArgumentInvalidException("wrong id", nameof(idData));

            var entity = await db.Measures.Where(m => m.IdWell == idWell && m.Id == idData)
                .FirstOrDefaultAsync(token)
                .ConfigureAwait(false);

            entity.IsDeleted = true;

            return await db.SaveChangesAsync(token).ConfigureAwait(false);
        }

        public Task<int> DeleteAsync(int idWell, int idData, CancellationToken token)
        {
            if (idData < 1)
                throw new ArgumentInvalidException("wrong id", nameof(idData));
            db.Measures.RemoveRange(db.Measures.Where(m => m.IdWell == idWell && m.Id == idData));
            return db.SaveChangesAsync(token);
        }
    }
}