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.Matrix42
{
    public class Matrix42TicketFinalizationController : ApiController
    {
        [HttpPost]
        [Route("api/Matrix42/TicketFinalization")]
        public async Task<IHttpActionResult> Matrix42TicketFinalization([FromBody] cApiM42Ticket jsonRequest)
        {
            MethodBase CM = null; if (cLogManager.DefaultLogger.IsDebug) { CM = MethodBase.GetCurrentMethod(); LogMethodBegin(CM); }
            var requestInfo = new cF4sdWebRequestInfo("Matrix42TicketFinalization", jsonRequest.Action?.ToString() + " + " + jsonRequest.Ticket?.ToString() ?? "", cAuthentication.GetUserInfo(ActionContext));
            if (cPerformanceLogger.IsActive && requestInfo != null) { cPerformanceLogger.LogPerformanceStart(0, requestInfo.requestName, requestInfo.id, requestInfo.created); }
            var apiError = 0;

            try
            {

                if (string.Equals(jsonRequest.StatusIdValue, "Closed", StringComparison.InvariantCultureIgnoreCase))
                    await WebApiApplication.Collector.M42WpmCollector.ReopenM42TicketAsync(jsonRequest, requestInfo, 1, CancellationToken.None);
                switch (jsonRequest.Action)
                {
                    case enumCloseCaseTicketStatus.Save:
                        if (jsonRequest.Ticket == null || jsonRequest.Ticket.HasValue && jsonRequest.Ticket.Equals(Guid.Empty))
                            return Ok(await WebApiApplication.Collector.M42WpmCollector.CreateM42TicketAsync(jsonRequest, requestInfo, 1, CancellationToken.None));
                        else
                            return Ok(await WebApiApplication.Collector.M42WpmCollector.UpdateM42TicketAsync(jsonRequest, requestInfo, 1, CancellationToken.None));
                    case enumCloseCaseTicketStatus.Close:
                        return Ok(await WebApiApplication.Collector.M42WpmCollector.CloseM42TicketAsync(jsonRequest, requestInfo, 1, CancellationToken.None));
                    case enumCloseCaseTicketStatus.OnHold:
                        return Ok(await WebApiApplication.Collector.M42WpmCollector.PauseM42TicketAsync(jsonRequest, requestInfo, 1, CancellationToken.None));
                    case enumCloseCaseTicketStatus.Forward:
                        return Ok(await WebApiApplication.Collector.M42WpmCollector.ForwardM42TicketAsync(jsonRequest, requestInfo, 1, CancellationToken.None));

                    default: break;
                }
                return Ok(jsonRequest);
            }
            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();
        }
    }
}