using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlServerCe;
using Foo.DataAccessLayer.DataTypes;

namespace Foo.DataAccessLayer
{
    internal class dVersioningTables
    {

        /// <summary>
        /// Quick Check if there is Version Information available for this Table
        /// </summary>
        /// <param name="TableName">a table name to check</param>
        /// <returns>true if the DB has version information for this Table</returns>
        internal bool DoesTableHaveVersionEntry(string TableName)
        {            
            // Check TableName Integrity
            if (!DataTypeValidation.IsValidTableName(TableName))
                return false;

            object obj = null;
            lock (this)
            {
                string sql = "SELECT [TableName] FROM VersioningTables WHERE [TableName] = @tablename";
                SqlCeParameter[] sqlparams = new SqlCeParameter[] { new SqlCeParameter("@tablename", TableName) };

                obj = DB.RunSQLCommandTextExecuteScalar(sql, sqlparams);
            }
            return (obj != null);
        }

        /// <summary>
        /// Retrieve the Version information for the specified Table
        /// </summary>
        /// <param name="TableName">a table name to check</param>
        /// <returns>version information string, or empty.string if not found</returns>
        internal string GetVersionInformationForTable(string TableName)
        {
            // Check TableName Integrity
            if (!DataTypeValidation.IsValidTableName(TableName))
                return String.Empty;

            object obj = null;
            lock (this)
            {
                string sql = "SELECT [Version] FROM VersioningTables WHERE [TableName] = @tablename";
                SqlCeParameter[] sqlparams = new SqlCeParameter[] { new SqlCeParameter("@tablename", TableName) };

                obj = DB.RunSQLCommandTextExecuteScalar(sql, sqlparams);
                if (obj != null)
                    return obj.ToString();
            }
            return String.Empty;
        }

        /// <summary>
        /// Use this most often to insert/update the version information of a table into the VersioningTables Table
        /// </summary>
        /// <param name="TableName">The table name for which to insert version information</param>
        /// <param name="VersionInformation">the version information in the format of n.n.nnnn</param>
        /// <returns>true if successfully inserted or updated, false otherwise</returns>
        internal bool AddUpdateVersionInformationForSpecifiedTable(string TableName, string VersionInformation)
        {
            // Check TableName Integrity
            if (!DataTypeValidation.IsValidTableName(TableName))
                return false;

            // Check Version Integrity
            if (!DataTypeValidation.IsValidVersionInformation(VersionInformation))
                return false;

            lock (this)
            {
                // First Determine if we need to update or insert
                bool bUpdate = DoesTableHaveVersionEntry(TableName);

                if (!bUpdate) // we need to insert
                {
                    string sql = "INSERT INTO VersioningTables ([TableName],[Version]) VALUES (@tablename,@version)";
                    SqlCeParameter[] sqlparams = new SqlCeParameter[] 
                    { 
                        new SqlCeParameter("@tablename", TableName),
                        new SqlCeParameter("@version", VersionInformation),
                    };

                    int nResult = DB.RunSQLCommandTextExecuteNonQuery(sql, sqlparams);
                    return (nResult == 1);                    
                }
                else // we need to update
                {
                    string sql2 = "UPDATE VersioningTables SET [Version]=@version WHERE [TableName]=@tablename";
                    SqlCeParameter[] sqlparams2 = new SqlCeParameter[] 
                    { 
                        new SqlCeParameter("@tablename", TableName),
                        new SqlCeParameter("@version", VersionInformation),
                    };


                    int nResult2 = DB.RunSQLCommandTextExecuteNonQuery(sql2, sqlparams2);
                    return (nResult2 == 1);
                }
            }            
        }
    }
}
