using System;
using System.Collections.Generic;
using System.Text;
using Howest.SchemaDiscovery.DbObjects;
using DataType = Howest.SchemaDiscovery.DbObjects.DbDataType;

namespace Howest.Guidance.DataAccess.CodeGeneration.ObjectModel.Mappings.Helpers
{
	static class ClonerHelper
	{
		public static DataType CloneDbDataType(DataType dataType)
		{
			DataType clone = new DataType();
			clone.Type = dataType.Type;
			clone.ProviderType = dataType.ProviderType;
			clone.Scale = dataType.Scale;
			clone.Size = dataType.Size;
			clone.Precision = dataType.Precision;
			return clone;
		}

		/// <summary>
		/// Clones the property.
		/// </summary>
		/// <param name="property">The property.</param>
		/// <returns></returns>
		public static Property CloneProperty(Property property)
		{
			Property clonedProperty = new Property();

			clonedProperty.ColumnName = property.ColumnName;
			clonedProperty.IsNullable = property.IsNullable;
			clonedProperty.IsPrimaryKey = property.IsPrimaryKey;
			clonedProperty.Name = property.Name;
			clonedProperty.ReadOnly = property.ReadOnly;
			clonedProperty.Type = property.Type;

			return clonedProperty;
		}

		/// <summary>
		/// Clones the parameter.
		/// </summary>
		/// <param name="parameter">The parameter.</param>
		/// <returns></returns>
		public static Parameter CloneParameter(Parameter parameter)
		{
			Parameter clonedParameter = new Parameter();
			DataType clonedDbDataType = new DataType();

			clonedDbDataType.Precision = parameter.DbDataType.Precision;
			clonedDbDataType.ProviderType = parameter.DbDataType.ProviderType;
			clonedDbDataType.Scale = parameter.DbDataType.Scale;
			clonedDbDataType.Size = parameter.DbDataType.Size;
			clonedDbDataType.Type = parameter.DbDataType.Type;
			clonedDbDataType.AllowDbNull = parameter.DbDataType.AllowDbNull;

			clonedParameter.DbDataType = clonedDbDataType;
			clonedParameter.Direction = parameter.Direction;
			clonedParameter.DBName = parameter.DBName;

			return clonedParameter;
		}

		/// <summary>
		/// Clones the entity.
		/// </summary>
		/// <param name="entity">The entity.</param>
		/// <returns></returns>
		public static Entity CloneEntity(Entity entity)
		{
			Entity clonedEntity = new Entity();

			clonedEntity.Name = entity.Name;

			foreach (Property property in entity.Properties)
			{
				Property clonedProperty = CloneProperty(property);

				clonedEntity.Properties.Add(clonedProperty);
			}

			clonedEntity.TableName = entity.TableName;

			return clonedEntity;
		}

		/// <summary>
		/// Clones the entities.
		/// </summary>
		/// <param name="entities">The entities.</param>
		/// <returns></returns>
		public static List<Entity> CloneEntities(List<Entity> entities)
		{
			List<Entity> clonedEntities = new List<Entity>();

			foreach (Entity entity in entities)
			{
				clonedEntities.Add(ClonerHelper.CloneEntity(entity));
			}

			return clonedEntities;
		}

		public static SqlStatement CloneSqlStatement(SqlStatement statement)
		{
			SqlStatement clonedStatement = new SqlStatement();
			clonedStatement.MethodName = statement.MethodName;
			clonedStatement.Sql = statement.Sql;
			clonedStatement.OperationType = statement.OperationType;
			clonedStatement.IsReadOnly = statement.IsReadOnly;
			clonedStatement.Description = statement.Description;
			foreach(Parameter par in statement.Parameters)
				clonedStatement.Parameters.Add(CloneParameter(par));
			return clonedStatement;
		}
	}
}
