using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Results;

using C4IT.FASD.Base;

using C4IT_DataHistoryProvider_Base.DataSources;
using C4IT_DataHistoryProvider_Base.Services;

using static C4IT.Logging.cLogManager;

namespace F4SDwebService.Controllers
{
    public class StagedSearchRelationController : ApiController
    {
        private IStagedRelationService _relationService = null;

        public StagedSearchRelationController()
        {
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        private bool checkRelationService()
        {
            if (_relationService == null && WebApiApplication.Collector != null)
                _relationService = WebApiApplication.Collector.GetStagedRelationService();

            return (_relationService != null);
        }

        [HttpPost]
        [Route("api/StagedSearchRelations")]
        public async Task<IHttpActionResult> StartGatheringRelatedInformationObjects([FromBody] IEnumerable<cFasdApiSearchResultEntry> relatedTo, [FromUri] int age)
        {
            MethodBase CM = null; if (DefaultLogger.IsDebug) { CM = MethodBase.GetCurrentMethod(); LogMethodBegin(CM); }

            try
            {
                if (!checkRelationService())
                    return NotFound();

                cF4sdStagedSearchResultRelationTaskId taskId = await _relationService.StartGatheringRelatedObjects(relatedTo, age);
                if (taskId.Id == Guid.Empty)
                    return BadRequest();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Accepted, taskId);
                return new ResponseMessageResult(response);
            }
            catch (Exception ex)
            {
                LogException(ex);
            }
            finally { LogMethodEnd(CM); }

            return InternalServerError();
        }

        [HttpGet]
        [Route("api/StagedSearchRelations/{id}")]
        public async Task<IHttpActionResult> GetRelatedInformationObjects([FromUri] Guid id, CancellationToken token)
        {
            MethodBase CM = null; if (DefaultLogger.IsDebug) { CM = MethodBase.GetCurrentMethod(); LogMethodBegin(CM); }
            try
            {
                if (!checkRelationService())
                    return NotFound();

                return Ok(await _relationService.GetRelatedObjectsAsync(id, token));
            }
            catch (Exception ex)
            {
                LogException(ex);
            }
            finally { LogMethodEnd(CM); }

            return InternalServerError();
        }

        [HttpPost]
        [Route("api/StagedSearchRelations/{id}/stop")]
        public IHttpActionResult StopGatheringRelatedInformationObjects([FromUri] Guid id)
        {
            MethodBase CM = null; if (DefaultLogger.IsDebug) { CM = MethodBase.GetCurrentMethod(); LogMethodBegin(CM); }
            try
            {
                if (!checkRelationService())
                    return NotFound();

                _relationService.StopGatheringRelatedObjects(id);
                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Accepted);
                return new ResponseMessageResult(response);
            }
            catch (Exception ex)
            {
                LogException(ex);
            }
            finally { LogMethodEnd(CM); }

            return InternalServerError();
        }
    }
}
