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 GetSelectionDataController : ApiController
    {
        [Route("api/GetSelectionCount")]
        public async Task<IHttpActionResult> GetCount(string jsonRequest)
        {
            MethodBase CM = null; if (cLogManager.DefaultLogger.IsDebug) { CM = MethodBase.GetCurrentMethod(); LogMethodBegin(CM); }

            try
            {
                var Request = JsonConvert.DeserializeObject<cF4sdHealthSelectionDataRequest>(jsonRequest);
                var requestInfo = new cF4sdWebRequestInfo("GetSelectionCount", Request.Identities?.First()?.Id.ToString(), cAuthentication.GetUserInfo(ActionContext));

                var apiError = 0;

                try
                {
                    var RetVal = await WebApiApplication.Collector.GetSelectionTableResultCountAsync(Request.Table, Request.Identities, Request.Search, requestInfo, CancellationToken.None, Request.ResetFilter, Request.FilterParams).ConfigureAwait(false);
                    return Ok(RetVal);
                }
                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/GetSelectionData")]
        public async Task<IHttpActionResult> Get(string jsonRequest)
        {
            MethodBase CM = null; if (cLogManager.DefaultLogger.IsDebug) { CM = MethodBase.GetCurrentMethod(); LogMethodBegin(CM); }
            try
            {
                var Request = JsonConvert.DeserializeObject<cF4sdHealthSelectionDataRequest>(jsonRequest);
                var requestInfo = new cF4sdWebRequestInfo("GetSelectionData", Request.Identities?.First()?.Id.ToString(), cAuthentication.GetUserInfo(ActionContext));

                var apiError = 0;

                try
                {
                    var RetVal = await WebApiApplication.Collector.GetSelectionTableResultAsync(Request.Table, Request.Identities, Request.Search, Request.PageSize, Request.Page, requestInfo, CancellationToken.None, Request.ResetFilter, Request.FilterParams).ConfigureAwait(false);
                    if (RetVal != null)
                        return Ok(RetVal);
                    return Ok();
                }
                catch (Exception E)
                {
                    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();
        }
    }
}