package game

import (
	"server/db"
	"server/game/mod/msg"
	GoUtil "server/game_util"
	"server/pkg/github.com/name5566/leaf/log"
	"sort"
)

func (p *Player) GetVisitorPlayer() int {
	PlayroomMod := p.PlayMod.getPlayroomMod()
	VisitorList := PlayroomMod.GetVisitor()                 // 到访用户
	TodayVisitedUsers := PlayroomMod.GetTodayVisitedUsers() // 今日已互动用户
	FriendMod := p.PlayMod.getFriendMod()
	type sortData struct {
		Uid  int
		Time int64
	}
	PlayerList := make([]sortData, 0)
	PlayerList2 := make([]sortData, 0)
	Now := GoUtil.Now()
	/**
		排除当日玩家已对其发起过交互的用户
	优先选择24小时内曾经与玩家进行过宠物交互的好友
	若存在复数对象的情况下，优先选择交互发生时间与当前时间最近的好友
	若不存在符合条件的用户，则选择24小时内曾经与玩家进行过宠物交互的陌生用户
	若存在复数对象的情况下，优先选择交互发生时间与当前时间最近的用户
	若不存在符合条件的用户，则选择24小时内登入过游戏且上次登入时间与当前时间最近的好友
	若不存在符合条件的用户，则依据以上用户推荐算法，选择一位随机推荐用户，并且在下次触发式订单完成时，不再排除已发起过交互的用户
	*/
	for k, v := range VisitorList {
		if GoUtil.InArray(k, TodayVisitedUsers) {
			continue
		}
		if v.Time < Now-86400 {
			continue
		}
		if FriendMod.CheckFriend(k) {
			PlayerList = append(PlayerList, sortData{k, v.Time})
		} else {
			PlayerList2 = append(PlayerList, sortData{k, v.Time})
		}
	}
	if len(PlayerList) != 0 {
		sort.Slice(PlayerList, func(i, j int) bool {
			return PlayerList[i].Time < PlayerList[j].Time
		})
		return PlayerList[0].Uid
	}
	if len(PlayerList2) != 0 {
		sort.Slice(PlayerList2, func(i, j int) bool {
			return PlayerList2[i].Time < PlayerList2[j].Time
		})
		return PlayerList2[0].Uid
	}
	// 若不存在符合条件的用户，则选择24小时内登入过游戏且上次登入时间与当前时间最近的好友
	var recentFriendUid int
	var recentLoginTime int64 = 0
	for uid := range FriendMod.GetFriendList() {
		if uid == int(p.M_DwUin) {
			continue
		}
		if GoUtil.InArray(uid, TodayVisitedUsers) {
			continue
		}
		ps := G_GameLogicPtr.GetSimplePlayerByUid(uid)
		if ps == nil {
			continue
		}
		if GoUtil.Now()-ps.Loginout > 86400 { // 24小时内登录过
			continue
		}
		if ps.Loginout > recentLoginTime {
			recentLoginTime = ps.Loginout
			recentFriendUid = uid
		}
	}
	if recentFriendUid != 0 {
		return recentFriendUid
	}
	PlayerList3 := G_GameLogicPtr.RankMgr.getAllRank(RANK_TYPE_USER)
	PlayerList4 := make([]int, 0)
	for _, v := range PlayerList3 {
		if v.Uid == int(p.M_DwUin) {
			continue
		}
		PlayerSimpleData := G_GameLogicPtr.GetSimplePlayerByUid(v.Uid)
		if PlayerSimpleData.Level < 15 {
			continue
		}
		PlayerList4 = append(PlayerList4, v.Uid)
	}
	L := GoUtil.RandSliceNum(PlayerList4, 1)
	if len(L) == 0 {
		return 0
	}
	return L[0]
}

func GetRecommendPlayer(p *Player, Num int) []int {
	PlayerList := G_GameLogicPtr.RankMgr.getAllRank(RANK_TYPE_USER)
	PlayerList1 := make([]int, 0)
	FriendMod := p.PlayMod.getFriendMod()
	for _, v := range PlayerList {
		if v.Uid == int(p.M_DwUin) {
			continue
		}
		if FriendMod.CheckSendApply(v.Uid) {
			continue
		}
		if FriendMod.CheckFriend(v.Uid) {
			continue
		}
		PlayerSimpleData := G_GameLogicPtr.GetSimplePlayerByUid(v.Uid)
		if GoUtil.Now()-PlayerSimpleData.Loginout > 86400 {
			continue
		}
		if PlayerSimpleData.Level < 4 {
			continue
		}
		PlayerList1 = append(PlayerList1, v.Uid)
	}
	if len(PlayerList1) == 0 {
		for _, v := range PlayerList {
			if v.Uid == int(p.M_DwUin) {
				continue
			}
			PlayerList1 = append(PlayerList1, v.Uid)
		}
	}
	return GoUtil.RandSliceNum(PlayerList1, Num)
}

func GetUidByFaceBook(Fb string) (int, error) {
	sqlStr := "SELECT dwUin FROM t_player_baseinfo WHERE FaceBookId = ?"
	type Result struct {
		Uid int `db:"dwUin"`
	}
	R := Result{}
	err := db.SqlDb.Get(&R, sqlStr, Fb)
	log.Debug("Fb :%s;Uid :%d", Fb, R.Uid)
	return R.Uid, err
}

func NotifyAllFriend(p *Player, m1 *msg.Msg) {
	m := m1.Clone()
	FriendMod := p.PlayMod.getFriendMod()
	for k := range FriendMod.GetFriendList() {
		if k == int(p.M_DwUin) {
			continue
		}
		m.To = k
		FriendMgrSend(m)
	}
}
