package friends

import (
	"server/console"
	usercenter "server/datacenter"
	requestaddfriend "server/datacenter/request_add_friend"
	agentmanager "server/game/agentManager"
	"server/msg"

	"github.com/name5566/leaf/gate"
)

// 获取用户的好友列表
func GetFriendsListByUserId(args []interface{}) {
	m := args[0].(*msg.ReqFriendList)
	a := args[1].(gate.Agent)
	UserId := m.UserId
	mysql_user_data, err := usercenter.GetUserByID(UserId)
	if err != nil {
		a.WriteMsg(&msg.ResFriendList{
			Success: false,
			ErrMsg: &msg.MsgError{
				ErrorCode: 101,
				ErrorMsg:  "error",
			},
		})
		return
	}
	users, err := mysql_user_data.GetAllFriend()

	if err == nil {
		var temp = make([]*msg.UserInfo, 0)
		for _, v := range users {
			temp = append(temp, &msg.UserInfo{
				UserId:       v.UserID,
				MHead:        v.Head,
				Name:         v.Name,
				OnlineStatus: int32(v.OnlineStatus),
			})
		}
		a.WriteMsg(&msg.ResFriendList{
			Success: true,
			ErrMsg:  nil,
			List:    temp,
		})
	} else {
		a.WriteMsg(&msg.ResFriendList{
			Success: false,
			ErrMsg: &msg.MsgError{
				ErrorCode: 101,
				ErrorMsg:  err.Error(),
			},
		})
	}

}

func DeleteFriend(args []interface{}) {

}

// 获取好友申请列表
func GetFriendRquestList(args []interface{}) {
	m := args[0].(*msg.ReqFriendRequestList)
	a := args[1].(gate.Agent)
	user_id := m.UserId
	list, err := requestaddfriend.GetRequests(user_id)
	if err != nil {
		a.WriteMsg(&msg.ResFriendRequestList{
			Success: false,
			ErrMsg: &msg.MsgError{
				ErrorCode: 101,
				ErrorMsg:  "error",
			},
		})
		return
	}
	var ids = make([]string, 0)
	var user_list = make(map[string]*requestaddfriend.FriendRequest, 0)
	for _, v := range list {
		ids = append(ids, v.FromUserID)
		user_list[v.FromUserID] = v
	}
	users, err := usercenter.GetUsersByIDs(ids)

	if err == nil {
		var temp = make([]*msg.FriendRequestItem, 0)
		for _, v := range users {
			temp = append(temp, &msg.FriendRequestItem{
				RequestMsg: user_list[v.UserID].Message,
				Status:     int32(user_list[v.UserID].Status),
				CreateTime: user_list[v.UserID].CreateTime.GoString(),
				Info: &msg.UserInfo{
					UserId: v.UserID,
					MHead:  v.Head,
					Name:   v.Name,
				},
			})
		}
		a.WriteMsg(&msg.ResFriendRequestList{
			Success: true,
			List:    temp,
		})
	} else {
		a.WriteMsg(&msg.ResFriendRequestList{
			Success: false,
			ErrMsg: &msg.MsgError{
				ErrorCode: 101,
				ErrorMsg:  "error",
			},
		})
	}

}

// 添加好友请求
func AddFriendrRquest(args []interface{}) {
	m := args[0].(*msg.ReqAddFriend)
	a := args[1].(gate.Agent)
	FromUserInfo := a.UserData().(*msg.UserInfo)
	FromUserID := FromUserInfo.UserId
	ToUserID := m.ToUserID
	requestaddfriend.CreateFriendRequest(FromUserID, ToUserID, m.Msg)
	NotifyFriendAddFriend(ToUserID, FromUserInfo)
}

// 通知添加好友请求
func NotifyFriendAddFriend(ToUserID string, FromUserID *msg.UserInfo) {
	ag := agentmanager.GetAgentByUserID(ToUserID)
	if ag != nil {
		ag.WriteMsg(&msg.RecvAddFriendRequest{
			Info: &msg.UserInfo{
				MHead:  FromUserID.MHead,
				Name:   FromUserID.Name,
				UserId: FromUserID.UserId,
			},
		})
	}
}

// 反馈操作好友请求
func OptFriendrRquest(args []interface{}) {
	m := args[0].(*msg.OptAddFriendRequest)
	var user_data *usercenter.User
	var err error

	user_data, err = usercenter.GetUserByID(m.Info.FromUserID)
	if err == nil {
		user_data.AddFriend(m.Info.ToUserID)
	}

	user_data, err = usercenter.GetUserByID(m.Info.ToUserID)
	if err == nil {
		user_data.AddFriend(m.Info.FromUserID)
	}

	var request_data *requestaddfriend.FriendRequest
	request_data, err = requestaddfriend.ProcessFriendRequest(m.Info.RequestID, int(m.Info.Status))

	if err == nil {
		request_data.Status = int(m.Info.Status)
		NotifyOptFriend(m.Info.FromUserID, request_data)
	} else {
		console.Log("反馈操作好友请求 error:", err)
	}
}

// 通知好友操作的结果
func NotifyOptFriend(FromUserID string, request_data *requestaddfriend.FriendRequest) {
	ag := agentmanager.GetAgentByUserID(FromUserID)
	if ag != nil {
		ag.WriteMsg(&msg.NotifyOptFriend{
			Info: &msg.RequestAddFriendItem{
				Status:   int32(request_data.Status),
				ToUserID: request_data.ToUserID,
			},
		})
	}
}

// 搜索玩家
func SearchUser(args []interface{}) {
	m := args[0].(*msg.SearchUser)
	a := args[1].(gate.Agent)
	user_data, err := usercenter.GetUserByID(m.UserId)
	if err != nil {
		a.WriteMsg(&msg.ResSearchUser{
			Success: false,
			ErrMsg: &msg.MsgError{
				ErrorCode: 101,
				ErrorMsg:  err.Error(),
			},
		})
	} else {
		a.WriteMsg(&msg.ResSearchUser{
			Success: true,
			ErrMsg:  nil,
			Info: &msg.UserInfo{
				MHead:  user_data.Head,
				Name:   user_data.Name,
				UserId: user_data.UserID,
			},
		})
	}
}

// 通知给在线的好友自己上线

// 通知给在线的好友自己下线

// 发送好友聊天消息

// 筛选在线的好友
