using C4IT.DataHistoryProvider;
using C4IT.FASD.Base;
using C4IT.Logging;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;
using static C4IT.Logging.cLogManager;

namespace F4SDwebService.Controllers
{
    public class GetUsageController : ApiController
    {
        public async Task<IHttpActionResult> Get(enumF4sdSearchResultClass Class, Guid ClassId, int Age)
        {
            MethodBase CM = null; if (cLogManager.DefaultLogger.IsDebug) { CM = MethodBase.GetCurrentMethod(); LogMethodBegin(CM); }

            var requestInfo = new cF4sdWebRequestInfo("GetUsage", ClassId.ToString(), cAuthentication.GetUserInfo(ActionContext));
            if (cPerformanceLogger.IsActive && requestInfo != null) { cPerformanceLogger.LogPerformanceStart(0, requestInfo.requestName, requestInfo.id, requestInfo.created); }

            var apiError = 0;

            try
            {

                List<cF4sdApiSearchResultRelation> RetVal = null;


                var infoClass = cF4sdIdentityEntry.GetFromSearchResult(Class);
                if (infoClass != enumFasdInformationClass.Unknown)
                {
                    RetVal = await WebApiApplication.Collector.GetUsageFromAgentData(infoClass, new List<Guid>() { ClassId }, Age, CancellationToken.None, requestInfo, 1);
                    return Ok(RetVal);
                }

                apiError = 404;
                return NotFound();
            }
            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); }
                if (CM != null) LogMethodEnd(CM);
            }

            return BadRequest();
        }

        [Route("api/GetUsageDetailed")]
        public async Task<IHttpActionResult> GetDetailed(enumF4sdSearchResultClass Class, string ClassIds, int Age)
        {
            MethodBase CM = null; if (cLogManager.DefaultLogger.IsDebug) { CM = MethodBase.GetCurrentMethod(); LogMethodBegin(CM); }

            var requestInfo = new cF4sdWebRequestInfo("GetUsageDetailed", ClassIds.Replace(",", "_"), cAuthentication.GetUserInfo(ActionContext));
            if (cPerformanceLogger.IsActive && requestInfo != null) { cPerformanceLogger.LogPerformanceStart(0, requestInfo.requestName, requestInfo.id, requestInfo.created); }

            var apiError = 0;

            try
            {
                var infoClass = cF4sdIdentityEntry.GetFromSearchResult(Class);
                if (infoClass == enumFasdInformationClass.Unknown)
                    return NotFound();

                var arrIds = ClassIds.Split(',');
                if (arrIds == null || arrIds.Length < 1)
                    return NotFound();

                var lstIds = new List<Guid>();
                foreach (var Entry in arrIds)
                {
                    var strId = Entry.Trim();
                    if (Guid.TryParse(strId, out var Id))
                        lstIds.Add(Id);
                }

                var res = await WebApiApplication.Collector.GetUsageFromAgentData(infoClass, lstIds, Age, CancellationToken.None, 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); }
                if (CM != null) LogMethodEnd(CM);
            }

            return BadRequest();
        }

    }
}