using C4IT.DataHistoryProvider;
using C4IT.FASD.Base;
using C4IT.Logging;
using Newtonsoft.Json;
using System;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;
using static C4IT.Logging.cLogManager;

namespace F4SDwebService.Controllers
{
    public class GetRawDataController : ApiController
    {
        public async Task<IHttpActionResult> Get(string jsonRequest)
        {
            MethodBase CM = null; if (cLogManager.DefaultLogger.IsDebug) { CM = MethodBase.GetCurrentMethod(); LogMethodBegin(CM); }
            try
            {
                var Request = JsonConvert.DeserializeObject<cF4sdHealthCardRawDataRequest>(jsonRequest);
                var requestInfo = new cF4sdWebRequestInfo("GetRawData", Request.Identities?.First()?.Id.ToString(), cAuthentication.GetUserInfo(ActionContext));
                if (cPerformanceLogger.IsActive && requestInfo != null) { cPerformanceLogger.LogPerformanceStart(0, requestInfo.requestName, requestInfo.id, requestInfo.created); }
                var apiError = 0;

                try
                {
                    var RetVal = await WebApiApplication.Collector.GetTableResults(Request.Tables, Request.Identities, Request.RefTime, Request.MaxAge, true, CancellationToken.None, requestInfo, 1).ConfigureAwait(false);

                    if (RetVal != null)
                        return Ok(RetVal);
                    return Ok(new cF4SDHealthCardRawData());
                }
                catch (Exception E)
                {
                    apiError = E.HResult;
                    LogException(E);
                }
                finally
                {
                    if (WebApiApplication.Debug_apiTiming) WebApiApplication.SaveApiTimingEntry(requestInfo.requestName, requestInfo.id, requestInfo.created, apiError);
                    if (cPerformanceLogger.IsActive && requestInfo != null) { cPerformanceLogger.LogPerformanceEnd(0, requestInfo.requestName, requestInfo.id, requestInfo.created, requestInfo.created, ErrorCode: apiError); }
                }
            }
            catch (Exception E)
            {
                LogException(E);
            }
            finally
            {
                if (CM != null) LogMethodEnd(CM);
            }

            return BadRequest();
        }

        [Route("api/GetRawDataStart")]
        public async Task<IHttpActionResult> GetWithSuccsession(string jsonRequest)
        {
            MethodBase CM = null; if (cLogManager.DefaultLogger.IsDebug) { CM = MethodBase.GetCurrentMethod(); LogMethodBegin(CM); }
            try
            {
                var Request = JsonConvert.DeserializeObject<cF4sdHealthCardRawDataRequest>(jsonRequest);

                var requestInfo = new cF4sdWebRequestInfo("GetRawDataStart", Request.Identities?.First()?.Id.ToString(), cAuthentication.GetUserInfo(ActionContext));
                if (cPerformanceLogger.IsActive && requestInfo != null) { cPerformanceLogger.LogPerformanceStart(0, requestInfo.requestName, requestInfo.id, requestInfo.created); }

                var apiError = 0;

                try
                {
                    var RetVal = await WebApiApplication.Collector.GetTableResults(Request.Tables, Request.Identities, Request.RefTime, Request.MaxAge, false, CancellationToken.None, requestInfo, 1).ConfigureAwait(false);


                    if (RetVal != null)
                        return Ok(RetVal);

                    return Ok(new cF4SDHealthCardRawData());
                }
                catch (Exception E)
                {
                    apiError = E.HResult;
                    LogException(E);
                }
                finally
                {
                    if (WebApiApplication.Debug_apiTiming) WebApiApplication.SaveApiTimingEntry(requestInfo.requestName, requestInfo.id, requestInfo.created, apiError);
                    if (cPerformanceLogger.IsActive && requestInfo != null) { cPerformanceLogger.LogPerformanceEnd(0, requestInfo.requestName, requestInfo.id, requestInfo.created, requestInfo.created, ErrorCode: apiError); }
                }
            }
            catch (Exception E)
            {
                LogException(E);
            }
            finally
            {
                if (CM != null) LogMethodEnd(CM);
            }

            return BadRequest();
        }

        [Route("api/GetRawDataNext")]
        public async Task<IHttpActionResult> GetNext(Guid RequestId)
        {
            MethodBase CM = null; if (cLogManager.DefaultLogger.IsDebug) { CM = MethodBase.GetCurrentMethod(); LogMethodBegin(CM); }
            try
            {
                var requestInfo = new cF4sdWebRequestInfo("GetRawDataNext", RequestId.ToString(), cAuthentication.GetUserInfo(ActionContext));
                if (cPerformanceLogger.IsActive && requestInfo != null) { cPerformanceLogger.LogPerformanceStart(0, requestInfo.requestName, requestInfo.id, requestInfo.created); }

                var apiError = 0;

                try
                {
                    var res = await SuccessorCache.GetNextResult(WebApiApplication.Collector, RequestId, false, requestInfo, 1);
                    return Ok(res);
                }
                catch (Exception E)
                {
                    apiError = E.HResult;
                    LogException(E);
                }
                finally
                {
                    if (WebApiApplication.Debug_apiTiming) WebApiApplication.SaveApiTimingEntry(requestInfo.requestName, requestInfo.id, requestInfo.created, apiError);
                    if (cPerformanceLogger.IsActive && requestInfo != null) { cPerformanceLogger.LogPerformanceEnd(0, requestInfo.requestName, requestInfo.id, requestInfo.created, requestInfo.created, ErrorCode: apiError); }
                }
            }
            catch (Exception E)
            {
                LogException(E);
            }
            finally
            {
                if (CM != null) LogMethodEnd(CM);
            }

            return BadRequest();
        }

    }
}