package game

import (
	chargeCfg "server/conf/charge"
	playroomCfg "server/conf/playroom"
	"server/game/mod/item"
	limitedTimeEvent "server/game/mod/limited_time_event"
	GoUtil "server/game_util"
	proto "server/msg"
)

func (p *Player) NotifyPlayroomTask() {
	PlayroomMod := p.PlayMod.getPlayroomMod()
	m := &proto.NotifyPlayroomTask{
		DailyTask:       PlayroomMod.BackDataTask(),
		DailyTaskReward: GoUtil.IntToInt32(PlayroomMod.DailyTaskReward),
	}
	p.PushClientRes(m)
}

func (p *Player) PlayroomBackData() {
	r := &proto.ResPlayroom{}
	PlayroomMod := p.PlayMod.getPlayroomMod()
	FriendMod := p.PlayMod.getFriendMod()
	r.Status = int32(PlayroomMod.Status)
	r.Items = item.ItemToMsg(PlayroomMod.Reward)

	Opponent := make([]*proto.RoomOpponent, 0, len(PlayroomMod.Visitor))
	FriendList := make([]*proto.FriendRoom, 0, len(FriendMod.GetFriendList()))
	if PlayroomMod.Target == 0 {
		PlayroomMod.SetTarget(p.GetVisitorPlayer())
	}
	TargerRoom := &proto.FriendRoom{}
	if PlayroomMod.Target != 0 {
		PlayerData := G_GameLogicPtr.GetSimplePlayerByUid(PlayroomMod.Target)
		if PlayerData != nil {
			TargerRoom.Uid = int64(PlayerData.Uid)
			TargerRoom.Name = PlayerData.Name
			TargerRoom.Face = int32(PlayerData.Face)
			TargerRoom.Avatar = int32(PlayerData.Avatar)
		} else {
			PlayroomMod.Target = 0
		}
	}
	for k, v := range PlayroomMod.Visitor {
		ps := G_GameLogicPtr.GetSimplePlayerByUid(k)
		if ps == nil {
			continue
		}
		if !FriendMod.CheckFriend(k) {
			Opponent = append(Opponent, &proto.RoomOpponent{
				Uid:      int64(k),
				Name:     ps.Name,
				Face:     int32(ps.Face),
				Avatar:   int32(ps.Avatar),
				LastTime: int32(v.Time),
			})
		}
	}
	Friend := FriendMod.GetFriendList()
	for k := range Friend {
		ps := G_GameLogicPtr.GetSimplePlayerByUid(k)
		if ps == nil {
			continue
		}
		Times, _ := PlayroomMod.GetVisitorInfo(k)
		FriendList = append(FriendList, &proto.FriendRoom{
			Uid:    int64(k),
			Name:   ps.Name,
			Face:   int32(ps.Face),
			Avatar: int32(ps.Avatar),
			Times:  int32(Times),
		})
	}
	r.Opponent = Opponent
	r.Friend = FriendList
	r.Target = TargerRoom

	collectList := PlayroomMod.GetCollect()
	Collect := make([]*proto.PlayroomCollectInfo, 0, len(collectList))
	for _, v := range collectList {
		Collect = append(Collect, &proto.PlayroomCollectInfo{
			Id:      int32(v.Id),
			AddTime: v.AddTime,
			EndTime: v.EndTime,
			Label:   v.Label,
		})
	}
	r.Collect = Collect
	Dress := make(map[int32]*proto.PlayroomDress)
	for _, v := range PlayroomMod.GetDress() {
		PlayroomDress, ok := Dress[int32(v.Part)]
		if !ok {
			PlayroomDress = &proto.PlayroomDress{}
		}
		PlayroomDress.List = append(PlayroomDress.List, &proto.PlayroomDressInfo{
			Id:      int32(v.Id),
			AddTime: int64(v.AddTime),
			EndTime: int64(v.EndTime),
			Label:   v.Label,
		})
		Dress[int32(v.Part)] = PlayroomDress
	}
	ChipMessage := make([]*proto.ChipInfo, 0, len(PlayroomMod.ChipList))
	for _, v := range PlayroomMod.ChipList {
		ChipMessage = append(ChipMessage, &proto.ChipInfo{
			Uid:     int64(v.Uid),
			EmojiId: int32(v.Emoji),
		})
	}
	AdWatch := make([]*proto.AdItem, 0, len(PlayroomMod.ADItem))
	for k, v := range PlayroomMod.ADItem {
		AdWatch = append(AdWatch, &proto.AdItem{
			Watch:     int32(v.Watch),
			LastWatch: int32(v.LastTime),
			ItemId:    int32(k),
		})
	}

	r.Dress = Dress
	r.DressSet = GoUtil.MapIntToInt32(PlayroomMod.GetDressSet())
	petAirList := PlayroomMod.GetPetAir()
	PetAir := make([]*proto.PlayroomAirInfo, 0, len(petAirList))
	for _, v := range petAirList {
		PetAir = append(PetAir, &proto.PlayroomAirInfo{
			Id:      int32(v.Id),
			AddTime: int64(v.AddTime),
			Label:   v.Label,
		})
	}
	weeklyDiscount := make(map[int32]*proto.WeeklyDiscountInfo)
	ChargeMod := p.PlayMod.getChargeMod()
	LimitedEventMod := p.PlayMod.getLimitedTimeEventMod()
	// 优惠日开启或者猫咪闪促开启
	if ChargeMod.IsWeeklyDiscountDay() || LimitedEventMod.CheckExist(limitedTimeEvent.EVENT_TYPE_CAT_DAY_SALE) {
		w1 := playroomCfg.GetShopWeeklyLimit()
		for k, v := range w1 {
			limitNum := PlayroomMod.WeeklyDiscount[k]
			weeklyDiscount[int32(k)] = &proto.WeeklyDiscountInfo{
				Id:       int32(k),
				Discount: int32(v.Discount),
				Count:    int32(v.WeeklyLimit - limitNum),
			}
		}
	}
	r.WeeklyDiscount = weeklyDiscount
	r.PetAir = PetAir
	r.PetAirSet = int32(PlayroomMod.GetPetAirSet())
	r.Chip = ChipMessage
	r.StartTime = int32(PlayroomMod.Starttime)
	r.WorkStatus = int32(PlayroomMod.WorkStatus)
	r.Playroom = GoUtil.MapIntToInt32(PlayroomMod.GetRoom())
	r.Mood = GoUtil.MapIntToInt32(PlayroomMod.GetMood())
	r.Physiology = GoUtil.MapIntToInt32(PlayroomMod.GetPhysiologyList())
	r.AllMood = int32(PlayroomMod.AllMood)
	r.Jackpot = int32(PlayroomMod.JackpotNum)
	r.Upvote = int32(PlayroomMod.Upvote)
	r.RoomPoint = int32(PlayroomMod.RoomPoint)
	r.Unlock = PlayroomMod.GetUnlockIds()
	r.DailyTaskReward = GoUtil.IntToInt32(PlayroomMod.DailyTaskReward)
	r.DailyTask = PlayroomMod.BackDataTask()
	r.Kiss = int32(p.GetPlayroomKiss(int(p.M_DwUin)))
	r.Revenge = PlayroomMod.RevengeUid
	r.InteractNum = int32(PlayroomMod.InteractNum)
	r.AdItem = AdWatch
	p.PushClientRes(r)
}

