using LHDN.CRM.BC;
using LHDN.CRM.BC.Services;
using LHDN.CRM.WebAPI.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using System.Web.Http.Filters;

namespace LHDN.CRM.WebAPI.Filters
{
    public class ApiErrorHandlerAttribute : FilterAttribute, IExceptionFilter
    {
        public Task ExecuteExceptionFilterAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
        {
            var ex = actionExecutedContext.Exception;

            if(ex is UnauthorizedAccessException)
            {
                actionExecutedContext.Response = new HttpResponseMessage()
                {
                    StatusCode = System.Net.HttpStatusCode.Unauthorized,
                    Content = new StringContent("Unauthorized Access"),
                    ReasonPhrase = "Unauthorized Access"
                };
            }
            else if (ex is PageAccessUnauthorizedException)
            {
                actionExecutedContext.Response = new HttpResponseMessage()
                {
                    StatusCode = System.Net.HttpStatusCode.MethodNotAllowed,
                    Content = new StringContent("Page Unauthorized Access"),
                    ReasonPhrase = "Page Unauthorized Access"
                };
            }
            else if(ex is NotFoundException)
            {
                actionExecutedContext.Response = new HttpResponseMessage()
                {
                    StatusCode = System.Net.HttpStatusCode.NotFound,
                    Content = new StringContent("Object Not Found"),
                    ReasonPhrase = "Object Not Found"
                };
            }
            else
            {
                HttpStatusCode statusCode = HttpStatusCode.BadRequest;
                string reasonPhrase = "Bad Request";
                
                if(ex is HttpResponseException)
                {
                    var respEx = ex as HttpResponseException;
                    statusCode = respEx.Response.StatusCode;
                    reasonPhrase = respEx.Response.ReasonPhrase;
                }

                actionExecutedContext.Response = new HttpResponseMessage()
                {
                    StatusCode = statusCode,
                    //Content = new StringContent(ex.Message),
                    Content = new StringContent(reasonPhrase),
                    ReasonPhrase = reasonPhrase
                };
            }

            return Task.Factory.StartNew(() =>
            {
                BaseApiController baseController = (BaseApiController)actionExecutedContext.ActionContext.ControllerContext.Controller;
                string userId = actionExecutedContext.ActionContext.RequestContext.Principal.Identity.Name;
                baseController.LogError(ex.Message +" "+ex.StackTrace, userId);
           
            }, cancellationToken);
        }
    }
}