﻿using System;
using System.Diagnostics.Contracts;
using LinqToDB;
using LinqToDB.Data;

namespace Hardcore.Db.Test
{
    /// <summary>
    /// Contains test database table definition
    /// </summary>
    public class TableDef<TModel> : TableDef
        where TModel : class
    {
        /// <summary>
        /// Gets or sets items for inserting
        /// </summary>
        public TModel[] Items { get; set; }

        /// <inheritdoc />
        public override void CreateTable(DataConnection cs)
        {
            var t = cs.CreateTable<TModel>();

            if (Items != null)
                t.InsertBatch(Items);
        }

        /// <inheritdoc />
        public override ITableCleanupper CreateTableCleanupper()
        {
            return new TableCleanupper<TModel>();
        }

        /// <inheritdoc />
        public override void DropTable(DataConnection dc)
        {
            dc.DropTable<TModel>();
        }
    }

    /// <summary>
    /// Contains test database table definition
    /// </summary>
    public abstract class TableDef
    {
        /// <summary>
        /// Gets or sets value that determines table cleanup mode
        /// </summary>
        public bool DoNotCleanup { get; set; }

        /// <summary>
        /// Initializes a new instance of <see cref="TableDef"/>
        /// </summary>
        protected TableDef()
        {
            
        }

        /// <summary>
        /// Creates and initializes new table
        /// </summary>
        public abstract void CreateTable(DataConnection dc);

        /// <summary>
        /// Creates an object that can delete all items from table
        /// </summary>
        public abstract ITableCleanupper CreateTableCleanupper();

        /// <summary>
        /// Delete table from data base
        /// </summary>
        public abstract void DropTable(DataConnection dc);
    }

    sealed class TableDefContract : TableDef
    {
        TableDefContract()
        {
            
        }

        /// <inheritdoc />
        public override void CreateTable(DataConnection cs)
        {
            if (cs == null) throw new ArgumentNullException(nameof(cs));
        }

        /// <inheritdoc />
        public override ITableCleanupper CreateTableCleanupper()
        {
            return null;
        }

        /// <inheritdoc />
        public override void DropTable(DataConnection dc)
        {
            if (dc == null) throw new ArgumentNullException(nameof(dc));
        }
    }
}