using C4IT.DataHistoryProvider;
using C4IT.FASD.Base;
using C4IT.Logging;
using F4SDwebService;
using System;
using System.Net;
using System.Reflection;
using System.Web.Http;
using static C4IT.Logging.cLogManager;

namespace FasdWebService.Controllers
{
    public class GetConfigurationController : ApiController
    {
        public IHttpActionResult Get(string configType)
        {
            MethodBase CM = null; if (cLogManager.DefaultLogger.IsDebug) { CM = MethodBase.GetCurrentMethod(); LogMethodBegin(CM); }

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

            try
            {
                if (!Enum.TryParse(configType, true, out enumFasdConfigurationType configurationType) || configurationType == enumFasdConfigurationType.unknown)
                {
                    LogEntry($"Couldn't find a configuration with name '{configType}'");
                    apiError = (int)HttpStatusCode.NotFound;
                    return NotFound();
                }

                var output = cConfigCache.Get(configurationType);

                if (output != null)
                    return Ok(output.Content);

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

            LogEntry("Bad request happended when calling GetConfiguration Controller.");
            return BadRequest();
        }

        [Route("api/GetCockpitConfiguration")]
        public IHttpActionResult GetCockpitConfiguration()
        {
            MethodBase CM = null; if (cLogManager.DefaultLogger.IsDebug) { CM = MethodBase.GetCurrentMethod(); LogMethodBegin(CM); }

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

            try
            {
                var retVal = WebApiApplication.Collector.GetCockpitConfiguration(requestInfo);
                if (retVal != null)
                    return Ok(retVal);

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

            return BadRequest();
        }

        [Route("api/CheckCollectorStatus")]
        public IHttpActionResult GetCollectorStatus()
        {
            MethodBase CM = null; if (cLogManager.DefaultLogger.IsDebug) { CM = MethodBase.GetCurrentMethod(); LogMethodBegin(CM); }

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

            try
            {
                var retVal = WebApiApplication.Collector.CheckCollectorStatus(requestInfo);
                if (retVal != null)
                    return Ok(retVal);

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

            return BadRequest();
        }
    }

}