using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace MEU.API
{
    public class ApplyCustomSchemaFilters : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter
    {
       

        public void Apply(OpenApiSchema schema, SchemaFilterContext context)
        {

            if (schema?.Properties == null)
            {
                return; 
            }

            Dictionary<string, string> dcValue = new Dictionary<string, string>();
            foreach (var property in context.Type.GetProperties())
            {
                if (property.Name.StartsWith("BODY_PART"))
                {
                    //"System.Collections.Generic.List`1[[MEU.API.Models.working_shift_time"
                    string fullName = property.PropertyType.FullName.Split(',')[0];
                    string name = "";
                    //"MEU.API.Models.working_shift"
                    if (fullName.Contains("System.Collections.Generic.List"))
                    {
                        string[] names = fullName.Split('.');
                        name = "list_" + names.LastOrDefault().Replace("\"", "");
                    }
                    else
                    {
                        string[] names = fullName.Split('.');
                        name = names.LastOrDefault().Replace("\"", "");
                    }
                    dcValue.Add(property.Name.ToUpper(), name);
                }
              
            }

            List<KeyValuePair<string, OpenApiSchema>> lstRemove = new List<KeyValuePair<string, OpenApiSchema>>();
            List<KeyValuePair<string, OpenApiSchema>> lstAdd = new List<KeyValuePair<string, OpenApiSchema>>();
            foreach (KeyValuePair<string, OpenApiSchema> item in schema.Properties)
            {
                if (item.Key.ToUpper().StartsWith("BODY_PART"))
                {
                    if (dcValue.ContainsKey(item.Key.ToUpper()))
                    {
                        KeyValuePair<string, OpenApiSchema> ab = new KeyValuePair<string, OpenApiSchema>(dcValue[item.Key.ToUpper()], item.Value);
                        lstRemove.Add(item);
                        lstAdd.Insert(0,ab);
                    }
                    
                }
            }

            foreach (var item in lstRemove)
            {
                schema.Properties.Remove(item);
            }
            foreach (var item in lstAdd)
            {
                schema.Properties.Add(item);
            }

            var excludedProperties = context.Type.GetProperties().Where(c=>c.GetMethod.IsVirtual).ToArray();
            foreach (PropertyInfo excludedProperty in excludedProperties)
            {
                if (schema.Properties.ContainsKey(excludedProperty.Name))
                {
                    schema.Properties.Remove(excludedProperty.Name);
                }
              
            }
        }
    }
}
