using System;
using System.Data;
using System.Collections;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Common;
using System.Data.SqlClient;
namespace digioz
{
    /// <summary>
    /// User Profile Management Class
    /// </summary>
    public class UserProfileManagement
    {
        private static PortalSettings coPortalSettings;

        public static UserProfile GetUserProfile(string psUsername)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();
            UserProfile loUserProfile = null;

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);

                string lsSQL = "select * from tblUserProfile where profile_username='" + psUsername + "' ORDER BY profile_id DESC;";

                loDB.openConnection();
                DataTable loUserProfileTable = loDB.QueryDBDataset(lsSQL);
                loDB.closeConnection();
                DataRow loDR = loUserProfileTable.Rows[0];
                int liHideBirthday;

                //foreach (DataRow loDR in loUserProfileTable.Rows)
                //{
                loUserProfile = new UserProfile();

                loUserProfile.ID = Convert.ToInt16(loDR["profile_id"]);
                loUserProfile.Username = Convert.ToString(loDR["profile_username"]);
                loUserProfile.Email = Convert.ToString(loDR["profile_email"]);
                if (!loDR["profile_birthday"].Equals(DBNull.Value))
                {
                    loUserProfile.Birthday = Convert.ToDateTime(loDR["profile_birthday"]);
                }
                liHideBirthday = Convert.ToInt16(loDR["profile_hide_birthday"]);
                if (liHideBirthday == 1)
                {
                    loUserProfile.HideBirthday = true;
                }
                else
                {
                    loUserProfile.HideBirthday = false;
                }
                loUserProfile.Address = Convert.ToString(loDR["profile_address"]);
                loUserProfile.Address2 = Convert.ToString(loDR["profile_address2"]);
                loUserProfile.City = Convert.ToString(loDR["profile_city"]);
                loUserProfile.State = Convert.ToString(loDR["profile_state"]);
                loUserProfile.Zip = Convert.ToString(loDR["profile_zip"]);
                loUserProfile.Country = Convert.ToString(loDR["profile_country"]);
                loUserProfile.Signature = Convert.ToString(loDR["profile_signature"]);
                loUserProfile.Avatar = Convert.ToString(loDR["profile_avatar"]);

