using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;

namespace digioz
{
    public class FriendsManagement
    {
        private static PortalSettings coPortalSettings;

        public static FriendsList GetFiendsInfoUser(string psUserid)
        {
            FriendsList loFriendsList = new FriendsList();

            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                Friends loFriends;
                DataTable loFriendsTable;

                string lsSQL = "SELECT tblUserFriends.approved, tblUserProfile.profile_id, tblUserProfile.profile_username, tblUserProfile.profile_email, tblUserProfile.profile_avatar FROM tblUserFriends LEFT JOIN tblUsers ON tblUserFriends.friendID = tblUsers.id LEFT JOIN tblUserProfile ON tblUsers.username = tblUserProfile.profile_username WHERE tblUserFriends.userid='" + psUserid + "'  ORDER by tblUserFriends.id DESC";

                loDB.openConnection();
                loFriendsTable = loDB.QueryDBDataset(lsSQL);
                loDB.closeConnection();

                foreach (DataRow loDR in loFriendsTable.Rows)
                {
                    loFriends = new Friends();
                    if (!loDR["approved"].Equals(DBNull.Value))
                    {
                        loFriends.Approved = (bool)loDR["approved"];
                    }
                    loFriends.ProfileID = (int)loDR["profile_id"];
                    loFriends.ProfileUsername = (string)loDR["profile_username"];
                    loFriends.ProfileEmail = (string)loDR["profile_email"];
                    loFriends.ProfileAvatar = (string)loDR["profile_avatar"];

                    loFriendsList.Add(loFriends);
                }
            }
            return loFriendsList;
        }

        public static FriendsList GetNeedApprovedFriends(string psUserid)
        {
            FriendsList loFriendsList = new FriendsList();

            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                Friends loFriends;
                DataTable loFriendsTable;

                string lsSQL = "SELECT tblUserProfile.profile_id, tblUserProfile.profile_username, tblUserProfile.profile_email, tblUserProfile.profile_avatar FROM tblUserFriends LEFT JOIN tblUsers ON tblUserFriends.userid = tblUsers.id LEFT JOIN tblUserProfile ON tblUsers.username = tblUserProfile.profile_username WHERE tblUserFriends.approved = 0 and tblUserFriends.friendid ='" + psUserid + "' ORDER by tblUserFriends.id DESC";

                loDB.openConnection();
                loFriendsTable = loDB.QueryDBDataset(lsSQL);
                loDB.closeConnection();
 
                foreach (DataRow loDR in loFriendsTable.Rows)
                {
                    loFriends = new Friends();
                    
                    loFriends.ProfileID = (int)loDR["profile_id"];
                    loFriends.ProfileUsername = (string)loDR["profile_username"];
                    loFriends.ProfileEmail = (string)loDR["profile_email"];
                    loFriends.ProfileAvatar = (string)loDR["profile_avatar"];

                    loFriendsList.Add(loFriends);
                }
            }
            return loFriendsList;
        }

        /// <summary>
        /// This method is based on GetNeedApprovedFriends.
        /// Instead of retrieving a recordset, it retrieves a count
        /// </summary>
        /// <param name="psUserid"></param>
        /// <returns></returns>
        public static int GetNeedApprovedFriendsCount(int piUserid)
        {
            int liCount = 0;
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                string lsSQL = string.Format("EXEC spGetNeedApprovedFriendsCount @Userid= {0}",
                                             piUserid);
                /*SqlParameter[] loParameters = new SqlParameter[1];
                loParameters[0] = new SqlParameter("@Userid", SqlDbType.Int);
                loParameters[0].Value = piUserid;*/

                try
                {
                    loDB.openConnection();
                    object loCount = loDB.ExecDBScalar(lsSQL);
                    if (loCount != null)
                    {
                        liCount = Convert.ToInt32(loCount);
                    }
                }
                finally
                {
                    loDB.closeConnection();
                }
            }
            return liCount;
        }

        public static void AddFriendInfo(string psUserid, string psFriendid, bool approve)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                string lsSQL = "EXEC spInsertUserFriend " + psUserid + "," + psFriendid + "," + approve.ToString();
                loDB.openConnection();
                loDB.ExecDB(lsSQL);
                loDB.closeConnection();
            }
        }

        public static bool IsInFriendList(string psUserid, string psFriendid)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();
            bool lbReturnValue = false;
            DataTable loDataTable;

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                string lsSQL = "SELECT id FROM tblUserFriends WHERE userid = '" + psUserid + "' and friendID = '" + psFriendid + "'";
                loDB.openConnection();
                loDataTable = loDB.QueryDBDataset(lsSQL);
                loDB.closeConnection();
                lbReturnValue = loDataTable.Rows.Count != 0;
            }
            return lbReturnValue;
        }

        public static void RemoveFriendInfo(string psUserid, string psFriendid)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                string lsSQL = "EXEC spDeleteUserFriend " + psUserid + "," + psFriendid;
                loDB.openConnection();
                loDB.ExecDB(lsSQL);
                loDB.closeConnection();
            }
        }
    }

}