func (p *Player) PlayroomVisit(Uid int) {
	if Uid == 0 {
		p.PushClientRes(&proto.ResPlayroomInfo{})
		return
	}
	PlayroomMod := p.PlayMod.getPlayroomMod()
	r := &proto.ResPlayroomInfo{}
	PlayerData := G_GameLogicPtr.GetSimplePlayerByUid(Uid)
	if PlayerData == nil {
		p.PushClientRes(&proto.ResPlayroomInfo{})
		return
	}
	Now := GoUtil.Now()
	Work := false
	if PlayerData.WorkStart > 0 && PlayerData.WorkStart+86400 > Now {
		Work = true
	}
	r.Uid = int64(Uid)
	r.Name = PlayerData.Name
	r.Face = int32(PlayerData.Face)
	r.Avatar = int32(PlayerData.Avatar)
	r.Playroom = GoUtil.MapIntToInt32(PlayerData.Playroom)
	r.GameId = int32(PlayroomMod.GameId)
	r.Defense = Work
	r.Emoji = GoUtil.MapIntToInt32(PlayerData.Emoji)
	r.PetName = PlayerData.PetName
	Items := make(map[int32]*proto.ItemInfo)
	for k, v := range PlayroomMod.GameReward {
		Items[int32(k)] = &proto.ItemInfo{
			Id:  int32(v.Id),
			Num: int32(v.Num),
		}
	}
	r.Upvote = GoUtil.InArray(Uid, PlayroomMod.UpvoteList)
	r.Items = Items
	r.Status = int32(PlayroomMod.GameStatus)
	r.UpvoteCount = int32(p.GetPlayroomUpvote(Uid))
	r.Chip = int32(p.GetPlayroomChip(Uid))
	r.Kiss = int32(p.GetPlayroomKiss(Uid))
	r.DressSet = GoUtil.MapIntToInt32(PlayerData.DressSet)
	p.PushClientRes(r)
}

