using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.Serialization;
using Newtonsoft.Json;

namespace InABox.Core
{
    public interface IColumn
    {
        string Property { get; }
    }

    public class Column<T> : SerializableExpression<T>, IColumn
    {

        public bool IsEqualTo(String name) => 
            !String.IsNullOrWhiteSpace(name) && String.Equals(Property, name);

        public bool IsParentOf(String name) =>
            !String.IsNullOrWhiteSpace(name) && name.StartsWith(Property + ".");
        
        public Column()
        {
        }

        public Column(Expression<Func<T, object?>> expression) : base(expression)
        {
            //String[] parts = expression.ToString().Split(new String[] { "=>" }, StringSplitOptions.RemoveEmptyEntries);
            //string property = String.Join(".", parts.Last().Split('.').Skip(1));
            //property = property.Replace("Convert(", "").Replace("(","").Replace(")", "");

            Property = CoreUtils.GetFullPropertyName(expression, ".");
        }

        public Column(string property)
        {
            Property = property;

            var iprop = DatabaseSchema.Property(typeof(T), property);
            if (iprop != null)
                Expression = iprop.Expression();
            else
                Expression = CoreUtils.CreateMemberExpression(typeof(T), property);
        }

        public string Property { get; private set; }

        public override void Deserialize(SerializationInfo info, StreamingContext context)
        {
        }

        public override void Serialize(SerializationInfo info, StreamingContext context)
        {
        }

        public static explicit operator Column<T>(Column<Entity> v)
        {
            var result = new Column<T>();
            var exp = CoreUtils.ExpressionToString(typeof(T), v.Expression, true);
            result.Expression = CoreUtils.StringToExpression(exp);
            result.Property = v.Property;
            return result;
        }

        public override string ToString()
        {
            var name = Expression.ToString().Replace("x => ", "").Replace("x.", "");
            if (Expression.NodeType == System.Linq.Expressions.ExpressionType.Index)
            {
                var chars = name.SkipWhile(x => !x.Equals('[')).TakeWhile(x => !x.Equals(']'));
                name = string.Join("", chars).Replace("[", "").Replace("]", "").Replace("\"", "");
            }

            return name;
            //return Property.ToString();
        }

        public Type ExpressionType()
        {
            if (Expression == null)
                throw new Exception(string.Format("Expression [{0}] may not be null!", Property));
            if (Expression is IndexExpression)
                return DatabaseSchema.Property(typeof(T), Property).PropertyType;
            return Expression.Type;
        }
    }

    public interface IColumns
    {
        bool Any();
        IEnumerable<string> ColumnNames();
        Dictionary<String, Type> AsDictionary();
        IColumns Add(string column);
        IColumns Add(IColumn column);
        IColumns Add<T>(Expression<Func<T, object?>> column);
        IColumns DefaultColumns(params ColumnType[] types);
    }

    public enum ColumnType
    {
        ExcludeVisible,
        ExcludeID,
        IncludeOptional,
        IncludeForeignKeys,
        IncludeLinked,
        IncludeAggregates,
        IncludeFormulae,
        IncludeUserProperties,
        IncludeNestedLinks,
        IncludeEditable,
        All
    }

    public static class Columns
    {
        public static IColumns Create<T>(Type concrete)
        {
            if (!typeof(T).IsAssignableFrom(concrete))
                throw new Exception($"Columns: {concrete.EntityName()} does not implement {typeof(T).EntityName()}");
            var type = typeof(Columns<>).MakeGenericType(concrete);
            var result = Activator.CreateInstance(type);
            return (result as IColumns)!;
        }
        public static IColumns Create(Type concrete)
        {
            var type = typeof(Columns<>).MakeGenericType(concrete);
            var result = Activator.CreateInstance(type) as IColumns;
            return result!;
        }
    }

    public class Columns<T> : IColumns
    {
        private readonly List<Column<T>> columns;

        public Columns()
        {
            columns = new List<Column<T>>();
        }

        public Columns(params Expression<Func<T, object?>>[] expressions) : this()
        {
            foreach (var expression in expressions)
                columns.Add(new Column<T>(expression));
        }

        public Columns(IEnumerable<string> properties) : this()
        {
            foreach (var property in properties)
                columns.Add(new Column<T>(property));
        }

        public Column<T>[] Items
        {
            get { return columns != null ? columns.ToArray() : new Column<T>[] { }; }
            set
            {
                columns.Clear();
                columns.AddRange(value);
            }
        }

        public bool Any() => columns.Any();

