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

namespace F4SDwebService.Controllers
{
    public class SearchDefaultController : ApiController
    {
        public async Task<IHttpActionResult> Get(string search)
        {
            MethodBase CM = null; if (cLogManager.DefaultLogger.IsDebug) { CM = MethodBase.GetCurrentMethod(); LogMethodBegin(CM); }

            var requestInfo = new cF4sdWebRequestInfo("SearchDefault", search);
            if (cPerformanceLogger.IsActive && requestInfo != null) { cPerformanceLogger.LogPerformanceStart(0, requestInfo.requestName, requestInfo.id, requestInfo.created); }
            var apiError = 0;

            try
            {
                if (WebApiApplication.Collector == null)
                    return null;

                var output = await WebApiApplication.Collector.DefaultSearchAsync(search, CancellationToken.None, requestInfo, 1);

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

                apiError = (int)HttpStatusCode.BadRequest;
            }
            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/SearchDetailed")]
        public async Task<IHttpActionResult> GetDetailed(string search)
        {
            MethodBase CM = null; if (cLogManager.DefaultLogger.IsDebug) { CM = MethodBase.GetCurrentMethod(); LogMethodBegin(CM); }

            var requestInfo = new cF4sdWebRequestInfo("SearchDetailed", search);
            if (cPerformanceLogger.IsActive && requestInfo != null) { cPerformanceLogger.LogPerformanceStart(0, requestInfo.requestName, requestInfo.id, requestInfo.created); }

            var apiError = 0;

            try
            {
                if (WebApiApplication.Collector == null)
                    return null;

                var output = await WebApiApplication.Collector.DetailedSearchAsync(search, CancellationToken.None, requestInfo, 1);

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

                apiError = (int)HttpStatusCode.BadRequest;
            }
            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/SearchDetailedStart")]
        public IHttpActionResult GetDetailedStart(string search)
        {
            MethodBase CM = null; if (cLogManager.DefaultLogger.IsDebug) { CM = MethodBase.GetCurrentMethod(); LogMethodBegin(CM); }

            var requestInfo = new cF4sdWebRequestInfo("SearchDetailedStart", search);
            if (cPerformanceLogger.IsActive && requestInfo != null) { cPerformanceLogger.LogPerformanceStart(0, requestInfo.requestName, requestInfo.id, requestInfo.created); }

            var apiError = 0;

            try
            {
                if (WebApiApplication.Collector == null)
                    return null;

                var output = WebApiApplication.Collector.DetailedSearchStart(search, requestInfo, 1);

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

                apiError = (int)HttpStatusCode.BadRequest;
            }
            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/SearchDetailedResult")]
        public async Task<IHttpActionResult> GetDetailedResult(Guid taskID)
        {
            MethodBase CM = null; if (cLogManager.DefaultLogger.IsDebug) { CM = MethodBase.GetCurrentMethod(); LogMethodBegin(CM); }

            var requestInfo = new cF4sdWebRequestInfo("SearchDetailedResult", taskID.ToString());
            if (cPerformanceLogger.IsActive && requestInfo != null) { cPerformanceLogger.LogPerformanceStart(0, requestInfo.requestName, requestInfo.id, requestInfo.created); }

            var apiError = 0;

            try
            {
                if (WebApiApplication.Collector == null)
                    return null;

                var output = await cDataHistoryCollectorSearchTaskCache.GetResultAsync(taskID);

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

                apiError = (int)HttpStatusCode.BadRequest;
            }
            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/SearchDetailedStop")]
        public IHttpActionResult GetDetailedStop(Guid taskID)
        {
            MethodBase CM = null; if (cLogManager.DefaultLogger.IsDebug) { CM = MethodBase.GetCurrentMethod(); LogMethodBegin(CM); }

            var requestInfo = new cF4sdWebRequestInfo("SearchDetailedStop", taskID.ToString());
            if (cPerformanceLogger.IsActive && requestInfo != null) { cPerformanceLogger.LogPerformanceStart(0, requestInfo.requestName, requestInfo.id, requestInfo.created); }

            var apiError = 0;

            try
            {
                if (WebApiApplication.Collector == null)
                    return null;

                cDataHistoryCollectorSearchTaskCache.StopTask(taskID);

                return Ok();
            }
            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/SearchPhone")]
        public async Task<IHttpActionResult> GetPhone(string searchPhone, string searchName)
        {
            var requestInfo = new cF4sdWebRequestInfo("SearchPhone", searchPhone);
            if (cPerformanceLogger.IsActive && requestInfo != null) { cPerformanceLogger.LogPerformanceStart(0, requestInfo.requestName, requestInfo.id, requestInfo.created); }

            var apiError = 0;

            MethodBase CM = null; if (cLogManager.DefaultLogger.IsDebug) { CM = MethodBase.GetCurrentMethod(); LogMethodBegin(CM); }
            try
            {
                if (WebApiApplication.Collector == null)
                    return null;

                var output = await WebApiApplication.Collector.PhoneSearchAsync(searchPhone, searchName, CancellationToken.None, requestInfo, 1);

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

                apiError = (int)HttpStatusCode.BadRequest;
            }
            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/SearchComputerByName")]
        public async Task<IHttpActionResult> GetComputerByName(string Name, string Domain = null)
        {
            MethodBase CM = null; if (cLogManager.DefaultLogger.IsDebug) { CM = MethodBase.GetCurrentMethod(); LogMethodBegin(CM); }

            var requestInfo = new cF4sdWebRequestInfo("SearchComputerByName", Name);
            if (cPerformanceLogger.IsActive && requestInfo != null) { cPerformanceLogger.LogPerformanceStart(0, requestInfo.requestName, requestInfo.id, requestInfo.created); }

            var apiError = 0;

            try
            {
                if (WebApiApplication.Collector == null)
                    return NotFound();

                var output = await WebApiApplication.Collector.ComputerSearchByNameAsync(Name, Domain, CancellationToken.None, requestInfo, 1);

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

                apiError = (int)HttpStatusCode.BadRequest;
            }
            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/SearchUserByNameAndSids")]
        public async Task<IHttpActionResult> GetUserByNameAndSids(string Name, string SIDs)
        {
            MethodBase CM = null; if (cLogManager.DefaultLogger.IsDebug) { CM = MethodBase.GetCurrentMethod(); LogMethodBegin(CM); }

            var requestInfo = new cF4sdWebRequestInfo("SearchUserByNameAndSids", Name);
            if (cPerformanceLogger.IsActive && requestInfo != null) { cPerformanceLogger.LogPerformanceStart(0, requestInfo.requestName, requestInfo.id, requestInfo.created); }

            var apiError = 0;

            try
            {
                if (WebApiApplication.Collector == null)
                    return null;

                var lstSids = new List<string>();
                if (!string.IsNullOrWhiteSpace(SIDs))
                {
                    var arrSids = SIDs.Split(',');
                    foreach (var Entry in arrSids)
                        lstSids.Add(Entry.Trim());
                }

                var output = await WebApiApplication.Collector.UserSearchByNameAndSidsAsync(Name, lstSids, CancellationToken.None, requestInfo, 1);

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

                apiError = (int)HttpStatusCode.BadRequest;
            }
            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();
        }

    }
}