package models

import (
	cutils "github.com/mhaya/extend/utils"
	"github.com/mhaya/game/game_cluster/internal/constant"
	"github.com/mhaya/game/game_cluster/internal/data"
	"go.mongodb.org/mongo-driver/bson"
)

const (
	SourceGuide       = 1
	SourceRoll        = 2
	SourceSignIn      = 3
	SourceDailyTask   = 4
	SourceAchieveTask = 5
	SourceRank        = 6
	SourceInvite      = 7
	SourceRefreshRoll = 8
	SourceShare       = 9
	SourceLevelUp     = 10
	SourceDraw        = 11
	SourceCashOut     = 12
)

type PlayerRewardBase struct {
	UserName   string            `json:"userName" bson:"userName"`
	Source     int               `json:"source" json:"source"`
	SourceId   int               `json:"sourceId" json:"sourceId"`
	AddReward  []data.ItemReward `json:"AddReward" bson:"addReward"`
	SubReward  []data.ItemReward `json:"SubReward" bson:"subReward"`
	Desc       interface{}       `json:"desc" bson:"desc"`
	CreateTime int64             `json:"createTime" bson:"createTime"`
}

type PlayerReward map[int]*PlayerRewardBase

func NewPlayerReward() map[int]*PlayerRewardBase {
	return make(map[int]*PlayerRewardBase)
}

type ToPlayerRewardBase struct {
	UserName   string          `json:"userName" bson:"userName"`
	AddReward  []*ToItemReward `json:"AddReward" bson:"addReward"`
	Desc       interface{}     `json:"desc" bson:"desc"`
	CreateTime int64           `json:"createTime" bson:"createTime"`
}

type ToItemReward struct {
	ItemID   int     // itemID:道具ID
	Amount   float64 // amount:数量
	ItemName string  // itemName:道具名称
}

type ToDesc struct {
	ID    int `json:"id" bson:"id"`
	CurID int `json:"cur_id" bson:"curid"`
}

func (uw *PlayerRewardBase) To() *ToPlayerRewardBase {
	return &ToPlayerRewardBase{
		UserName: uw.UserName,
		AddReward: func() []*ToItemReward {
			ret := make([]*ToItemReward, 0, 8)
			for _, v := range uw.AddReward {
				itemName := ""
				cfg, exist := data.ItemConfig.Get(v.ItemID)
				if exist {
					itemName = cfg.ItemKey
				}

				ret = append(ret, &ToItemReward{
					ItemID: v.ItemID,
					Amount: func() float64 {
						if itemName == "u" || itemName == "ton" {
							return cutils.QuoInt64ByRatioToFloat64(int64(v.Amount), constant.MoneyRatio)
						}

						return float64(v.Amount)
					}(),
					ItemName: itemName,
				})
			}

			return ret
		}(),
		Desc: func() *ToDesc {
			if uw.Desc == nil {
				return nil
			}

			drawMap := uw.Desc.(bson.D).Map()
			return &ToDesc{
				ID:    int(drawMap["id"].(int32)),
				CurID: int(drawMap["curid"].(int32)),
			}
		}(),
		// Desc:       uw.Desc,
		CreateTime: uw.CreateTime,
	}
}
