using AsbCloudApp.Data;
using AsbCloudApp.Services;
using AsbCloudWebApi.SignalR;
using Microsoft.AspNetCore.Authorization;
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.SAUB
{
    [ApiController]
    [Authorize]
    [Route("api/[controller]")]
    public abstract class TelemetryDataBaseController<TDto> : ControllerBase
        where TDto : ITelemetryData
    {
        protected readonly IWellService wellService;
        private readonly ITelemetryService telemetryService;
        private readonly ITelemetryDataService<TDto> telemetryDataService;
        private readonly IHubContext<TelemetryHub> telemetryHubContext;

        public string SignalRMethodGetDataName { get; protected set; } = "ReceiveData";

        public TelemetryDataBaseController(
            ITelemetryService telemetryService,
            ITelemetryDataService<TDto> telemetryDataService,
            IWellService wellService,
            IHubContext<TelemetryHub> telemetryHubContext)
        {
            this.telemetryService = telemetryService;
            this.telemetryDataService = telemetryDataService;
            this.wellService = wellService;
            this.telemetryHubContext = telemetryHubContext;
        }

        /// <summary>
        /// Принимает данные от разных систем по скважине
        /// </summary>
        /// <param name="uid">Уникальный идентификатор отправителя</param>
        /// <param name="dtos">Данные</param>
        /// <param name="token">Токен для отмены задачи</param>
        /// <returns></returns>
        [HttpPost("{uid}")]
        [AllowAnonymous]
        public virtual async Task<IActionResult> PostDataAsync(string uid, [FromBody] IEnumerable<TDto> dtos,
            CancellationToken token)
        {
            await telemetryDataService.UpdateDataAsync(uid, dtos, token).ConfigureAwait(false);

            var idWell = telemetryService.GetIdWellByTelemetryUid(uid);
            if (idWell is not null && dtos.Any())
                _ = Task.Run(() => telemetryHubContext.Clients.Group($"well_{idWell}")
                      .SendAsync(SignalRMethodGetDataName, dtos), CancellationToken.None);

            return Ok();
        }

        /// <summary>
        /// Возвращает данные САУБ по скважине.
        /// По умолчанию за последние 10 минут.
        /// </summary>
        /// <param name="idWell">id скважины</param>
        /// <param name="begin">дата начала выборки. По умолчанию: текущее время - intervalSec</param>
        /// <param name="intervalSec">интервал времени даты начала выборки, секунды</param>
        /// <param name="approxPointsCount">желаемое количество точек. Если в выборке точек будет больше, то выборка будет прорежена.</param>
        /// <param name="token">Токен завершения задачи</param>
        /// <returns></returns>
        [HttpGet("{idWell}")]
        [Permission]
        public virtual async Task<ActionResult<IEnumerable<TDto>>> GetDataAsync(int idWell,
            DateTime begin = default,
            int intervalSec = 600, 
            int approxPointsCount = 1024,
            //TODO: сделать cancellationToken обязательным 
            CancellationToken token = default)
        {
            int? idCompany = User.GetCompanyId();

            if (idCompany is null)
                return Forbid();

            bool isCompanyOwnsWell = await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
                idWell, token).ConfigureAwait(false);

            if (!isCompanyOwnsWell)
                return Forbid();

            var content = await telemetryDataService.GetAsync(idWell, begin,
                intervalSec, approxPointsCount, token).ConfigureAwait(false);

            return Ok(content);
        }

        /// <summary>
        /// Возвращает диапазон дат сохраненных данных.
        /// </summary>
        /// <param name="idWell">id скважины</param>
        /// <param name="token">Токен завершения задачи</param>
        /// <returns></returns>
        [HttpGet("{idWell}/datesRange")]
        [Permission]
        [ProducesResponseType(typeof(DatesRangeDto), (int)System.Net.HttpStatusCode.OK)]
        public virtual async Task<ActionResult<DatesRangeDto>> GetDataDatesRangeAsync(int idWell,
            CancellationToken token)
        {
            int? idCompany = User.GetCompanyId();

            if (idCompany is null)
                return Forbid();

            bool isCompanyOwnsWell = await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
                idWell, token).ConfigureAwait(false);

            if (!isCompanyOwnsWell)
                return Forbid();
            try
            {
                var dataDatesRange = wellService.GetDatesRange(idWell);
                return Ok(dataDatesRange);
            }
            catch(KeyNotFoundException)
            {
                return NoContent();
            }
        }
    }
}