                // loUserProfileList.Add(loUserProfile);
                //}
            }
            else
            {
                IDataNonRelational loDB = DataFactoryNonRelational.GetDataObject(coPortalSettings);
                loDB.openEntity();
                IEnumerable loObjs = loDB.GetEntity("UserProfile");                
                foreach (object obj in loObjs)
                {
                    UserProfile loProfile = (UserProfile)obj;
                    if (loProfile.Username == psUsername)
                    {
                        loUserProfile = loProfile;
                        break;
                    }                    
                }
                loDB.closeEntity();
            }

            return loUserProfile;
        }

        public static UserProfile GetUserProfileByEmail(string psEmail)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();
            UserProfile loUserProfile = null;

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);

                string lsSQL = "select * from tblUserProfile where profile_email LIKE LOWER('" + psEmail + "') ORDER BY profile_id DESC;";

                loDB.openConnection();
                DataTable loUserProfileTable = loDB.QueryDBDataset(lsSQL);
                loDB.closeConnection();
                DataRow loDR = loUserProfileTable.Rows[0];
                int liHideBirthday;

                //foreach (DataRow loDR in loUserProfileTable.Rows)
                //{
                loUserProfile = new UserProfile();

                loUserProfile.ID = Convert.ToInt16(loDR["profile_id"]);
                loUserProfile.Username = Convert.ToString(loDR["profile_username"]);
                loUserProfile.Email = Convert.ToString(loDR["profile_email"]);
                if (!loDR["profile_birthday"].Equals(DBNull.Value))
                {
                    loUserProfile.Birthday = Convert.ToDateTime(loDR["profile_birthday"]);
                }
                liHideBirthday = Convert.ToInt16(loDR["profile_hide_birthday"]);
                if (liHideBirthday == 1)
                {
                    loUserProfile.HideBirthday = true;
                }
                else
                {
                    loUserProfile.HideBirthday = false;
                }
                loUserProfile.Address = Convert.ToString(loDR["profile_address"]);
                loUserProfile.Address2 = Convert.ToString(loDR["profile_address2"]);
                loUserProfile.City = Convert.ToString(loDR["profile_city"]);
                loUserProfile.State = Convert.ToString(loDR["profile_state"]);
                loUserProfile.Zip = Convert.ToString(loDR["profile_zip"]);
                loUserProfile.Country = Convert.ToString(loDR["profile_country"]);
                loUserProfile.Signature = Convert.ToString(loDR["profile_signature"]);
                loUserProfile.Avatar = Convert.ToString(loDR["profile_avatar"]);

                // loUserProfileList.Add(loUserProfile);
                //}
            }
            else
            {
                // Non Relational Database
            }

            return loUserProfile;
        }


        public static UserProfileList GetUserProfileTop5List()
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();
            UserProfile loUserProfile;
            UserProfileList loUserProfileList = new UserProfileList();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                string lsSQL = "select Top 5* from tblUserProfile ORDER BY profile_id DESC;";

                loDB.openConnection();
                DataTable loUserProfileTable = loDB.QueryDBDataset(lsSQL);
                loDB.closeConnection();
                int liHideBirthday;

                foreach (DataRow loDR in loUserProfileTable.Rows)
                {
                    loUserProfile = new UserProfile();

                    loUserProfile.ID = Convert.ToInt16(loDR["profile_id"]);
                    loUserProfile.Username = Convert.ToString(loDR["profile_username"]);
                    loUserProfile.Email = Convert.ToString(loDR["profile_email"]);
                    if (!loDR["profile_birthday"].Equals(DBNull.Value))
                    {
                        loUserProfile.Birthday = Convert.ToDateTime(loDR["profile_birthday"]);
                    }
                    liHideBirthday = Convert.ToInt16(loDR["profile_hide_birthday"]);
                    if (liHideBirthday == 1)
                    {
                        loUserProfile.HideBirthday = true;
                    }
                    else
                    {
                        loUserProfile.HideBirthday = false;
                    }
                    loUserProfile.Address = Convert.ToString(loDR["profile_address"]);
                    loUserProfile.Address2 = Convert.ToString(loDR["profile_address2"]);
                    loUserProfile.City = Convert.ToString(loDR["profile_city"]);
                    loUserProfile.State = Convert.ToString(loDR["profile_state"]);
                    loUserProfile.Zip = Convert.ToString(loDR["profile_zip"]);
                    loUserProfile.Country = Convert.ToString(loDR["profile_country"]);
                    loUserProfile.Signature = Convert.ToString(loDR["profile_signature"]);
                    loUserProfile.Avatar = Convert.ToString(loDR["profile_avatar"]);

                    loUserProfileList.Add(loUserProfile);
                }
                
            }
            return loUserProfileList;
        }

        public static bool SaveUserProfile(UserProfile poUserProfile, string psMode)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();
            bool lbResult = false;

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                string lsSQL = String.Empty;

                if (psMode == "New")
                {
                    string lsBirthday, lsHideBirthday;
                    if (poUserProfile.Birthday != DateTime.MinValue) { lsBirthday = "'" + poUserProfile.Birthday.ToShortDateString() + "',"; } else { lsBirthday = "null,"; }
                    if (poUserProfile.HideBirthday == true) { lsHideBirthday = "'1',"; } else { lsHideBirthday = "'0',"; }

                    lsSQL = "EXEC spInsertUserRegistration "
                            + "'" + poUserProfile.Username + "',"
                            + "'" + EncryptString(poUserProfile.Password) + "',"
                            + "'" + poUserProfile.Permission + "',"
                            + "'" + poUserProfile.Email + "',"
                            + lsBirthday
                            + lsHideBirthday
                            + "'" + poUserProfile.Address + "',"
                            + "'" + poUserProfile.Address2 + "',"
                            + "'" + poUserProfile.City + "',"
                            + "'" + poUserProfile.State + "',"
                            + "'" + poUserProfile.Zip + "',"
                            + "'" + poUserProfile.Country + "',"
                            + "'" + poUserProfile.Signature + "';";

                    loDB.openConnection();
                    loDB.ExecDB(lsSQL);
                    loDB.closeConnection();
                    lbResult = true;
                }
                else if (psMode == "Update")
                {
                    string lsBirthday, lsHideBirthday;
                    if (poUserProfile.Birthday != DateTime.MinValue) { lsBirthday = "'" + poUserProfile.Birthday.ToShortDateString() + "',"; } else { lsBirthday = "null,"; }
                    if (poUserProfile.HideBirthday == true) { lsHideBirthday = "'1',"; } else { lsHideBirthday = "'0',"; }

                    lsSQL = "EXEC spUpdateUserProfile "
                            + "'" + poUserProfile.Username + "',"
                            + "'" + poUserProfile.Email + "',"
                            + lsBirthday
                            + lsHideBirthday
                            + "'" + poUserProfile.Address + "',"
                            + "'" + poUserProfile.Address2 + "',"
                            + "'" + poUserProfile.City + "',"
                            + "'" + poUserProfile.State + "',"
                            + "'" + poUserProfile.Zip + "',"
                            + "'" + poUserProfile.Country + "',"
                            + "'" + poUserProfile.Signature + "';";

                    loDB.openConnection();
                    loDB.ExecDB(lsSQL);
                    loDB.closeConnection();
                    lbResult = true;
                }
            }
            else
            {
                digioz.XML xml = new XML(coPortalSettings);
                xml.SaveEntity(poUserProfile);
            }

            return lbResult;
        }

        public static bool UpdateUserProfileAvatar(string psUsername, string psAvatar)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();
            bool lbResult = false;

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                string lsSQL = String.Empty;

                lsSQL = "Update tblUserProfile set profile_avatar =  '" + psAvatar + "' Where profile_username = '" + psUsername + "'"; ;

                loDB.openConnection();
                loDB.ExecDB(lsSQL);
                loDB.closeConnection();
                lbResult = true;
                }
            return lbResult;
        }

        public static void DeleteUserProfile(string psUsername)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();
            SqlParameter[] parameters = { new SqlParameter("@username", psUsername)};

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                string lsSQLUser = "DELETE FROM tblUsers WHERE username='" + psUsername + "'";
                string lsSQLUserProfile = "DELETE FROM tblUsersProfile WHERE profile_username='" + psUsername + "'";

                loDB.openConnection();
                loDB.ExecDB(lsSQLUser);
                loDB.ExecDB(lsSQLUserProfile);
                loDB.closeConnection();
            }
            else
            {
                IDataNonRelational loDB = DataFactoryNonRelational.GetDataObject(coPortalSettings);
                IEnumerable loObjs = loDB.GetEntity("User");
                foreach (object obj in loObjs)
                {
                    User loUser = (User)obj;
                    if (loUser.Username == psUsername)
                    {
                        loDB.DeleteEntity(loUser);
                    }                    
                }
                loObjs = loDB.GetEntity("UserProfile");
                foreach (object obj in loObjs)
                {
                    UserProfile loProfile = (UserProfile)obj;
                    if (loProfile.Username == psUsername)
                    {
                        loDB.DeleteEntity(loProfile);
                    }
                }                
            }
        }

        public static bool DoesEmailExist(string psEmail)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();
            UserProfile loUserProfile = null;
            bool lbResult = false;

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                int loCount = 0;

                string lsSQL = "select COUNT(*) from tblUserProfile WHERE profile_email LIKE LOWER('" + psEmail + "');";

                loDB.openConnection();
                loCount = Convert.ToInt16(loDB.ExecDBScalar(lsSQL));
                loDB.closeConnection();

                if (loCount > 0)
                {
                    lbResult = true;
                }
            }
            else
            {
                // Non Relational Database
            }

            return lbResult;
        }

        public static string EncryptString(string psOriginalText)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            RijndaelEnhanced lsRijndael = new RijndaelEnhanced(coPortalSettings.EncryptionKey, coPortalSettings.EncryptionVector);
            return lsRijndael.Encrypt(psOriginalText);
        }
    }
}
