// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.

using System.IO;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Threading.Tasks;

namespace System.Web.Http.ModelBinding
{
    // Supports JQuery schema on FormURL. 
    public class JQueryMvcFormUrlEncodedFormatter : FormUrlEncodedMediaTypeFormatter
    {
        public override bool CanReadType(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            return true;
        }

        public override Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            if (readStream == null)
            {
                throw new ArgumentNullException("readStream");
            }

            // For simple types, defer to base class
            if (base.CanReadType(type))
            {
                return base.ReadFromStreamAsync(type, readStream, content, formatterLogger);
            }

            return base.ReadFromStreamAsync(typeof(FormDataCollection), readStream, content, formatterLogger).Then(
                (obj) =>
                {
                    FormDataCollection fd = (FormDataCollection)obj;

                    try
                    {
                        return fd.ReadAs(type, String.Empty, RequiredMemberSelector, formatterLogger);
                    }
                    catch (Exception e)
                    {
                        if (formatterLogger == null)
                        {
                            throw;
                        }
                        formatterLogger.LogError(String.Empty, e);
                        return GetDefaultValueForType(type);
                    }
                });
        }
    }
}
