// Copyright © Selmo Technology GmbH. All Rights Reserved

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using SelmoStudio.Hmi.Server.Db.Authentication;
using SelmoStudio.Hmi.Server.TestSetup.Services;
using System.Collections.Generic;
using System.Diagnostics;

namespace SelmoStudio.Hmi.Server.Api
{
    [Route("api/[controller]")]
    [ApiController]
    public class TestSetupsController : ControllerBase
    {
        private readonly IProcessDataDbTestSetupService _processDataDbTestSetupService;

#if DEBUG

        /*
        This Controller was created for TEST Reasons.
        Therefore it should only be available in the DEBUG BUILD and --> N O T <--
        in the RELEASE BUILD !!!!!!!!!!!!!!!
        */

        private const int TestInserValues = 1000;

        public TestSetupsController(IProcessDataDbTestSetupService processDataDbTestSetupService)
        {
            _processDataDbTestSetupService = processDataDbTestSetupService;
        }

        [Authorize(Policy = "ReadHistoryValue")]
        [Authorize(Policy = "WriteHistoryValue")]
        [HttpPost("setupTestProcessValueTables")]
        [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(string))]
        [ProducesResponseType(StatusCodes.Status500InternalServerError)]
        [ProducesResponseType(StatusCodes.Status400BadRequest)]
        public async Task<string> Post([FromBody] List<Guid> values)
        {
            var principal = this.User;
            //principal.AddIdentity()

            Stopwatch stopwatch = Stopwatch.StartNew();

            await _processDataDbTestSetupService.CreateProcessValueTableSetup(TestInserValues, values);

            stopwatch.Stop();

            var info = $"Create table + Insert {TestInserValues} process value rows ({values.Count()} columns). " +
                $"Execution Time = {stopwatch.ElapsedMilliseconds} [ms]";

            return info;
        }

        /*
        [AllowAnonymous]
        [HttpPost("getTestProcessValues")]
        [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(string))]
        [ProducesResponseType(StatusCodes.Status500InternalServerError)]
        [ProducesResponseType(StatusCodes.Status400BadRequest)]
        public Task<ProcessValuesGetResponse> Get([FromBody] ProcessValuesGetRequest request)
        {
            var startString = request.From.ToString("O");
            var endString = request.To.ToString("O");
            ProcessValuesGetResponse response = new ProcessValuesGetResponse();

            Stopwatch stopwatch = Stopwatch.StartNew();

            var resultValues = _processDataDbTestSetupService.GetProcessValues(request.ValueIdsAsGuid);

            stopwatch.Stop();

            var info = $"Read {TestInserValues} process value rows ({request.ValueIdsAsGuid.Count()} columns). " +
                $"Execution Time = {stopwatch.ElapsedMilliseconds} [ms]";

            response.From = request.From;
            response.To = request.To;
            response.ValueIdsAsGuid = request.ValueIdsAsGuid;
            response.ResultValueRows = ConvertValues(resultValues);
            response.ResultMessage = info;

            return Task.FromResult(response);
        }

        private List<ProcessValuesGetResponseResultRow> ConvertValues(
            List<(DateTimeOffset timestamp, float[] values)> resultValues)
        {
            List<ProcessValuesGetResponseResultRow> converted = new();
            foreach (var value in resultValues)
            {
                ProcessValuesGetResponseResultRow val = new();
                val.Timestamp = value.timestamp;
                val.ResultValues = value.values;
                converted.Add(val);
            }
            return converted;
        }
        */
#endif
    }
}