package models

import (
	"github.com/mhaya/game/game_cluster/internal/data"
	"strconv"
)

type SignInBase struct {
	ID       int               `json:"id"  bson:"id""`       // #周期签到
	Reward   []data.ItemReward `json:"reward" bson:"reward"` // 奖励
	IsSign   int               `json:"isSign"  bson:"isSign"`
	SignTime int64             `json:"signTime"  bson:"signTime"`
}

// 签到节点进度
type NodeProgress struct {
	Total int `json:"total"  bson:"total"`     // 总进度
	Curr  int `json:"current"  bson:"current"` // 当前进度
}

// 节点奖励
type NodeReward struct {
	NodeId  int               `json:"nodeId" bson:"nodeId"`    // 节点id
	NodePos int               `json:"nodePos" bson:"nodePos"`  // 节点位置
	IsClaim int               `json:"isClaim"  bson:"isClaim"` // 是否领取 1领取0未领取
	Reward  []data.ItemReward `json:"reward" bson:"reward"`
}

type SignIn struct {
	IsSignCur     int                 `json:"isSignCur"  bson:"isSignCur"`
	SignDailyTime int64               `json:"signDailyTime"  bson:"signDailyTime"`
	Num           int                 `json:"num"  bson:"num"`
	Sign          map[int]*SignInBase `json:"sign" bson:"sign"`
	NodeProgress  NodeProgress        `json:"progress" bson:"progress"`
	NodeReward    map[int]*NodeReward `json:"reward" bson:"reward"`
}

func NewSignIn() SignIn {
	return SignIn{
		SignDailyTime: 0,
		Num:           0,
		Sign:          make(map[int]*SignInBase),
		NodeProgress:  NodeProgress{},
		NodeReward:    make(map[int]*NodeReward),
	}
}

func (si *SignIn) RefreshSignIn() {
	ret := data.WeeklySignInConfig.GetAll()
	si.Sign = make(map[int]*SignInBase)
	for _, d := range ret {
		si.Sign[d.ID] = &SignInBase{
			ID:       d.ID,
			Reward:   d.Reward,
			IsSign:   0,
			SignTime: 0,
		}
	}

	si.SignDailyTime = 0
	si.Num = 0
}

// 刷新签到节点奖励
func (si *SignIn) RefreshSignNode(signInNode []int) {
	var maxProgress int
	si.NodeReward = make(map[int]*NodeReward)
	for i, id := range signInNode {
		ret, _ := data.DiscreteRuleConfig.Get(id)

		node, _ := data.DiscreteRuleConfig.Get(signInNode[i])
		pos, _ := strconv.Atoi(node.Condition)
		si.NodeReward[id] = &NodeReward{
			NodeId:  id,
			IsClaim: 0,
			Reward:  ret.Reward,
			NodePos: pos,
		}

		if pos > maxProgress {
			maxProgress = pos
		}
	}

	si.NodeProgress = NodeProgress{
		Total: maxProgress,
		Curr:  0,
	}
	si.Num = 0
}