        public IColumns Add(string column)
        {
            if(CoreUtils.TryGetProperty(typeof(T), column, out var propertyInfo))
            {
                if (!propertyInfo.PropertyType.GetInterfaces().Contains(typeof(IEnclosedEntity)) &&
                    !propertyInfo.PropertyType.GetInterfaces().Any(x => x == typeof(IEntityLink)))
                {
                    var exists = columns.Any(x => x.Expression.ToString().Replace("x.", "").Equals(column));
                    if (!exists)
                        columns.Add(new Column<T>(column));
                }
            }
            else
            {
                var prop = DatabaseSchema.Property(typeof(T), column);
                if (prop != null)
                {
                    var exists = columns.Any(x => x.Expression.Equals(prop.Expression()));
                    if (!exists)
                        columns.Add(new Column<T>(column));
                }
            }
            return this;
        }

        public IColumns Add<TEntity>(Expression<Func<TEntity, object?>> expression)
        {
            return Add(CoreUtils.GetFullPropertyName(expression, "."));
        }
        public Columns<T> Add(Column<T> column)
        {
            if(!columns.Any(x => x.Property.Equals(column.Property)))
            {
                columns.Add(column);
            }
            return this;
        }
        public IColumns Add(IColumn column)
        {
            if (column is Column<T> col)
                return Add(col);
            return this;
        }

        public IEnumerable<string> ColumnNames()
        {
            return Items.Select(c => c.Property);
            //List<String> result = new List<string>();
            //foreach (var col in Items)
            //    result.Add(col.Property);
            //return result;
        }

        public Dictionary<String, Type> AsDictionary() 
        { 
            Dictionary< String, Type> result = new Dictionary< String, Type>();
            foreach (var column in Items)
                result[column.Property] = column.ExpressionType();
            return result;
        }


        public IColumns DefaultColumns(params ColumnType[] types)
        {
            return Default(types);
        }

        public Columns<T> Add(params string[] columnnames)
        {
            foreach (var name in columnnames)
                Add(name);
            return this;
        }
        public Columns<T> Add(IEnumerable<string> columnnames)
        {
            foreach (var name in columnnames)
                Add(name);
            return this;
        }
        
        public Columns<T> Add(Expression<Func<T, object?>> expression)
        {
            try
            {
                var property = CoreUtils.GetFullPropertyName(expression, ".");
                var exists = columns.Any(x => x.Expression.ToString().Replace("x.", "").Equals(property));
                if (!exists)
                    columns.Add(new Column<T>(expression));
            }
            catch (Exception e)
            {
                Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
            }

            return this;
        }

        public Columns<T> Add<TType>(Expression<Func<T, TType>> expression)
        {
            try
            {
                var property = CoreUtils.GetFullPropertyName(expression, ".");
                var exists = columns.Any(x => x.Expression.ToString().Replace("x.", "").Equals(property));
                if (!exists)
                {
                    columns.Add(new Column<T>(property));
                }
            }
            catch (Exception e)
            {
                Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
            }

            return this;
        }

        public Columns<T> Remove(string column)
        {
            var col = new Column<T>(column);
            columns.RemoveAll(x => x.ToString() == col.ToString());
            return this;
        }

        public static explicit operator Columns<T>(Columns<Entity> vs)
        {
            var result = new Columns<T>();
            var items = vs.Items.Cast<Column<T>>().ToArray();
            result.Items = items;

            //List<Column<T>> cols = new List<Column<T>>();
            //foreach (var v in vs.Items)
            //	cols.Add((Column<T>)v);
            //result.Items = cols.ToArray();
            return result;
        }

