using C4IT.DataHistoryProvider;
using C4IT.FASD.Base;
using C4IT.Logging;
using System;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;
using static C4IT.Logging.cLogManager;
namespace F4SDwebService.Controllers
{
    public class QuickActionDataController : ApiController
    {

        [HttpPost]
        [Route("api/QuickAction/Run")]
        public async Task<IHttpActionResult> QuickActionRun([FromBody] cF4SDServerQuickActionParameters jsonRequest)
        {
            MethodBase CM = null; if (cLogManager.DefaultLogger.IsDebug) { CM = MethodBase.GetCurrentMethod(); LogMethodBegin(CM); }
            var requestInfo = new cF4sdWebRequestInfo(jsonRequest.Action, jsonRequest.ParameterType);
            if (cPerformanceLogger.IsActive && requestInfo != null) { cPerformanceLogger.LogPerformanceStart(0, requestInfo.requestName, requestInfo.id, requestInfo.created); }
            var apiError = 0;

            try
            {
                if (jsonRequest.Category == "Intune")
                {
                    var result = await WebApiApplication.Collector.ActiveDirectory.QuickActionRun(jsonRequest, requestInfo, CancellationToken.None);

                    if (result is null)
                        return Ok();

                    return Ok(result);
                }
                if (jsonRequest.Category == "Citrix")
                {
                    var result = await WebApiApplication.Collector.CitrixCollector.QuickActionRun(jsonRequest, requestInfo, CancellationToken.None);

                    if (result is null)
                        return Ok();

                    return Ok(result);
                }
                else
                    return Ok();
            }
            catch (Exception E)
            {
                apiError = E.HResult;
                LogException(E);
                return BadRequest();
            }
            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);
            }
        }


        [HttpGet]
        [Route("api/QuickAction/GetQuickActionList")]
        public IHttpActionResult GetQuickActionList()
        {
            MethodBase CM = null; if (cLogManager.DefaultLogger.IsDebug) { CM = MethodBase.GetCurrentMethod(); LogMethodBegin(CM); }
            var requestInfo = new cF4sdWebRequestInfo("GetQuickActionList", null);
            if (cPerformanceLogger.IsActive && requestInfo != null) { cPerformanceLogger.LogPerformanceStart(0, requestInfo.requestName, requestInfo.id, requestInfo.created); }
            var apiError = 0;

            try
            {
                var _quickActions = WebApiApplication.Collector.GetQuickActionAllList();

                return Ok(_quickActions.Result);
            }
            catch (Exception E)
            {
                apiError = E.HResult;
                LogException(E);
                return BadRequest();
            }
            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);
            }
        }


    }
}
