using System;
using System.Collections.Generic;
using System.Text;
using JuSNS.Model;
using JuSNS.Common;
using JuSNS.Factory;
using System.Data;

namespace JuSNS.Home
{
    /// <summary>
    /// 群组类
    /// </summary>
    public class UserGroup
    {
        private IUserGroup dal;
        /// <summary>
        /// 构造函数
        /// </summary>
        public UserGroup()
        {
            dal = DataAccess.CreateUserGroup();
        }

        /// <summary>
        /// 添加群组
        /// </summary>
        /// <param name="Info">群组实体类</param>
        /// <returns>添加成功返回群编号</returns>
        public int Add(UserGroupInfo Info)
        {
            return dal.Add(Info);
        }

        /// <summary>
        /// 删除群组(后台)
        /// </summary>
        /// <param name="GroupID">群组编号</param>
        /// <returns>删除成功返回1</returns>
        public int Del(int GroupID)
        {
            UserGroupInfo tempinfo = new UserGroupInfo();
            tempinfo = this.Info(GroupID);
            int n = dal.Del(GroupID);
            if (n == 1)
            {
                string s_Rootpath = ServerInfo.GetRootPath();
                if (tempinfo.Portrait.IndexOf("grouppic.jpg") <= 0)
                    Public.DelFile(s_Rootpath + tempinfo.Portrait);
            }
            return n;
        }

        public int isRec(int GroupId, int st)
        {
            return dal.isRec(GroupId, st);
        }
        /// <summary>
        ///  删除群组(前台)
        /// </summary>
        /// <param name="GroupID">群组编号</param>
        /// <param name="UserID">群主编号</param>
        /// <returns>删除成功返回1,否则返回-1</returns>
        public int Del(int GroupID, int UserID)
        {
            UserGroupInfo tempinfo = new UserGroupInfo();
            tempinfo = this.Info(GroupID);
            int n = dal.Del(GroupID, UserID);
            if (n == 1)
            {
                string s_Rootpath = ServerInfo.GetRootPath();
                if (tempinfo.Portrait.IndexOf("grouppic.jpg") <= 0)
                    Public.DelFile(s_Rootpath + tempinfo.Portrait);
            }
            return n;
        }

        /// <summary>
        /// 修改群组基本信息
        /// </summary>
        /// <param name="Info">群组实体类</param>
        /// <returns>修改成功返回1</returns>
        public int EditInfo(UserGroupInfo Info)
        {
            return dal.EditInfo(Info);
        }

        /// <summary>
        /// 修改群组头像
        /// </summary>
        /// <param name="Info">群组实体类</param>
        /// <returns>修改成功返回1</returns>
        public int EditHead(UserGroupInfo Info)
        {
            UserGroupInfo tempinfo = new UserGroupInfo();
            tempinfo = this.Info(Info.GroupID);
            int n = dal.EditHead(Info);
            if (n == 1)
            {
                string s_Rootpath = ServerInfo.GetRootPath();
                if (tempinfo.Portrait.IndexOf("grouppic.jpg") <= 0)
                    Public.DelFile(s_Rootpath + tempinfo.Portrait);
            }
            return n;
        }

        /// <summary>
        /// 取得会员总共加入的群组数
        /// </summary>
        /// <param name="UserID">会员编号</param>
        /// <param name="a">join & creat</param>
        /// <returns></returns>
        public int Count(int UserID,string a)
        {
            return dal.Count(UserID, a);
        }

        /// <summary>
        /// 检查是否是自已的群组
        /// </summary>
        /// <param name="GroupID">群组编号</param>
        /// <param name="UserID">会员编号</param>
        /// <returns></returns>
        public bool IsMyGroup(int GroupID, int UserID)
        {
            return dal.IsMyGroup(GroupID, UserID);
        }

        /// <summary>
        /// 群组信息
        /// </summary>
        /// <param name="GroupID">群组编号</param>
        /// <returns>返回群组实体类</returns>
        public UserGroupInfo Info(int GroupID)
        {
            return dal.Info(GroupID);
        }

        /// <summary>
        /// 检查是否有访问群组的权限
        /// </summary>
        /// <param name="GroupID">群编号</param>
        /// <param name="FriendUserID">好友编号</param>
        /// <param name="IsDis">是否讨论区</param>
        /// <returns></returns>
        public bool CheckPublicity(int GroupID, int UserID, bool IsDis)
        {
            bool tf = false;
            JuSNS.Model.UserGroupInfo usergroupinfo = Info(GroupID);
            JuSNS.Home.GroupMember groupmember = new JuSNS.Home.GroupMember();
            switch (usergroupinfo.Publicity)
            {
                case JuSNS.Model.EnumGroupPrivacy.ForWholeSite:
                    tf = true;
                    break;
                case JuSNS.Model.EnumGroupPrivacy.ForSemiOvert:
                    if (IsDis)
                    {
                        if (groupmember.IsGroupMember(UserID, GroupID))
                            tf = true;
                    }
                    else
                        tf = true;
                    break;
                case JuSNS.Model.EnumGroupPrivacy.ForGroupMember:
                    if (groupmember.IsGroupMember(UserID, GroupID))
                        tf = true;
                    break;
            }
            return tf;
        }

        /// <summary>
        /// 取得隐私名称
        /// </summary>
        /// <param name="EnumPrivacy">类型</param>
        /// <returns></returns>
        public static string getPublicityName(JuSNS.Model.EnumGroupPrivacy EnumPrivacy)
        {
            string s_Name = "";
            switch (EnumPrivacy)
            {
                case JuSNS.Model.EnumGroupPrivacy.ForWholeSite:
                    s_Name = "公开";
                    break;
                case JuSNS.Model.EnumGroupPrivacy.ForGroupMember:
                    s_Name = "私密";
                    break;
                case JuSNS.Model.EnumGroupPrivacy.ForSemiOvert:
                    s_Name = "半公开";
                    break;
            }
            return s_Name;
        }
        /// <summary>
        /// 取得指定用户群组
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public List<UserGroupInfo> GetUserGroup(int UserID)
        {
            return dal.GetUserGroup(UserID);
        }

        public DataTable GetUserGroupDis(int GroupID, int TopNum)
        {
            return dal.GetUserGroupDis(GroupID, TopNum);
        }
    }
}