func (p *Player) NotifyPlayroomKiss() {
	m := &proto.NotifyPlayroomKiss{
		Kiss: int32(p.GetPlayroomKiss(int(p.M_DwUin))),
	}
	p.PushClientRes(m)
}

func (p *Player) BackUserInfo() {
	BaseMod := p.PlayMod.getBaseMod()
	FaceMod := p.PlayMod.getFaceMod()
	AvatarMod := p.PlayMod.getAvatarMod()
	DecorateMod := p.PlayMod.getDecorateMod()
	p.PushClientRes(&proto.UserInfo{
		Uid:         p.M_DwUin,
		Nickname:    BaseMod.NickName,
		Avatar:      int32(AvatarMod.SetId),
		Face:        int32(FaceMod.SetId),
		DecorateCnt: int32(DecorateMod.DecorateNum),
		AvatarList:  AvatarMod.BackData(),
		FaceList:    FaceMod.BackData(),
		EmojiList:   p.PlayMod.getEmojiMod().BackData(),
		SetEmoji:    p.PlayMod.getEmojiMod().GetEmojiSet(),
		Login:       int32(BaseMod.GetLoginTime()),
		PetName:     BaseMod.PetName,
		IdNum:       BaseMod.IdCardNum,
		AddCode:     BaseMod.AddCode,
	})
}

func (p *Player) ChargeBackData() {
	c := p.PlayMod.getChargeMod()
	LimitedEventMod := p.PlayMod.getLimitedTimeEventMod()
	SpecialShop := make(map[int32]*proto.ResSpecialShop)
	ChessShop := make(map[int32]*proto.ResChessShop)
	for k, v := range c.SpecialShop {
		SpecialShop[int32(k)] = &proto.ResSpecialShop{
			Grade: int32(v.Grade),
			Count: int32(v.Count),
		}
	}

	for k, v := range c.ChessShop {
		ChessShop[int32(k)] = &proto.ResChessShop{
			Diamond: int32(v.Diamond),
			Count:   int32(v.Count),
			ChessId: int32(v.Id),
		}
	}
	resWish := &proto.WishList{}
	if c.WishList != nil {
		resWish = &proto.WishList{
			Id:    int32(c.WishList.ItemId),
			Count: int32(c.WishList.Count),
			Uid:   c.WishList.SendList,
		}
	}
	WeeklyDiscount := make(map[int32]*proto.WeeklyDiscountInfo)
	WeeklyDiscountInfo := chargeCfg.GetWeeklyInfoAll()
	// 优惠日开启或者猫咪闪促开启
	if c.IsWeeklyDiscountDay() || LimitedEventMod.CheckExist(limitedTimeEvent.EVENT_TYPE_CAT_DAY_SALE) {
		for k, v := range WeeklyDiscountInfo {
			LimitNum := c.WeeklyDiscount[k]
			WeeklyDiscount[int32(k)] = &proto.WeeklyDiscountInfo{
				Discount: int32(v.Discount),
				Count:    int32(v.WeeklyLimit - LimitNum),
				Id:       int32(k),
			}
		}
	}
	CatDaySaleEndTime := LimitedEventMod.GetCatDaySaleEndTime()
	p.PushClientRes(&proto.ResCharge{
		Charge:            float32(c.Charge),
		Total:             int32(c.Total),
		First:             GoUtil.MapIntToSlice(c.EnergyShop),
		SpecialShop:       SpecialShop,
		FreeShop:          int32(c.FreeShop),
		ChessShop:         ChessShop,
		Gift:              GoUtil.MapIntToInt32(c.Gift),
		Ad:                c.Ad,
		SpecialCharge:     float32(c.SpecialCharge),
		SpecialChargeWeek: int32(GoUtil.FullWeeksSince(c.LastSpecialCharge)),
		TodayCharge:       float32(c.TodayCharge),
		MonthCharge:       float32(c.MonthCharge),
		Wish:              resWish,
		AdEndTime:         c.AdEndTime,
		WeeklyDiscount:    WeeklyDiscount,
		PetWorkRemainTime: c.PetWorkTime,
		WeeklyEndTime:     max(c.WeeklyEndTime, CatDaySaleEndTime),
	})
}

