using AsbCloudApp.Data;
using AsbCloudApp.Data.WITS;
using AsbCloudApp.Services;
using AsbCloudWebApi.SignalR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace AsbCloudWebApi.Controllers.WITS
{
    [Route("api/[controller]")]
    [ApiController]
    public abstract class WitsControllerAbstract<TDto> : ControllerBase
        where TDto : ITelemetryData
    {
        private readonly ITelemetryService telemetryService;
        private readonly IHubContext<TelemetryHub> telemetryHubContext;
        protected abstract string SirnalRMethodGetDataName { get; }

        public WitsControllerAbstract(
            ITelemetryService telemetryService,
            IHubContext<TelemetryHub> telemetryHubContext)
        {
            this.telemetryService = telemetryService;
            this.telemetryHubContext = telemetryHubContext;
        }

        [HttpPost("{uid}")]
        [AllowAnonymous]
        public async virtual Task<IActionResult> PostDataAsync(
            string uid,
            [FromBody] IEnumerable<TDto> dtos,
            [FromServices] IWitsRecordRepository<TDto> witsRecordRepository,
            CancellationToken token = default)
        {
            var idTelemetry = telemetryService.GetOrCreateTelemetryIdByUid(uid);
            await witsRecordRepository.SaveDataAsync(idTelemetry, dtos, token).ConfigureAwait(false);
            var idWell = telemetryService.GetIdWellByTelemetryUid(uid);
            if (idWell != null && dtos.Any())
                _ = Task.Run(() => telemetryHubContext.Clients.Group($"well_{idWell}_wits")
                      .SendAsync(SirnalRMethodGetDataName, dtos), CancellationToken.None);

            return Ok();
        }

        [HttpGet("{idWell}")]
        [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(IEnumerable<RecordBaseDto>))]
        public async virtual Task<IActionResult> GetDataAsync(
            int idWell,
            DateTime begin,
            DateTime end,
            [FromServices] IWitsRecordRepository<TDto> witsRecordRepository,
            CancellationToken token)
        {
            var idTelemetry = telemetryService.GetIdTelemetryByIdWell(idWell);
            if (idTelemetry is null)
                return NoContent();
            var dtos = await witsRecordRepository.GetAsync((int)idTelemetry, begin, end, token);
            return Ok(dtos);
        }

        [HttpGet("{idWell}/datesRange")]
        [ProducesResponseType(StatusCodes.Status200OK, Type = typeof((DateTime begin, DateTime end, int count)?))]
        public async virtual Task<IActionResult> GetDatesRangeAsync(
            int idWell,
            [FromServices] IWitsRecordRepository<TDto> witsRecordRepository,
            CancellationToken token)
        {
            var idTelemetry = telemetryService.GetIdTelemetryByIdWell(idWell);
            if (idTelemetry is null)
                return NoContent();
            var dtos = await witsRecordRepository.GetStatAsync((int)idTelemetry, token);
            return Ok(dtos);
        }
    }
}