package game

import (
	"fmt"
	activityCfg "server/conf/activity"
	catnipCfg "server/conf/catnip"
	guesscolorCfg "server/conf/guess_color"
	languageCfg "server/conf/language"
	miningCfg "server/conf/mining"
	passCfg "server/conf/pass"
	raceCfg "server/conf/race"
	"server/game/mod/activity"
	"server/game/mod/item"
	"server/game/mod/mail"
	GoUtil "server/game_util"
	"server/msg"
)

// 活动模块 登录
func (p *Player) ActivityLogin() {
	ItemMod := p.PlayMod.getItemMod()
	// 挖矿
	ActivityId := p.GetActivityId(activity.ACT_TYPE_MINING)
	MiningMod := p.PlayMod.getMiningMod()
	OldId := MiningMod.Login(ActivityId)
	if OldId != 0 {
		ItemId := guesscolorCfg.GetActivityItemId(OldId)
		ItemNum := ItemMod.GetItem(ItemId)
		if ItemNum != 0 {
			ItemMod.AddItem(ItemId, -ItemNum)
			p.SendActivityMail(ItemId, ItemNum, ActivityId, nil)
		}
	}
	// 猜颜色
	ActivityId = p.GetActivityId(activity.ACT_TYPE_GUESS_COLOR)
	GuessColorMod := p.PlayMod.getGuessColorMod()
	OldId = GuessColorMod.Login(ActivityId)
	if OldId != 0 {
		ItemId := guesscolorCfg.GetActivityItemId(OldId)
		ItemNum := ItemMod.GetItem(ItemId)
		if ItemNum != 0 {
			ItemMod.AddItem(ItemId, -ItemNum)
			p.SendActivityMail(ItemId, ItemNum, ActivityId, nil)
		}
	}

	// 赛跑
	ActivityId = p.GetActivityId(activity.ACT_TYPE_RACE)
	RaceMod := p.PlayMod.getRaceMod()
	OldId = RaceMod.Login(ActivityId)
	if OldId != 0 {
		ItemId := guesscolorCfg.GetActivityItemId(OldId)
		ItemNum := ItemMod.GetItem(ItemId)
		if ItemNum != 0 {
			ItemMod.AddItem(ItemId, -ItemNum)
			p.SendActivityMail(ItemId, ItemNum, ActivityId, nil)
		}
	}
	// 猫草大作战
	ActivityId = p.GetActivityId(activity.ACT_TYPE_CATNIP)
	CatnipMod := p.PlayMod.getCatnipMod()
	OldId, CatnipUnReward := CatnipMod.Login(ActivityId)
	if OldId != 0 && len(CatnipUnReward) > 0 {
		// 清空猫草大作战数据无需发邮件
		MailMod := p.PlayMod.getMailMod()
		MailMod.SendMail(&mail.MailStruct{
			Title:     languageCfg.GetLanguage(msg.LANG_TYPE_LANG_CN, "backend_gardenend_mail_title"),
			TitleEn:   languageCfg.GetLanguage(msg.LANG_TYPE_LANG_EN, "backend_gardenend_mail_title"),
			Content:   languageCfg.GetLanguage(msg.LANG_TYPE_LANG_CN, "backend_gardenend_mail_content"),
			ContentEn: languageCfg.GetLanguage(msg.LANG_TYPE_LANG_EN, "backend_gardenend_mail_content"),
			Items:     CatnipUnReward,
			Type:      mail.MAIL_TYPE_NORMAL,
		})
	}

	// 通行证
	ActivityId = p.GetActivityId(activity.ACT_TYPE_PASS)
	PassMod := p.PlayMod.getPassMod()
	OldId = PassMod.Login(ActivityId)
	if OldId != 0 {
		ItemId := passCfg.GetActivityItemId(OldId)
		ItemNum := PassMod.Num
		RewardItems, _ := PassMod.GetRewardItems()
		if ItemNum != 0 {
			ItemMod.AddItem(ItemId, -ItemNum)
			p.SendActivityMail(ItemId, ItemNum, ActivityId, RewardItems)
		}
	}
}

