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

namespace Foo.DataAccessLayer
{
    public class dState
    {
        private const string EMPTY_WORKSPACE_NAME_STATE = "____CURRENT__WORKSPACE____IS___SET_TO__NIL__";

        /// <summary>
        /// Get the Current WorkspaceName in the State Table
        /// </summary>
        /// <returns>the name of the current's state workspace</returns>
        public string GetCurrentWorkspaceName()
        {
            object obj = null;
            lock (this)
            {
                string sql = "SELECT [CurrentWorkspaceName] FROM State";                
                obj = DB.RunSQLCommandTextExecuteScalar(sql);
            }

            if (obj != null)
            {
                if (obj.ToString() == EMPTY_WORKSPACE_NAME_STATE)
                    return string.Empty;
                else
                    return obj.ToString();
            }             
            else
                return string.Empty;                        
        }
       
        /// <summary>
        /// Set the Current WorkspaceName in the State Table
        /// </summary>
        /// <param name="currentWorkspaceName">the name of the new workspace name (Can NOT be String.Empty!, use ClearCurrentWorkspaceName() instead)</param>
        /// <returns>true if successful, false otherwise</returns>
        public bool SetCurrentWorkspaceName(string currentWorkspaceName)
        {
            if (!String.IsNullOrEmpty(currentWorkspaceName) && DataTypeValidation.IsValidWorkspaceName(currentWorkspaceName))
            {                                
                lock (this)
                {
                    // Existence Check
                    if ((currentWorkspaceName != EMPTY_WORKSPACE_NAME_STATE) && 
                        !Data.Workspace.DoesWorkspaceNameExist(currentWorkspaceName))
                        return false;

                    string currentWorkspaceNameInDB = GetCurrentWorkspaceName();

                    // check if different
                    bool bIsDifferent = (currentWorkspaceNameInDB.ToLower() != currentWorkspaceName.ToLower());
                    if (!bIsDifferent)
                        return false;

                    bool bInsert = !IsInEmptyWorkspaceNameState() && String.IsNullOrEmpty(currentWorkspaceNameInDB);
                    if (bInsert)
                    {
                        string sql = "INSERT INTO State ([CurrentWorkspaceName]) VALUES (@workspacename)";
                        SqlCeParameter[] sqlparams = new SqlCeParameter[] { new SqlCeParameter("@workspacename", currentWorkspaceName) };

                        int nResult = DB.RunSQLCommandTextExecuteNonQuery(sql, sqlparams);
                        if (nResult != 1)
                            return false;
                        else
                            return true;                        
                    }
                    else
                    {                        
                        string sql = "UPDATE State SET [CurrentWorkspaceName]=@newworkspacename WHERE [CurrentWorkspaceName]=@oldworkspacename";
                        SqlCeParameter[] sqlparams;

                        if (!IsInEmptyWorkspaceNameState())
                        {
                            sqlparams = new SqlCeParameter[]
                            {
                                new SqlCeParameter("@oldworkspacename", currentWorkspaceNameInDB),
                                new SqlCeParameter("@newworkspacename", currentWorkspaceName),
                            };
                        }
                        else
                        {
                            sqlparams = new SqlCeParameter[]
                            {
                                new SqlCeParameter("@oldworkspacename", EMPTY_WORKSPACE_NAME_STATE),
                                new SqlCeParameter("@newworkspacename", currentWorkspaceName),
                            };
                        }

                        int nResult = DB.RunSQLCommandTextExecuteNonQuery(sql, sqlparams);
                        if (nResult != 1)
                            return false;
                        else
                            return true;
                    }
                }
            }
            return false;
        }

        /// <summary>
        /// Mostly used internally for the Empty WorkspaceName State
        /// </summary>
        /// <returns>true if we are in this state, false otherwise</returns>
        internal bool IsInEmptyWorkspaceNameState()
        {
            object obj = null;
            lock (this)
            {
                string sql = "SELECT [CurrentWorkspaceName] FROM State";
                obj = DB.RunSQLCommandTextExecuteScalar(sql);
            }

            if (obj != null)
            {
                if (obj.ToString() == EMPTY_WORKSPACE_NAME_STATE)
                    return true;
                else
                    return false;
            }
            else
                return false;
        }

        /// <summary>
        /// Clears the current workspace in the State Table
        /// </summary>
        public void ClearCurrentWorkspaceName()
        {
            SetCurrentWorkspaceName(EMPTY_WORKSPACE_NAME_STATE);
        }
    }
}
