using Amazon.Route53.Model;
using Amazon.Runtime;
using Amazon.Runtime.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Amazon.Route53.Internal
{
    /// 
    /// Custom pipeline handler
    /// 
    public class AmazonRoute53PreMarshallHandler : PipelineHandler
    {
        /// 
        /// Calls pre invoke logic before calling the next handler 
        /// in the pipeline.
        /// 
        /// The execution context which contains both the
        /// requests and response context.
        public override void InvokeSync(IExecutionContext executionContext)
        {
            PreInvoke(executionContext);
            base.InvokeSync(executionContext);
        }
#if AWS_ASYNC_API 
        /// 
        /// Calls pre invoke logic before calling the next handler 
        /// in the pipeline.
        /// 
        /// The response type for the current request.
        /// The execution context, it contains the
        /// request and response context.
        /// A task that represents the asynchronous operation.
        public override System.Threading.Tasks.Task InvokeAsync(IExecutionContext executionContext)
        {
            PreInvoke(executionContext);
            return base.InvokeAsync(executionContext);                        
        }
#elif AWS_APM_API
        /// 
        /// Calls pre invoke logic before calling the next handler 
        /// in the pipeline.
        /// 
        /// The execution context which contains both the
        /// requests and response context.
        /// IAsyncResult which represent an async operation.
        public override IAsyncResult InvokeAsync(IAsyncExecutionContext executionContext)
        {
            PreInvoke(ExecutionContext.CreateFromAsyncContext(executionContext));
            return base.InvokeAsync(executionContext);
        }
#endif
        /// 
        /// Custom pipeline handler
        /// 
        /// 
        protected virtual void PreInvoke(IExecutionContext executionContext)
        {
            ProcessRequestHandlers(executionContext);
        }
        /// 
        /// Remove prefixes in resource ids.
        /// 
        /// Execution context.
        private static void ProcessRequestHandlers(IExecutionContext executionContext)
        {
            var request = executionContext.RequestContext.OriginalRequest;
            var createHostedZoneRequest = request as CreateHostedZoneRequest;
            if (createHostedZoneRequest != null)
            {
                createHostedZoneRequest.DelegationSetId = ModifyId(createHostedZoneRequest.DelegationSetId);
                return;
            }
            var listHostedZonesRequest = request as ListHostedZonesRequest;
            if (listHostedZonesRequest != null)
            {
                listHostedZonesRequest.DelegationSetId = ModifyId(listHostedZonesRequest.DelegationSetId);
                return;
            }
            var createReusableDelegationSetRequest = request as CreateReusableDelegationSetRequest;
            if (createReusableDelegationSetRequest != null)
            {
                createReusableDelegationSetRequest.HostedZoneId = ModifyId(createReusableDelegationSetRequest.HostedZoneId);
                return;
            }
        }
        private const char slash = '/';
        private static string ModifyId(string id)
        {
            if (string.IsNullOrEmpty(id))
                return id;
            int slashIndex = id.LastIndexOf(slash);
            if (slashIndex < 0)
                return id;
            var newId = id.Substring(slashIndex + 1);
            return newId;
        }
    }
}