// 发送活动邮件
func (p *Player) SendActivityMail(ItemId, ItemNum, ActivityId int, RewardItems []*item.Item) {
	MailMod := p.PlayMod.getMailMod()
	activity_title_key, mail_title_key, mail_content_key := activityCfg.GetActivityRecycleMail(ActivityId)
	activity_title_zh := languageCfg.GetLanguage(msg.LANG_TYPE_LANG_CN, activity_title_key)
	activity_title_en := languageCfg.GetLanguage(msg.LANG_TYPE_LANG_EN, activity_title_key)
	activity_title_pt := languageCfg.GetLanguage(msg.LANG_TYPE_LANG_PTBR, activity_title_key)
	mt_zh := languageCfg.GetLanguage(msg.LANG_TYPE_LANG_CN, mail_title_key)
	mc_zh := languageCfg.GetLanguage(msg.LANG_TYPE_LANG_CN, mail_content_key)
	mt_en := languageCfg.GetLanguage(msg.LANG_TYPE_LANG_EN, mail_title_key)
	mc_en := languageCfg.GetLanguage(msg.LANG_TYPE_LANG_EN, mail_content_key)
	mt_pt := languageCfg.GetLanguage(msg.LANG_TYPE_LANG_PTBR, activity_title_key)
	mc_pt := languageCfg.GetLanguage(msg.LANG_TYPE_LANG_PTBR, mail_content_key)
	Items := []*item.Item{item.NewItem(ItemId, ItemNum)}
	Items = append(Items, RewardItems...)
	MailMod.SendMail(&mail.MailStruct{
		Title:       fmt.Sprintf(mt_zh, activity_title_zh),
		Content:     fmt.Sprintf(mc_zh, activity_title_zh),
		TitleEn:     fmt.Sprintf(mt_en, activity_title_en),
		ContentEn:   fmt.Sprintf(mc_en, activity_title_en),
		TitlePtBr:   fmt.Sprintf(mt_pt, activity_title_pt),
		ContentPtBr: fmt.Sprintf(mc_pt, activity_title_pt),
		Items:       Items,
		Type:        mail.MAIL_TYPE_NORMAL,
	})
}

// 活动模块 零点更新
func (p *Player) ActivityZeroUpdate() {
	p.ActivityLogin()
	ActivityInfo := p.GetActivityInfo(activity.ACT_TYPE_MINING)
	if ActivityInfo != nil {
		MiningMod := p.PlayMod.getMiningMod()
		MiningMod.ZeroUpdate(ActivityInfo.Id)
	}
	ActivityInfo = p.GetActivityInfo(activity.ACT_TYPE_GUESS_COLOR)
	if ActivityInfo != nil {
		GuessColorMod := p.PlayMod.getGuessColorMod()
		GuessColorMod.ZeroUpdate(ActivityInfo.Id)
	}
	ActivityInfo = p.GetActivityInfo(activity.ACT_TYPE_RACE)
	if ActivityInfo != nil {
		RaceMod := p.PlayMod.getRaceMod()
		RaceMod.ZeroUpdate(ActivityInfo.Id)
	}
	ActivityInfo = p.GetActivityInfo(activity.ACT_TYPE_PASS)
	if ActivityInfo != nil {
		PassMod := p.PlayMod.getPassMod()
		PassMod.ZeroUpdate(ActivityInfo.Id)
	}
	ActivityInfo = p.GetActivityInfo(activity.ACT_TYPE_CATNIP)
	if ActivityInfo != nil {
		CatnipMod := p.PlayMod.getCatnipMod()
		CatnipMod.ZeroUpdate(ActivityInfo.Id)
	}
}

// 获取活动信息
func (p *Player) GetActivityInfo(actType int) *ActivityInfo {
	for _, v := range p.activity {
		if v.Type == actType {
			return v
		}
	}
	return nil
}

// 获取活动ID
func (p *Player) GetActivityId(actType int) int {
	for _, v := range p.activity {
		if v.Type == actType {
			return v.Id
		}
	}
	return 0
}

// 根据活动ID获取活动信息
func (p *Player) GetActivityInfoById(Id int) *ActivityInfo {
	for _, v := range p.activity {
		if v.Id == Id {
			return v
		}
	}
	return nil
}

// 获取活动状态
func (p *Player) GetActivityStatus(actType int) int {
	ActivityInfo := p.GetActivityInfo(actType)
	if ActivityInfo == nil {
		return ACT_STATUS_NOT_START
	}
	Now := GoUtil.Now()
	if Now < ActivityInfo.StartT {
		return ACT_STATUS_NOT_START
	}
	if Now > ActivityInfo.EndT {
		return ACT_STATUS_END
	}
	return ACT_STATUS_START
}