func (p *Player) BackChampship() {
	ChampshipMod := p.PlayMod.getChampshipMod()
	rank, preRank := p.GetChampshipRank()
	p.PushClientRes(ChampshipMod.BackData(rank, preRank))
}

// 获取冠军赛排名 redis缓存
func (p *Player) GetChampshipRank() (int, int) {
	preRank, rank, _, _ := GetRankCache(int(p.M_DwUin))
	return rank, preRank
}

// 返回好友信息
func (p *Player) FriendListBackData() {
	FriendMod := p.PlayMod.getFriendMod()
	friendList := FriendMod.GetFriendList()
	fl := make([]*proto.ResPlayerSimple, 0, len(friendList))
	for k, v := range friendList {
		if k == int(p.M_DwUin) {
			continue
		}
		ps := G_GameLogicPtr.GetResSimplePlayerByUid(k)
		if ps != nil {
			ps.AddTime = v.AddTime
			ps.Interact = FriendMod.GetInteractTime(k)
			fl = append(fl, ps)
		}
	}
	ReqFriendList := make([]int64, 0, len(FriendMod.SendApply))
	for k := range FriendMod.SendApply {
		ReqFriendList = append(ReqFriendList, int64(k))
	}
	p.PushClientRes(&proto.ResFriendList{
		FriendList:   fl,
		ReqApplyList: ReqFriendList,
		Npc:          GoUtil.IntToInt32(FriendMod.GetNpc()),
		Sponsor:      int32(FriendMod.GetSponsor()),
	})
}

func (p *Player) FriendApplyBackData() {
	FriendMod := p.PlayMod.getFriendMod()
	al := make([]*proto.ResFriendApplyInfo, 0, len(FriendMod.ApplyList))
	for k, v := range FriendMod.ApplyList {
		ps := G_GameLogicPtr.GetResSimplePlayerByUid(k)
		if ps != nil {
			al = append(al, &proto.ResFriendApplyInfo{
				Player: ps,
				Time:   int32(v),
			})
		}
	}
	p.PushClientRes(&proto.ResFriendApply{
		ApplyList: al,
	})
}

func (p *Player) FriendLogBackData() {
	FriendMod := p.PlayMod.getFriendMod()
	log := make([]*proto.ResFriendLog, 0, len(FriendMod.Log))
	for _, v := range FriendMod.Log {
		ps := G_GameLogicPtr.GetResSimplePlayerByUid(v.Uid)
		if ps == nil {
			ps = &proto.ResPlayerSimple{
				Uid: int64(v.Uid),
			}
		}
		log = append(log, &proto.ResFriendLog{
			Player: ps,
			Type:   int32(v.Type),
			Time:   int32(v.Time),
			Param:  v.Param,
			Id:     int32(v.Id),
			Upvote: v.Upvote,
		})
	}
	reply := make([]*proto.ResFriendReply, 0, len(FriendMod.ReplyList))
	for _, v := range FriendMod.ReplyList {
		ps := G_GameLogicPtr.GetResSimplePlayerByUid(v.Uid)
		if ps == nil {
			ps = &proto.ResPlayerSimple{
				Uid: int64(v.Uid),
			}
		}
		reply = append(reply, &proto.ResFriendReply{
			Player:  ps,
			Type:    int32(v.Type),
			Param:   v.Param,
			Id:      int32(v.Id),
			Status:  int32(v.Status),
			AddTime: v.AddTime,
			EndTime: v.EndTime,
		})
	}
	p.PushClientRes(&proto.ResFriendTimeLine{
		Log:   log,
		Reply: reply,
	})
}

func (p *Player) FriendCardBackData() {
	FriendMod := p.PlayMod.getFriendMod()
	msgList := make([]*proto.ResFriendCard, 0, len(FriendMod.Card))
	for _, v := range FriendMod.Card {
		msgList = append(msgList, GetCardInfoMsg(v))
	}
	p.PushClientRes(&proto.ResFriendCardMsg{
		MsgList: msgList,
	})
}
