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

namespace Foo.DataAccessLayer
{
    public enum UserSetting
    {
        ON_LAUNCH_CLOSEPREVIOUS_WORKSPACE,
    }

    public class dUserSettings
    {       
        /// <summary>
        /// Quick Check if there is already a Setting Information available for this Table
        /// </summary>
        /// <param name="setting">a Setting enum</param>
        /// <returns>true if the DB has setting information for this setting</returns>
        internal bool DoesSettingHaveAnEntry(UserSetting setting)        
        {                        
            object obj = null;
            lock (this)
            {
                string sql = "SELECT [SettingName] FROM UserSettings WHERE [SettingName] = @settingname";
                SqlCeParameter[] sqlparams = new SqlCeParameter[] { new SqlCeParameter("@settingname", setting.ToString()) };
                obj = DB.RunSQLCommandTextExecuteScalar(sql, sqlparams);
            }
            return (obj != null);
        }

        /// <summary>
        /// Retrieve Setting Value for the Setting as a string
        /// </summary>
        /// <param name="setting">a Setting enum</param>
        /// <returns>setting value string, or empty.string if not found</returns>
        public string GetSettingValueStr(UserSetting setting)
        {
            object obj = null;
            lock (this)
            {
                string sql = "SELECT [SettingValue] FROM UserSettings WHERE [SettingName] = @settingname";
                SqlCeParameter[] sqlparams = new SqlCeParameter[] { new SqlCeParameter("@settingname", setting.ToString()) };

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

        /// <summary>
        /// Set a Setting Value for the Setting as a string
        /// </summary>
        /// <param name="setting">a Setting enum</param>
        /// <param name="SettingValue">a string value to store for a setting</param>
        /// <returns>true if successfully inserted or updated, false otherwise</returns>
        public bool SetSettingValueStr(UserSetting setting, string strSettingValue)
        {
            // Ensure Value Integrity
            if (String.IsNullOrEmpty(strSettingValue))
                return false;            

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

                if (!bUpdate) // we need to insert
                {
                    string sql = "INSERT INTO UserSettings ([SettingName],[SettingValue]) VALUES (@settingname,@settingvalue)";
                    SqlCeParameter[] sqlparams = new SqlCeParameter[] 
                    { 
                        new SqlCeParameter("@settingname", setting.ToString()),
                        new SqlCeParameter("@settingvalue", strSettingValue),
                    };

                    int nResult = DB.RunSQLCommandTextExecuteNonQuery(sql, sqlparams);
                    return (nResult == 1);                    
                }
                else // we need to update
                {
                    string sql2 = "UPDATE UserSettings SET [SettingValue]=@settingvalue WHERE [SettingName]=@settingname";
                    SqlCeParameter[] sqlparams2 = new SqlCeParameter[] 
                    { 
                        new SqlCeParameter("@settingname", setting.ToString()),
                        new SqlCeParameter("@settingvalue", strSettingValue),
                    };


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

        #region Helper Getter/Setters

        public bool GetSettingValueBool(UserSetting setting)
        {
            try
            {
                string strValue = GetSettingValueStr(setting);
                if(!String.IsNullOrEmpty(strValue))
                {
                    bool bRet = bool.Parse(strValue);
                    return bRet;
                }
            }
            catch(Exception){ /* ignore */ }
            return false;
        }

        public int GetSettingValueInt(UserSetting setting)
        {
            try
            {
                string strValue = GetSettingValueStr(setting);
                if (!String.IsNullOrEmpty(strValue))
                {
                    int bRet = int.Parse(strValue);
                    return bRet;
                }
            }
            catch (Exception) { /* ignore */ }
            return int.MinValue;
        }

        public DateTime GetSettingValueDateTime(UserSetting setting)
        {
            try
            {
                string strValue = GetSettingValueStr(setting);
                if (!String.IsNullOrEmpty(strValue))
                {
                    DateTime bRet = DateTime.Parse(strValue);
                    return bRet;
                }
            }
            catch (Exception) { /* ignore */ }
            return DateTime.MinValue;
        }

        public double GetSettingValueDouble(UserSetting setting)
        {
            try
            {
                string strValue = GetSettingValueStr(setting);
                if (!String.IsNullOrEmpty(strValue))
                {
                    double bRet = double.Parse(strValue);
                    return bRet;
                }
            }
            catch (Exception) { /* ignore */ }
            return double.MinValue;
        }

        public bool SetSettingValueBool(UserSetting setting, bool settingValue)
        {
            return SetSettingValueStr(setting, settingValue.ToString());
        }

        public bool SetSettingValueInt(UserSetting setting, int settingValue)
        {
            return SetSettingValueStr(setting, settingValue.ToString());
        }

        public bool SetSettingValueDateTime(UserSetting setting, DateTime settingValue)
        {
            return SetSettingValueStr(setting, settingValue.ToString());
        }

        public bool SetSettingValueDouble(UserSetting setting, double settingValue)
        {
            return SetSettingValueStr(setting, settingValue.ToString());
        }

        #endregion
    }
}