// 挖矿活动数据返回
func (p *Player) MiningBackData() {
	ActivityInfo := p.GetActivityInfo(activity.ACT_TYPE_MINING)
	if ActivityInfo == nil {
		return
	}
	Status := p.GetActivityStatus(activity.ACT_TYPE_MINING)
	Template := miningCfg.GetTemplate(ActivityInfo.Id)
	MiningMod := p.PlayMod.getMiningMod()
	p.PushClientRes(&msg.ResMining{
		Id:       int32(ActivityInfo.Id),
		Status:   int32(Status),
		EndTime:  int32(ActivityInfo.EndT),
		Template: int32(Template),
		Pass:     int32(MiningMod.GetPass()),
		Gem:      GoUtil.IntToInt32(MiningMod.GetGem()),
		Map:      MiningMod.GetMap(),
		Mining:   int32(MiningMod.GetMining()),
	})
}

// 猜颜色活动数据返回
func (p *Player) GuessColorBackData() {
	ActivityInfo := p.GetActivityInfo(activity.ACT_TYPE_GUESS_COLOR)
	if ActivityInfo == nil {
		return
	}

	Status := p.GetActivityStatus(activity.ACT_TYPE_GUESS_COLOR)
	GuessColorMod := p.PlayMod.getGuessColorMod()
	MapList := make([]*msg.GuessColorInfo, 0)
	for _, v := range GuessColorMod.MapList {
		MapList = append(MapList, &msg.GuessColorInfo{
			Map: v,
		})
	}
	p.PushClientRes(&msg.ResGuessColor{
		Id:      int32(ActivityInfo.Id),
		Status:  int32(Status),
		EndTime: int32(ActivityInfo.EndT),
		Pass:    int32(GuessColorMod.Pass),
		Opponent: &msg.Opponent{
			Name:     GuessColorMod.Opponent.Name,
			Face:     int32(GuessColorMod.Opponent.Face),
			Avatar:   int32(GuessColorMod.Opponent.Avatar),
			Progress: int32(GuessColorMod.Opponent.Progress),
		},
		WinTime: int32(GuessColorMod.WinTime),
		MapList: MapList,
		OMap:    GuessColorMod.OMap,
	})
}

// 赛跑活动数据返回
func (p *Player) RaceBackData() {
	ActivityInfo := p.GetActivityInfo(activity.ACT_TYPE_RACE)
	if ActivityInfo == nil {
		return
	}
	Status := p.GetActivityStatus(activity.ACT_TYPE_RACE)
	RaceMod := p.PlayMod.getRaceMod()
	Opponent := make([]*msg.Raceopponent, 0)
	for _, v := range RaceMod.Opponent {
		Opponent = append(Opponent, &msg.Raceopponent{
			Id:       int32(v.Id),
			Name:     v.Name,
			Face:     int32(v.Face),
			Avatar:   int32(v.Avatar),
			Progress: int32(v.Progress),
		})
	}
	p.PushClientRes(&msg.ResRace{
		Id:            int32(ActivityInfo.Id),
		Status:        int32(Status),
		EndTime:       int32(ActivityInfo.EndT),
		Pass:          int32(RaceMod.Pass),
		GameStartTime: int32(RaceMod.StartTime),
		Progress:      int32(RaceMod.Progress),
		GameEndTime:   int32(RaceMod.EndTime),
		Opponent:      Opponent,
		Rank:          int32(RaceMod.Rank),
	})
}

// 红点数据返回
func (p *Player) RedBackData() {
	result := make(map[int32]int32)
	Now := GoUtil.Now()
	for _, v := range p.activity {
		if v.StartT < Now && v.EndT > Now {
			result[int32(v.Type)] = int32(p.GetRed(v))
		}
	}
	p.PushClientRes(&msg.ResActRed{Red: result})
}

// 通行证活动数据返回
func (p *Player) ActPassBackData() {
	ActivityInfo := p.GetActivityInfo(activity.ACT_TYPE_PASS)
	if ActivityInfo == nil {
		return
	}
	Status := p.GetActivityStatus(activity.ACT_TYPE_PASS)
	Template := passCfg.GetTemplate(ActivityInfo.Id)
	PassMod := p.PlayMod.getPassMod()
	p.PushClientRes(&msg.ResActPass{
		Id:       int32(ActivityInfo.Id),
		Status:   int32(Status),
		Template: int32(Template),
		EndTime:  int32(ActivityInfo.EndT),
		LowPass:  PassMod.LowPass > 0,
		HighPass: PassMod.HighPass > 0,
		Score:    int32(PassMod.Num),
		Reward:   GoUtil.IntToInt32(PassMod.Reward),
	})
}

