package game

import (
	"fmt"
	languageCfg "server/conf/language"
	notification_cfg "server/conf/notification"
	GoUtil "server/game_util"
	"server/msg"
)

const (
	NOTIFY_TYPE_FRIEND_APPLY = 1
	NOTIFY_TYPE_PETROOM_GAME = 2
)

/*
对方来petroom游戏的通知
每天最多通知3次
*/
func NotifyPetroomGame(PlayerId int) {
	PlayerSimpleData := G_GameLogicPtr.GetSimplePlayerByUid(PlayerId)
	if !checkLogout(PlayerSimpleData) {
		return
	}
	count, last := GetPetroomGameNotification(PlayerId)
	cooldown, dailyLimit := notification_cfg.GetPetroomGameNotificationCooldown()
	if count >= dailyLimit {
		return
	}
	if GoUtil.Now()-last < int64(cooldown*onehour) {
		return
	}
	titlekey, infokey := notification_cfg.GetPetroomGameNotificationMsg()
	title := languageCfg.GetLanguage(msg.LANG_TYPE(PlayerSimpleData.Lang), titlekey)
	info := languageCfg.GetLanguage(msg.LANG_TYPE(PlayerSimpleData.Lang), infokey)
	GoUtil.NotifyPlayer(GoUtil.Int(PlayerSimpleData.Account), NOTIFY_TYPE_PETROOM_GAME, fmt.Sprintf(title, PlayerSimpleData.PetName), fmt.Sprintf(info, PlayerSimpleData.PetName))
	SetPetroomGameNotification(PlayerId, count+1)
}

/*
好友申请通知
好友申请累计两次
*/
func NotifyFriendApply(PlayerId, FriendId int) {
	PlayerSimpleData := G_GameLogicPtr.GetSimplePlayerByUid(PlayerId)
	if !checkLogout(PlayerSimpleData) {
		return
	}
	FriendSimpleData := G_GameLogicPtr.GetSimplePlayerByUid(FriendId)
	if FriendSimpleData == nil {
		return
	}
	count := GetFriendApplyNotification(PlayerId)
	if count > 0 {
		return
	}
	titlekey, infokey := notification_cfg.GetFriendApplyNotificationMsg()
	title := languageCfg.GetLanguage(msg.LANG_TYPE(PlayerSimpleData.Lang), titlekey)
	info := languageCfg.GetLanguage(msg.LANG_TYPE(PlayerSimpleData.Lang), infokey)
	GoUtil.NotifyPlayer(GoUtil.Int(PlayerSimpleData.Account), NOTIFY_TYPE_FRIEND_APPLY, title, fmt.Sprintf(info, FriendSimpleData.Name))
	SetFriendApplyNotification(PlayerId, count+1)
}

/*
玩家需处于离线状态
玩家离线时间少于10分钟时，不发送消息
若用户已连续7天未登入游戏，不再发送通知
*/
func checkLogout(PlayerSimpleData *PlayerSimpleData) bool {
	if PlayerSimpleData == nil {
		return false
	}
	if PlayerSimpleData.Loginout == 0 {
		return false
	}
	now := GoUtil.Now()
	if now-PlayerSimpleData.Loginout > 7*24*3600 {
		return false
	}
	if now-PlayerSimpleData.Loginout < 600 {
		return false
	}
	return true
}
