using System;
using System.Net.Http;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;
using CompControl.ErpConnector.Utility.Models;


namespace CompControl.SapFrameworkConnector
{
    [RoutePrefix("api/standard")]
    public class StandardApiController: ApiController
    {
        private static readonly log4net.ILog _log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        [Route("getbestellungen")]
        [HttpPut]
        public async Task<HttpResponseMessage> GetBestellungen([FromBody] FilterBestellungen filterBestellungen, CancellationToken cancellationToken)
        {
            ApiResultBestellungen result = new ApiResultBestellungen(); //New ApiResult to return via Web
            _log.Debug("GetBestellungen received for Filter: " + filterBestellungen.ToString());

            try
            {
                result = SapStandardFunctions.GetBestellung(filterBestellungen.OrderNo); //Maybe use true? 

                if (result.IsSuccessful) { _log.Debug(result); } //Action was successful => log for debug
                else { _log.Error(result); } //An error occurred
            }
            catch (Exception ex)
            {
                string sm = $"GetBestellungen: An unexpected error occured: " + ex;
                _log.Error(sm);
                result = new ApiResultBestellungen //ApiResult on Exception
                {
                    ErrorNumber = 101,
                    MessageShort = sm,
                    MessageLong = sm,
                    MessageTechnical = $"{sm} [{filterBestellungen}] - {ex}"
                };
            }

            return Request.CreateResponse(HttpStatusCode.OK, result);
        }

        [Route("getherstellerdaten")]
        public async Task<HttpResponseMessage> GetHerstellerdaten([FromUri] string itemNo, CancellationToken cancellationToken)
        {
            ApiResultHerstellerdaten result = new ApiResultHerstellerdaten(); //New ApiResult to return via Web
            _log.Debug("GetHerstellerdaten received for itemNo: " + itemNo);

            try
            {
                result = SapStandardFunctions.GetHerstellerdaten(itemNo);

                if (result.IsSuccessful) { _log.Debug(result); } //Action was successful => log for debug
                else { _log.Error(result); } //An error occurred
            }
            catch (Exception ex)
            {
                string sm = $"GetHerstellerDaten: An unexpected error occured for '{itemNo}' " + ex;
                _log.Error(sm);
                result = new ApiResultHerstellerdaten //Default ApiResult on error
                {
                    ErrorNumber = 101,
                    MessageShort = sm,
                    MessageLong = sm,
                    MessageTechnical = $"{sm} [{itemNo}] - {ex}"
                };
            }

            return Request.CreateResponse(HttpStatusCode.OK, result);
        }

        [Route("getmaterialdaten")]
        public async Task<HttpResponseMessage> GetMaterialdaten([FromUri] string itemNo, CancellationToken cancellationToken)
        {
            ApiResultMaterialdaten result = new ApiResultMaterialdaten(); //New ApiResult to return via Web
            _log.Debug("GetMaterialdaten received for itemNo: " + itemNo);

            try
            {
                result = SapStandardFunctions.GetMaterialdaten(itemNo);

                if (result.IsSuccessful) { _log.Debug(result); } //Action was successful => log for debug
                else { _log.Error(result); } //An error occurred
            }
            catch (Exception ex)
            {
                string sm = $"GetMaterialdaten: An unexpected error occured for '{itemNo}' " + ex;
                _log.Error(sm);
                result = new ApiResultMaterialdaten //Default ApiResult on error
                {
                    ErrorNumber = 101,
                    MessageShort = sm,
                    MessageLong = sm,
                    MessageTechnical = $"{sm} [{itemNo}] - {ex}"
                };
            }

            return Request.CreateResponse(HttpStatusCode.OK, result);
        }

        [Route("getallmaterialdaten")]
        public async Task<HttpResponseMessage> GetAllMaterialdaten(CancellationToken cancellationToken)
        {
            ApiResultMaterialdaten result = new ApiResultMaterialdaten(); //New ApiResult to return via Web
            _log.Debug("GetAllMaterialdaten received ");

            try
            {
                result = SapStandardFunctions.GetMaterialdaten("");

                if (result.IsSuccessful) { _log.Debug(result); } //Action was successful => log for debug
                else { _log.Error(result); } //An error occurred
            }
            catch (Exception ex)
            {
                string sm = $"GetAllMaterialdaten: An unexpected error occured => " + ex;
                _log.Error(sm);
                result = new ApiResultMaterialdaten //Default ApiResult on error
                {
                    ErrorNumber = 101,
                    MessageShort = sm,
                    MessageLong = sm,
                    MessageTechnical = $"{sm} [] - {ex}"
                };
            }

            return Request.CreateResponse(HttpStatusCode.OK, result);
        }

        [Route("goodsmovement")]
        [HttpPut]
        public async Task<HttpResponseMessage> ExecuteGoodsMovement([FromBody] SAP_CompControl_Models.GoodsMovementParameters par, CancellationToken cancellationToken)
        {
            ApiResultStringItems result = new ApiResultStringItems(); //New ApiResult to return via Web
            _log.Debug("ExecuteGoodsMovement received ");

            try
            {
                result = SapStandardFunctions.ExecuteGoodsMovement(par);

                if (result.IsSuccessful) { _log.Debug(result); } //Action was successful => log for debug
                else { _log.Error(result); } //An error occurred
            }
            catch (Exception ex)
            {
                string sm = $"ExecuteGoodsMovement: An unexpected error occured => " + ex;
                _log.Error(sm);
                result = new ApiResultStringItems //Default ApiResult on error
                {
                    ErrorNumber = 101,
                    MessageShort = sm,
                    MessageLong = sm,
                    MessageTechnical = $"{sm} [] - {ex}"
                };
            }

            return Request.CreateResponse(HttpStatusCode.OK, result);
        }
    }
}