// 获取活动道具
func (p *Player) GetActivityItem(ActType []int) []*item.Item {
	Items := make([]*item.Item, 0)
	for _, v := range ActType {
		Status := p.GetActivityStatus(v)
		ActivityInfo := p.GetActivityInfo(v)
		if ActivityInfo == nil {
			continue
		}
		if Status != ACT_STATUS_START {
			continue
		}
		switch v {
		case activity.ACT_TYPE_MINING:
			Item := miningCfg.GetLoseItem(ActivityInfo.Id)
			Items = item.Merge(Items, Item)
		case activity.ACT_TYPE_GUESS_COLOR:
			Item := guesscolorCfg.GetLoseItem(ActivityInfo.Id)
			Items = item.Merge(Items, Item)
		case activity.ACT_TYPE_RACE:
			ItemId := raceCfg.GetCoin(ActivityInfo.Id)
			Item := item.NewItem(ItemId, 1)
			Items = append(Items, Item)
		case activity.ACT_TYPE_PASS:
			ItemId := passCfg.GetActivityItemId(ActivityInfo.Id)
			Item := item.NewItem(ItemId, 1)
			Items = append(Items, Item)
		}
	}
	return Items
}

// 猫草大作战活动数据返回
func (p *Player) CatnipBackData() {
	ActivityInfo := p.GetActivityInfo(activity.ACT_TYPE_PASS)
	if ActivityInfo == nil {
		return
	}
	CatnipMod := p.PlayMod.getCatnipMod()
	Status := p.GetActivityStatus(activity.ACT_TYPE_CATNIP)
	if CatnipMod == nil {
		return
	}
	FriendMod := p.PlayMod.getFriendMod()
	GameList := make([]*msg.CatnipGame, 0)
	for _, v := range CatnipMod.Game {
		GameInfo := &msg.CatnipGame{
			Id:             int32(v.Id),
			Progress:       int32(v.Progress),
			Status:         int32(v.Status),
			Reward:         GoUtil.SliceIntToInt32(v.Reward),
			Emoji:          int32(v.EmojiId),
			FriendProgress: int32(v.PartnerAdd),
			SendEmoji:      int32(v.SendEmoji),
		}
		if v.Partner != 0 {
			PlayerData := G_getGameLogic().GetResSimplePlayerByUid(v.Partner)
			if PlayerData != nil {
				GameInfo.Partner = PlayerData
			}
		}

		GameList = append(GameList, GameInfo)
	}
	tmpData := make(map[int]*msg.CatnipInvite)
	InviteList := make([]*msg.CatnipInvite, 0)
	for uid, info := range CatnipMod.InviteList {
		tmpData[uid] = &msg.CatnipInvite{
			Uid:  int64(uid),
			Time: info.Time,
			Type: 1,
		}
	}
	for uid, info := range CatnipMod.BeInvitedList {
		tmpData[uid] = &msg.CatnipInvite{
			Uid:  int64(uid),
			Time: info.Time,
			Type: 2,
		}
	}

	for uid := range FriendMod.NewFriendList {
		partnerList := p.GetCatnipPartner(uid)
		if len(GoUtil.IntSlice(partnerList)) >= 4 {
			tmpData[uid] = &msg.CatnipInvite{
				Uid:  int64(uid),
				Type: 3,
			}
			continue
		}
		if _, ok := tmpData[uid]; !ok {
			tmpData[uid] = &msg.CatnipInvite{
				Uid:  int64(uid),
				Type: 0,
			}
		}
	}
	for _, v := range CatnipMod.Game {
		if invite, ok := tmpData[v.Partner]; ok {
			invite.Type = 4 // 已参与游戏的好友不显示邀请
		}
	}
	for _, v := range tmpData {
		ResPlayerSimple := G_getGameLogic().GetResSimplePlayerByUid(int(v.Uid))
		if ResPlayerSimple != nil {
			v.Player = ResPlayerSimple
		}
		InviteList = append(InviteList, v)
	}

	Template := catnipCfg.GetTemplateId(CatnipMod.Id)
	res := &msg.ResCatnip{
		Id:         int32(CatnipMod.Id),
		EndTime:    int32(ActivityInfo.EndT),
		Status:     int32(Status),
		Template:   int32(Template),
		GameList:   GameList,
		Multiply:   int32(CatnipMod.Mul),
		FriendList: InviteList,
	}
	p.PushClientRes(res)
}
