using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using InABox.Core;

namespace InABox.Mobile
{
    public class ShellColumns<TParent,TEntity> : IShellColumns<TEntity> where TEntity : Entity
    {
        
        private static Dictionary<string, Tuple<int,Expression<Func<TEntity, object>>>> _columns = new Dictionary<string, Tuple<int,Expression<Func<TEntity, object>>>> ();

        public int IndexOf(string name) => _columns[name].Item1;
        
        public Expression<Func<TEntity, object>> this[string name] => _columns[name].Item2;

        public ShellColumns<TParent,TEntity> Map(string property, Expression<Func<TEntity, object>> expression)
        {
            int iCol = _columns.TryGetValue(property, out var column)
                ? column.Item1
                : _columns.Keys.Count;
            
            _columns[property] = new Tuple<int, Expression<Func<TEntity, object>>>(iCol, expression);
            //_columns[property] = new Tuple<int, Expression<Func<TEntity, object>>>(_columns.Keys.Count, expression);
            return this;
        }

        public Columns<TEntity> Columns => new Columns<TEntity>(ColumnTypeFlags.None).Add(_columns.Select(x => x.Value.Item2).ToArray());
        
        public int Count => _columns.Count;

    }
}