using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RDH.Data
{
    public class Identity : IEquatable<Identity>
    {
        internal Identity ForGrid(Type primaryType, int gridIndex)
        {
            return new Identity(sql, commandType, connectionString, primaryType, parametersType, null, gridIndex);
        }

        internal Identity ForGrid(Type primaryType, Type[] otherTypes, int gridIndex)
        {
            return new Identity(sql, commandType, connectionString, primaryType, parametersType, otherTypes, gridIndex);
        }

        public Identity ForDynamicParameters(Type type)
        {
            return new Identity(sql, commandType, connectionString, this.type, type, null, -1);
        }

        internal Identity(string sql, CommandType? commandType, IDbConnection connection, Type type, Type parametersType, Type[] otherTypes)
            : this(sql, commandType, connection.ConnectionString, type, parametersType, otherTypes, 0)
        { }
        private Identity(string sql, CommandType? commandType, string connectionString, Type type, Type parametersType, Type[] otherTypes, int gridIndex)
        {
            this.sql = sql;
            this.commandType = commandType;
            this.connectionString = connectionString;
            this.type = type;
            this.parametersType = parametersType;
            this.gridIndex = gridIndex;
            unchecked
            {
                hashCode = 17; // we *know* we are using this in a dictionary, so pre-compute this
                hashCode = hashCode * 23 + commandType.GetHashCode();
                hashCode = hashCode * 23 + gridIndex.GetHashCode();
                hashCode = hashCode * 23 + (sql == null ? 0 : sql.GetHashCode());
                hashCode = hashCode * 23 + (type == null ? 0 : type.GetHashCode());
                if (otherTypes != null)
                {
                    foreach (var t in otherTypes)
                    {
                        hashCode = hashCode * 23 + (t == null ? 0 : t.GetHashCode());
                    }
                }
                hashCode = hashCode * 23 + (connectionString == null ? 0 : connectionString.GetHashCode());
                hashCode = hashCode * 23 + (parametersType == null ? 0 : parametersType.GetHashCode());
            }
        }
        public override bool Equals(object obj)
        {
            return Equals(obj as Identity);
        }
        public readonly string sql;
        public readonly CommandType? commandType;
        public readonly int hashCode, gridIndex;
        private readonly Type type;
        public readonly string connectionString;
        public readonly Type parametersType;
        public override int GetHashCode()
        {
            return hashCode;
        }
        public bool Equals(Identity other)
        {
            return
                other != null &&
                gridIndex == other.gridIndex &&
                type == other.type &&
                sql == other.sql &&
                commandType == other.commandType &&
                connectionString == other.connectionString &&
                parametersType == other.parametersType;
        }
    }

}
