using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using InABox.Clients;
using InABox.Configuration;
using InABox.Core;
using InABox.Wpf;
using InABox.WPF;

namespace InABox.DynamicGrid;

public interface IDynamicManyToManyGrid<TManyToMany, TThis> : IDynamicEditorPage
{
}

public class DynamicManyToManyGrid<TManyToMany, TThis> : DynamicGrid<TManyToMany>,
    IDynamicEditorPage,
    IDynamicManyToManyGrid<TManyToMany, TThis>,
    IDynamicMemoryEntityGrid<TManyToMany>
    where TThis : Entity, new()
    where TManyToMany : Entity, IPersistent, IRemotable, new()
{
    public IQueryProviderFactory Client = Clients.Client.Factory;

    //private Guid ID = Guid.Empty;
    protected TThis Item;

    /// <summary>
    /// Keeps a cache of initially loaded objects, so that we can figure out which guys to delete when we save.
    /// </summary>
    private TManyToMany[] MasterList = Array.Empty<TManyToMany>();

    protected PropertyInfo otherproperty;
    protected IEntityLink GetOtherLink(TManyToMany item) => (otherproperty.GetValue(item) as IEntityLink)!;

    protected PropertyInfo thisproperty;
    protected IEntityLink GetThisLink(TManyToMany item) => (thisproperty.GetValue(item) as IEntityLink)!;
    
    protected List<TManyToMany> WorkingList = new();

    IEnumerable<TManyToMany> IDynamicMemoryEntityGrid<TManyToMany>.Items => WorkingList;

    public PageType PageType => PageType.Other;

    private bool _readOnly;
    public bool ReadOnly
    {
        get => _readOnly;
        set
        {
            if(_readOnly != value)
            {
                _readOnly = value;
                Reconfigure();
            }
        }
    }

    public virtual bool Visible => Security.CanView<TManyToMany>();

    private static bool IsAutoEntity => typeof(TManyToMany).HasAttribute<AutoEntity>();

    protected DynamicGridCustomColumnsComponent<TManyToMany> ColumnsComponent;

    public HashSet<string>? LoadedColumns { get; set; }

    public DynamicManyToManyGrid()
    {
        MultiSelect = true;
        thisproperty = CoreUtils.GetManyToManyThisProperty(typeof(TManyToMany), typeof(TThis));
        otherproperty = CoreUtils.GetManyToManyOtherProperty(typeof(TManyToMany), typeof(TThis));

        HiddenColumns.Add(x => x.ID);
        HiddenColumns.Add(CoreUtils.CreateLambdaExpression<TManyToMany>(otherproperty.Name + ".ID"));

        ColumnsComponent = new DynamicGridCustomColumnsComponent<TManyToMany>(this, GetTag());
    }

    protected override void DoReconfigure(DynamicGridOptions options)
    {
        base.DoReconfigure(options);

        options.RecordCount = true;
        options.SelectColumns = true;
        options.MultiSelect = true;

        if (Security.CanEdit<TManyToMany>() && !ReadOnly)
        {
            options.AddRows = true;
            options.EditRows = true;
        }
        if (Security.CanDelete<TManyToMany>() && !ReadOnly)
            options.DeleteRows = true;
        if (Security.CanImport<TManyToMany>() && !ReadOnly)
            options.ImportData = true;
        if (Security.CanExport<TManyToMany>())
            options.ExportData = true;
        if (Security.CanMerge<TManyToMany>())
            options.MultiSelect = true;
    }

    public bool MultiSelect { get; set; }

    public DynamicEditorGrid EditorGrid { get; set; }

    public string Caption()
    {
        //var m2m = typeof(TManyToMany).GetInterfaces().FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IManyToMany<,>) && i.GenericTypeArguments.Contains(typeof(TThis)));
        //Type other = m2m.GenericTypeArguments.FirstOrDefault(x => x != typeof(TThis));
        //var serv = System.Data.Entity PluralizationService.CreateService(new System.Globalization.CultureInfo("en-us"));
        //var plural = serv.Pluralize(source);
        //return MvcHtmlString.Create(plural);

        var result = new Inflector.Inflector(new CultureInfo("en")).Pluralize(OtherType().Name);
        return result;
    }

    public virtual int Order { get; set; } = int.MinValue;

    public bool Ready { get; set; }

    public void Load(object item, Func<Type, CoreTable?>? PageDataHandler)
    {
        Item = (TThis)item;

        var data = PageDataHandler?.Invoke(typeof(TManyToMany));
        if (data != null)
        {
            RefreshData(data);
        }
        else
        {
            if (Item.ID == Guid.Empty)
            {
                data = new CoreTable();
                data.LoadColumns(typeof(TManyToMany));
                RefreshData(data);
            }
            else
            {
                var exp = CoreUtils.GetPropertyExpression<TManyToMany>(thisproperty.Name + ".ID");
                var filter = new Filter<TManyToMany>(exp).IsEqualTo(Item.ID).And(exp).IsNotEqualTo(Guid.Empty);
                var sort = LookupFactory.DefineSort<TManyToMany>();

                var columns = DynamicGridUtils.LoadEditorColumns(DataColumns());

                Client.Query(filter, columns, sort, null, (o, e) =>
                {
                    if (o != null)
                    {
                        LoadedColumns = columns.ColumnNames().ToHashSet();

                        Dispatcher.Invoke(() => RefreshData(o));
                    }
                    else if(e != null)
                    {
                        Dispatcher.Invoke(() =>
                        {
                            MessageWindow.ShowError("An error occurred while loading data.", e);
                        });
                    }
                });
            }
        }
    }

    public void BeforeSave(object item)
    {
        // Don't need to do anything here
    }

    public void AfterSave(object item)
    {
        if (IsAutoEntity)
        {
            return;
        }
        // First remove any deleted files
        foreach (var map in MasterList)
            if (!WorkingList.Contains(map))
                Client.Delete(map, typeof(TManyToMany).Name + " Deleted by User");

        foreach (var map in WorkingList)
        {
            var prop = GetThisLink(map);
            if (prop.ID != Item.ID)
                prop.ID = Item.ID;
        }

        if (WorkingList.Any(x => x.IsChanged()))
            Client.Save(WorkingList.Where(x => x.IsChanged()), "Updated by User");

        MasterList = WorkingList.ToArray();
    }

    public Size MinimumSize()
    {
        return new Size(400, 400);
    }

    private static Type OtherType() =>
        CoreUtils.GetManyToManyOtherType(typeof(TManyToMany), typeof(TThis));

    private static string GetTag()
    {
        return typeof(TManyToMany).Name + "." + typeof(TThis).Name;
    }

    public override DynamicGridColumns GenerateColumns()
    {
        var cols = new DynamicGridColumns();
        cols.AddRange(base.GenerateColumns().Where(x => !x.ColumnName.StartsWith(thisproperty.Name + ".")));
        return cols;
    }

    protected override DynamicGridColumns LoadColumns()
    {
        return ColumnsComponent.LoadColumns();
    }

    protected override void SaveColumns(DynamicGridColumns columns)
    {
        ColumnsComponent.SaveColumns(columns);
    }
    protected override void LoadColumnsMenu(ContextMenu menu)
    {
        base.LoadColumnsMenu(menu);
        ColumnsComponent.LoadColumnsMenu(menu);
    }

    protected override DynamicGridSettings LoadSettings()
    {
        var tag = GetTag();

        var user = Task.Run(() => new UserConfiguration<DynamicGridSettings>(tag).Load());
        user.Wait();

        //var global = Task.Run(() => new GlobalConfiguration<DynamicGridSettings>(tag).Load());
        //global.Wait();
        //Task.WaitAll(user, global);
        //var columns = user.Result.Any() ? user.Result : global.Result;

        return user.Result;
    }
    protected override void SaveSettings(DynamicGridSettings settings)
    {
        var tag = GetTag();
        new UserConfiguration<DynamicGridSettings>(tag).Save(settings);
    }

    protected virtual Guid[] CurrentGuids()
    {
        var result = new List<Guid>();
        foreach (var item in WorkingList)
        {
            var prop = GetOtherLink(item);
            // var prop = GetThisLink(item);
            result.Add(prop.ID);
        }

        return result.ToArray();
    }


    protected virtual IFilter? GetFilter()
    {
        var result = LookupFactory.DefineChildFilter(typeof(TThis), OtherType(), new[] { Item });

        var filter = Filter.Create(OtherType(), "ID").NotInList(CurrentGuids());

        if (filter != null)
        {
            if (result != null)
            {
                filter.And(result);
            }

            return filter;
        }

        if (result != null) return result;

        return null;
    }

    protected override void DoAdd(bool openEditorOnDirectEdit = false)
    {
        if (MultiSelect)
        {
            var filter = GetFilter();

            var dlgtype = typeof(MultiSelectDialog<>).MakeGenericType(OtherType());
            var columns = LookupFactory.DefineLookupColumns(typeof(TManyToMany), OtherType(), otherproperty.Name);
            var prefix = otherproperty.Name + ".";
            foreach(var column in DataColumns())
            {
                if(column.Property.StartsWith(prefix))
                {
                    columns.Add(column.Property[prefix.Length..]);
                }
            }

            var dlg = (Activator.CreateInstance(dlgtype, filter, columns, true) as IMultiSelectDialog)!;
            if (dlg.ShowDialog())
            {
                var guids = CurrentGuids();
                foreach (var entity in dlg.Data().ToObjects(OtherType()).Cast<Entity>())
                {
                    if (!guids.Contains(entity.ID))
                    {
                        var newitem = CreateItem();
                        var prop = GetOtherLink(newitem);
                        prop.ID = entity.ID;
                        prop.Synchronise(entity);
                        SaveItem(newitem);
                    }
                }

                Refresh(false, true);
            }
        }
        else
        {
            base.DoAdd();
        }
    }

    public override TManyToMany CreateItem()
    {
        var result = new TManyToMany();
        if (Item != null)
        {
            var prop = GetThisLink(result);
            prop.ID = Item.ID;
            prop.Synchronise(Item);
        }

        return result;
    }

    public override TManyToMany LoadItem(CoreRow row)
    {
        return WorkingList[_recordmap[row].Index];
    }

    public override void SaveItem(TManyToMany item)
    {
        if (!WorkingList.Contains(item))
            WorkingList.Add(item);
    }

    public override void DeleteItems(params CoreRow[] rows)
    {
        foreach (var row in rows)
        {
            var id = row.Get<TManyToMany, Guid>(c => c.ID);
            var item = WorkingList.FirstOrDefault(x => x.ID.Equals(id));
            if (item != null)
                WorkingList.Remove(item);
        }
    }

    public void Cancel()
    {
        foreach(var item in MasterList)
        {
            item.CancelChanges();
        }
        WorkingList = MasterList.ToList();
        Refresh(false, true);
    }

    private void RefreshData(CoreTable data)
    {
        MasterList = data.ToArray<TManyToMany>();
        WorkingList = MasterList.ToList();
        Refresh(true, true);
        Ready = true;
    }

    protected override void Reload(
        Filters<TManyToMany> criteria, Columns<TManyToMany> columns, ref SortOrder<TManyToMany>? sort, 
        CancellationToken token, Action<CoreTable?, Exception?> action)
    {
        var results = new CoreTable();
        results.LoadColumns(typeof(TManyToMany));

        this.EnsureColumns(columns);

        if (sort != null)
        {
            var exp = IQueryableExtensions.ToLambda<TManyToMany>(sort.Expression);
            var sorted = sort.Direction == SortDirection.Ascending
                ? WorkingList.AsQueryable().OrderBy(exp)
                : WorkingList.AsQueryable().OrderByDescending(exp);
            foreach (var then in sort.Thens)
            {
                var thexp = IQueryableExtensions.ToLambda<TManyToMany>(then.Expression);
                sorted = sort.Direction == SortDirection.Ascending ? sorted.ThenBy(exp) : sorted.ThenByDescending(exp);
            }

            WorkingList = sorted.ToList();
        }
        results.LoadRows(WorkingList);

        //results.LoadRows(WorkingList);
        action.Invoke(results, null);
    }

    protected override BaseEditor? GetEditor(object item, DynamicGridColumn column)
    {
        var type = CoreUtils.GetProperty(typeof(TManyToMany), column.ColumnName).DeclaringType;
        if (type.GetInterfaces().Contains(typeof(IEntityLink)) && type.ContainsInheritedGenericType(typeof(TThis)))
            return new NullEditor();
        return base.GetEditor(item, column);
    }

    public override void LoadEditorButtons(TManyToMany item, DynamicEditorButtons buttons)
    {
        base.LoadEditorButtons(item, buttons);
        buttons.Add("Audit Trail", Wpf.Resources.view.AsBitmapImage(), item, AuditTrailClick);
    }

    private void AuditTrailClick(object sender, object? item)
    {
        if (item is not TManyToMany entity) return;

        var window = new AuditWindow(entity.ID);
        window.ShowDialog();
    }

    public override DynamicEditorPages LoadEditorPages(TManyToMany item)
    {
        return item.ID != Guid.Empty ? base.LoadEditorPages(item) : new DynamicEditorPages();
    }

    protected override bool BeforeCopy(IList<TManyToMany> items)
    {
        if (!base.BeforeCopy(items)) return false;

        for(int i = 0; i < items.Count; ++i)
        {
            var newItem = items[i].Clone();
            newItem.ID = Guid.Empty;
            items[i] = newItem;
        }
        return true;
    }
}