using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Net.Mail;
using System.Text.RegularExpressions;
using JuSNS.Model;
using JuSNS.Factory;
using JuSNS.Common;
using JuSNS.Config;

namespace JuSNS.Home
{
    /// <summary>
    /// 好友类
    /// </summary>
    public class Friend
    {

        private IFriend dal;
        /// <summary>
        /// 构造函数
        /// </summary>
        public Friend()
        {
            dal = DataAccess.CreateFriend();
        }
        
        /// <summary>
        /// 得到好友分类
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public IDataReader GetFriendClass(int UserID)
        {
            return dal.GetFriendClass(UserID);
        }
        /// <summary>
        /// 发送交友请求
        /// </summary>
        /// <param name="proposer"></param>
        /// <param name="friend"></param>
        /// <returns></returns>
        public int Request(int proposer, int friend)
        {
            return dal.Request(proposer, friend);
        }
        /// <summary>
        /// 回应好友请求
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="requestid"></param>
        /// <param name="reply"></param>
        /// <param name="detail"></param>
        /// <returns></returns>
        public int Response(int userid, int requestid, EnumReply reply, string detail)
        {
            return dal.Response(userid, requestid, reply, detail);
        }
        /// <summary>
        /// 删除好友
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="friendid"></param>
        /// <returns></returns>
        public int Diffidation(int userid, int friendid)
        {
            return dal.Diffidation(userid, friendid);
        }
        /// <summary>
        /// 判断是否是好友
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="friendid"></param>
        /// <returns></returns>
        public bool IsFriends(int userid, int friendid)
        {
            return dal.IsFriends(userid, friendid);
        }
        /*
        /// <summary>
        /// 修改好友描述
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="detail"></param>
        /// <returns></returns>
        public int EditFriendsDetail(int userid, int friendid, List<FriendDescInfo> detail)
        {
            return dal.EditFriendsDetail(userid, friendid, detail);
        }
        
        /// <summary>
        /// 获取好友描述信息
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="friendid"></param>
        /// <returns></returns>
        public List<FriendDescInfo> GetDetail(int userid, int friendid)
        {
            return dal.GetDetail(userid, friendid);
        }
        /// <summary>
        /// 回复好友描述请求
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="friendid"></param>
        /// <param name="isconfirm"></param>
        /// <returns></returns>
        public int ReplyFriendDetail(int userid, int friendid, bool isconfirm)
        {
            return dal.ReplyFriendDetail(userid, friendid, isconfirm);
        }
        */

        public int SetFriendClassID(int UserID, int ClassID, int ID)
        {
            return dal.SetFriendClassID(UserID, ClassID, ID);
        }

        /// <summary>
        /// 打招呼,非好友也可以打招呼
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="friendid"></param>
        /// <returns></returns>
        public int Greet(int userid, int friendid)
        {
            return dal.Greet(userid, friendid);
        }
        /// <summary>
        /// 邮件邀请好友
        /// </summary>
        /// <param name="userid">邀请者ID</param>
        /// <param name="emails">被邀请者的email</param>
        /// <param name="postscript">附言</param>
        /// <returns>0成功,1该邮箱已经注册,2该邮箱已被邀请,3写入数据库失败</returns>
        public Dictionary<string, int> InviteFriends(int userId, string userName, string emails, string postscript, bool isRepeat)
        {
            string[] email = Regex.Split(emails, @"\r\n|,");
            return dal.InviteFriends(userId, userName, email, postscript, isRepeat);
        }
        /// <summary>
        /// 寻找好友
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public int SeekFriends(int userid)
        {
            return 0;
        }
        #region 留言
        /// <summary>
        /// 发送留言
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="friendid"></param>
        /// <param name="word"></param>
        /// <returns></returns>
        public int SendLeaveword(int userid, int friendid, string word)
        {
            return dal.SendLeaveword(userid, friendid, word);
        }
        /// <summary>
        /// 删除留言
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="wordid"></param>
        /// <returns></returns>
        public int DeleteLeaveword(int userid, int wordid)
        {
            return dal.DeleteLeaveword(userid, wordid);
        }
        #endregion
        /// <summary>
        /// 判断是否在同一网络
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="friendid"></param>
        /// <returns></returns>
        public bool IsInSameNetwork(int userid, int friendid)
        {
            return dal.IsInSameNetwork(userid, friendid);
        }
        /// <summary>
        /// 取得好友
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="friendname"></param>
        /// <returns></returns>
        public Dictionary<int, string> GetFriends(int userid, string friendname)
        {
            return dal.GetFriends(userid, friendname);
        }

