package player

import (
	"context"
	"fmt"
	jsoniter "github.com/json-iterator/go"
	"github.com/mhaya/game/game_cluster/internal/code"
	"github.com/mhaya/game/game_cluster/internal/constant"
	"github.com/mhaya/game/game_cluster/internal/data"
	"github.com/mhaya/game/game_cluster/internal/mdb"
	"github.com/mhaya/game/game_cluster/internal/mdb/models"
	"github.com/mhaya/game/game_cluster/internal/param"
	"github.com/mhaya/game/game_cluster/internal/third"
)

func (p *actorPlayer) rank(req *param.GetRankReq) (*param.GetRankResp, int32) {
	if !p.isOnline {
		return nil, code.LoginError
	}
	var resp param.GetRankResp
	rankList := models.GetRankList(req.Id, req.Tp)
	resp.Ranks = rankList
	var isClaim int
	for _, rank := range rankList {
		if req.Tp == 1 {
			if p.Player.RankReward[req.Id].Status == 2 {
				isClaim = 1
			} else {
				isClaim = 0
			}
		}
		//if p.Player.UserName == rank.UserName {
		//	resp.Self = rank
		//}
		rank.IsClaim = isClaim
	}
	if resp.Self == nil {
		ctx := context.Background()
		info := mdb.RDB.HGet(ctx, constant.PlayerBaseKey, p.Player.UserName).Val()
		var playerBase param.PlayerBase
		jsoniter.Unmarshal([]byte(info), &playerBase)
		yesterdayKey := models.GetRankKey(req.Id, req.Tp, true)
		todayKey := models.GetRankKey(req.Id, req.Tp, false)
		yesterdayRank := models.GetPlayerRank(yesterdayKey, p.Player.UserName)
		todayRank := models.GetPlayerRank(todayKey, p.Player.UserName)

		resp.Self = &param.PlayerBase{
			UserName:      playerBase.UserName,
			UserNameMaybe: playerBase.UserNameMaybe,
			Avatar:        playerBase.Avatar,
			OpenId:        playerBase.OpenId,
			NickName:      playerBase.NickName,
			Rank:          todayRank,
			Score:         models.GetPlayerRankScore(todayKey, p.Player.UserName),
			Reward:        data.RankRewardConfig.GetRewardByRank(req.Id, yesterdayRank),
			IsClaim:       isClaim,
		}

		resp.Reward = data.RankRewardConfig.GetRewardByRank(req.Id, todayRank)
	}
	return &resp, code.OK
}

func (p *actorPlayer) countryRank() (*param.GetCountryRankResp, int32) {
	if !p.isOnline {
		return nil, code.LoginError
	}

	var resp param.GetCountryRankResp
	countryMap := make(map[string]*data.ContryConfigRow, 0)
	countryConfig := data.ContryConfig.GetAll()
	all := data.RankConfig.GetAll()
	fmt.Println(all)
	for _, row := range countryConfig {
		countryMap[row.NameI] = row
	}

	rankList := models.GetCountryRankList(countryMap)
	resp.Ranks = rankList

	country, _ := third.GetCountryByIP(p.Player.LoginIP)
	for _, rank := range rankList {
		if country == rank.Country {
			resp.Self = rank
		}
	}
	if resp.Self == nil {
		ctx := context.Background()
		info := mdb.RDB.HGet(ctx, constant.PlayerBaseKey, p.Player.UserName).Val()
		var playerBase param.PlayerBase
		jsoniter.Unmarshal([]byte(info), &playerBase)
		key := fmt.Sprintf("%s", constant.CountryRankKey)
		if value, ok := countryMap[country]; ok {
			resp.Self = &param.CountryBase{
				Country: value.NameII,
				Rank:    models.GetCountryRank(key, country),
				Score:   models.GetCountryRankScore(key, country),
				Icon:    value.Icon_flag,
			}
		}

	}
	return &resp, code.OK
}
