using Knoks.Core;
using Newtonsoft.Json;
using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerGen;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Knoks.Api.Filters
{
    public class UidConvertSchemaFilter : ISchemaFilter
    {
        //public void Apply(Schema schema, SchemaRegistry schemaRegistry, Type type)
        //{
        //    // Get model properties where the type is DateTime and the property has a JsonConverter attribute.
        //    var properties = type.GetProperties().Where(prop => prop.PropertyType == typeof(DateTime) && prop.GetCustomAttributes(typeof(JsonConverterAttribute), false).Count() > 0);
        //    foreach (var property in properties)
        //    {
        //        // If the JsonConverter attribute's type parameter is ShortDateConverter i.e. [JsonConverter(typeof(ShortDateConverter))]
        //        if (((JsonConverterAttribute)property.GetCustomAttributes(typeof(JsonConverterAttribute), false).FirstOrDefault()).ConverterType == typeof(Framework.Json.UidConverter))
        //        {
        //            // Update format from "date-time" to "date".
        //            schema.properties[property.Name.ToCamelCase()].format = "date";
        //        }
        //    }
        //}

        public void Apply(Schema model, SchemaFilterContext context)
        {
            var type = context.SystemType;
            // Get model properties where the type is DateTime and the property has a JsonConverter attribute.
            var properties = type.GetProperties().Where(prop => prop.PropertyType == typeof(int) && prop.GetCustomAttributes(typeof(JsonConverterAttribute), false).Count() > 0);
            foreach (var property in properties)
            {
                // If the JsonConverter attribute's type parameter is UidConverter i.e. [JsonConverter(typeof(UidConverter))]
                if (((JsonConverterAttribute)property.GetCustomAttributes(typeof(JsonConverterAttribute), false).FirstOrDefault()).ConverterType == typeof(UidConverter))
                {
                    model.Properties[property.Name].Format = "string";
                }
            }
        }
    }
}