        /// <summary>
        /// 取得好友(群组)
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="GroupID"></param>
        /// <param name="friendname"></param>
        /// <returns></returns>
        public Dictionary<int, string> GetFriendsForGroup(int userid, int GroupID, string friendname)
        {
            return dal.GetFriendsForGroup(userid, GroupID, friendname);
        }
        /// <summary>
        /// 查看两人关系
        /// </summary>
        /// <param name="myid"></param>
        /// <param name="otherid"></param>
        /// <returns>0无任何关系,1本人,2好友,3已发出好友申请,等待确认好友申请,4被申请</returns>
        public int GetRelation(int myid, int otherid)
        {
            if (myid == otherid)
                return 1;
            else
                return dal.GetRelation(myid, otherid);
        }
        /// <summary>
        /// 好友邀请统计
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="confirmed">确认了的</param>
        /// <param name="pending">还未确认的</param>
        public void InviteStat(int userId, out int confirmed, out int pending)
        {
            dal.InviteStat(userId, out confirmed, out pending);
        }
        /// <summary>
        /// 查看是否具有查看好友列表的权限
        /// </summary>
        /// <param name="guestId"></param>
        /// <param name="masterId"></param>
        /// <returns></returns>
        public bool CheckListAuthority(int guestId, int masterId)
        {
            return dal.CheckListAuthority(guestId, masterId);
        }
        /// <summary>
        /// 寻找MSN,Gtalk好友
        /// </summary>
        /// <param name="eamil">邮件列表</param>
        /// <returns>返回数据表</returns>
        public DataTable FindFriend(string eamil)
        {
            return dal.FindFriend(eamil);
        }

        public IList<FriendInfo> GetList(int pageIndex, int pageSize, out int recordCount, out int pageCount, int userId, int network, string searchName,string c, out Dictionary<NetworkInfo, int> stat)
        {
            return dal.GetList(pageIndex, pageSize, out recordCount, out pageCount, userId, network, searchName, c, out stat);
        }

        /// <summary>
        /// 更新分组名称
        /// </summary>
        /// <param name="UserID"></param>
        /// <param name="CName"></param>
        /// <param name="ID"></param>
        /// <returns></returns>
        public int UpdateFriendClass(int UserID, string CName, int ID)
        {
            return dal.UpdateFriendClass(UserID, CName, ID);
        }

        /// <summary>
        /// 取得未分组的好友
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public int GetUnClassFriend(int UserID)
        {
            return dal.GetUnClassFriend(UserID);
        }

        /// <summary>
        /// 删除好友分组
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="UserID"></param>
        public void DelFriendClass(int ID, int UserID)
        {
            dal.DelFriendClass(ID, UserID);
        }

        /// <summary>
        /// 增加分组
        /// </summary>
        /// <param name="CName"></param>
        /// <param name="UserID"></param>
        public void AddFriendClass(string CName, int UserID)
        {
            dal.AddFriendClass(CName, UserID);
        }

        /// <summary>
        /// 根据用户Email获得ID
        /// </summary>
        /// <param name="Email"></param>
        /// <returns></returns>
        public int GetUserIDFromEmail(string Email)
        {
          return   dal.GetUserIDFromEmail(Email);
        }
    }
}