        public Columns<T> Default(params ColumnType[] types)
        {
            columns.Clear();
            
            var props = DatabaseSchema.Properties(typeof(T)).Where(x=>x.Setter() != null).OrderBy(x => CoreUtils.GetPropertySequence(typeof(T), x.Name)).ToList();

            if (types.Contains(ColumnType.All))
            {
                foreach (var prop in props)
                    columns.Add(new Column<T>(prop.Name));
                return this;
            }

            if (typeof(T).IsSubclassOf(typeof(Entity)) && !types.Contains(ColumnType.ExcludeID))
                columns.Add(new Column<T>("ID"));
            
            for  (int iCol=0; iCol<props.Count; iCol++)
            {
                var prop = props[iCol];
                var bOK = true;
                var bIsForeignKey = false;
                var bNullEditor = prop.Editor is NullEditor;

                if (prop is CustomProperty)
                {
                    if (!types.Any(x => x.Equals(ColumnType.IncludeUserProperties)))
                        bOK = false;
                    else
                        columns.Add(new Column<T>(prop.Name));
                }


                if (bOK)
                    if (prop.Name.Contains(".") && !(prop is CustomProperty))
                    {
                        var ancestors = prop.Name.Split('.');
                        var anclevel = 2;
                        for (var i = 1; i < ancestors.Length; i++)
                        {
                            var ancestor = string.Join(".", ancestors.Take(i));
                            var ancprop = CoreUtils.GetProperty(typeof(T), ancestor);
                            
                            bNullEditor = bNullEditor || ancprop.GetCustomAttribute<NullEditor>() != null;

                            if (ancprop.PropertyType.GetInterfaces().Contains(typeof(IEnclosedEntity)))
                                anclevel++;
                            else if (ancprop.PropertyType.GetInterfaces().Contains(typeof(IEntityLink)))
                            {
                                if (types.Contains(ColumnType.IncludeLinked) || types.Contains(ColumnType.IncludeForeignKeys))
                                {
                                    if (types.Contains(ColumnType.IncludeNestedLinks) || ancestors.Length <= anclevel)
                                    {
                                        if (prop.Name.EndsWith(".ID") && types.Contains(ColumnType.IncludeForeignKeys))
                                        {
                                            bIsForeignKey = true;
                                            break;
                                        }

                                        if (!types.Contains(ColumnType.IncludeLinked))
                                        {
                                            bOK = false;
                                            break;
                                        }
                                    }
                                    else
                                    {
                                        bOK = false;
                                        break;
                                    }
                                }
                                else
                                {
                                    bOK = false;
                                    break;
                                }
                            }
                        }
                    }

                if (bOK)
                {
                    var visible = prop.Editor != null 
                        ? bNullEditor 
                            ? Visible.Hidden 
                            : prop.Editor.Visible 
                        : Visible.Optional;
                    var editable = prop.Editor != null
                        ? bNullEditor
                            ? Editable.Hidden
                            : prop.Editor.Editable
                        : Editable.Enabled;
                    
                    bOK = (types.Any(x => x.Equals(ColumnType.IncludeForeignKeys)) && bIsForeignKey) ||
                          (!types.Any(x => x.Equals(ColumnType.ExcludeVisible)) && visible.Equals(Visible.Default)) ||
                          (types.Any(x => x.Equals(ColumnType.IncludeOptional)) && visible.Equals(Visible.Optional)) ||
                          (types.Any(x => x.Equals(ColumnType.IncludeEditable)) && !editable.Equals(Editable.Hidden));
                }

                var property = bOK ? DatabaseSchema.Property(typeof(T), prop.Name) : null;
                if (property is StandardProperty)
                {
                    if (bOK && !types.Any(x => x.Equals(ColumnType.IncludeAggregates)))
                        bOK = CoreUtils.GetProperty(typeof(T), prop.Name).GetCustomAttribute<AggregateAttribute>() == null;
                    if (bOK && !types.Any(x => x.Equals(ColumnType.IncludeFormulae)))
                        bOK = CoreUtils.GetProperty(typeof(T), prop.Name).GetCustomAttribute<FormulaAttribute>() == null;
                }

                if (bOK && !columns.Any(x => string.Equals(x.Property?.ToUpper(), prop.Name?.ToUpper())))
                {
                    if (bOK && prop.Editor is LookupEditor le)
                    {
                        if (le.OtherColumns != null)
                        {
                            var prefix = String.Join(".",prop.Name.Split('.').Reverse().Skip(1).Reverse());
                            foreach (var col in le.OtherColumns)
                            {
                                String newcol = prefix + "." + col.Key;
                                if (!columns.Any(x => String.Equals(newcol, x.Property)))
                                    columns.Add(new Column<T>(newcol));
                            }
                        }
                    }
                    if (!columns.Any(x => String.Equals(prop.Name, x.Property)))
                        columns.Add(new Column<T>(prop.Name));
                }
            }

            return this;
        }
    }

    public class ColumnJsonConverter : JsonConverter
    {
        public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
        {
            if(value is null)
            {
                writer.WriteNull();
                return;
            }

            var property = (CoreUtils.GetPropertyValue(value, "Expression") as Expression)
                ?? throw new Exception("'Column.Expression' may not be null");

            var prop = CoreUtils.ExpressionToString(value.GetType().GenericTypeArguments[0], property, true);

            var name = CoreUtils.GetPropertyValue(value, "Property") as string;

            writer.WriteStartObject();

            writer.WritePropertyName("Expression");
            writer.WriteValue(prop);

            writer.WritePropertyName("Property");
            writer.WriteValue(name);

            writer.WriteEndObject();
        }

        public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null)
                return null;

            var data = new Dictionary<string, object>();
            while (reader.TokenType != JsonToken.EndObject && reader.Read())
                if (reader.Value != null)
                {
                    var key = reader.Value.ToString();
                    reader.Read();
                    data[key] = reader.Value;
                }

            var prop = data["Property"].ToString();
            var result = Activator.CreateInstance(objectType, prop);

            return result;
        }


        public override bool CanConvert(Type objectType)
        {
            if (objectType.IsConstructedGenericType)
            {
                var ot = objectType.GetGenericTypeDefinition();
                var tt = typeof(Column<>);
                if (ot == tt)
                    return true;
            }

            return false;
        }
    }
}