package player

import (
	mhayaTime "github.com/mhaya/extend/time"
	"github.com/mhaya/game/game_cluster/internal/code"
	"github.com/mhaya/game/game_cluster/internal/data"
	"github.com/mhaya/game/game_cluster/internal/mdb/models"
	"github.com/mhaya/game/game_cluster/internal/param"
)

func (p *actorPlayer) signIn() (*param.ChangeData, int32) {
	if !p.isOnline {
		return nil, code.PlayerNotLogin
	}

	// todo 测试代码

	resp, reCode := p.SignInOp()
	p.dirty = true

	return resp, reCode

	//// 正式代码
	//if !mhayaTime.CreateFromTimestamp(p.Player.SinIn.SignDailyTime).IsToday() {
	//	resp := p.Player.SignIn()
	//	p.dirty = true
	//
	//	p.itemStat(resp.AddItem)
	//
	//	return resp, code.OK
	//} else {
	//	return nil, code.PlayerSignInError
	//}
}

func (p *actorPlayer) signInList() (*models.SignIn, int32) {
	if !p.isOnline {
		return nil, code.PlayerNotLogin
	}
	if mhayaTime.CreateFromTimestamp(p.Player.SinIn.SignDailyTime).IsToday() {
		p.Player.SinIn.IsSignCur = 1
	} else {
		p.Player.SinIn.IsSignCur = 0
	}
	return &p.Player.SinIn, code.OK
}

func (p *actorPlayer) SignInOp() (*param.ChangeData, int32) {
	si := p.Player.SinIn.Sign
	if p.Player.SinIn.Num > len(si)-1 {
		//return nil, code.PlayerSignInError
	}
	var sign param.ChangeData
	p.Player.SinIn.SignDailyTime = mhayaTime.Now().Unix()
	p.Player.SinIn.Sign[p.Player.SinIn.Num].IsSign = 1
	p.Player.SinIn.Sign[p.Player.SinIn.Num].SignTime = mhayaTime.Now().Unix()
	var addItem models.AddItem
	at := addItem.Add(p.Player.SinIn.Sign[p.Player.SinIn.Num].Reward)
	sign.AddItem = at
	p.Player.Item.AddItem(p.Player.SinIn.Sign[p.Player.SinIn.Num].Reward)
	p.Player.SinIn.Num += 1
	p.itemStat(at)

	// 节点奖励进度
	p.Player.SinIn.NodeProgress.Curr += 1

	// 领取签到节点奖励
	for _, nodeReward := range p.Player.SinIn.NodeReward {
		if p.Player.SinIn.NodeProgress.Curr >= nodeReward.NodePos && nodeReward.IsClaim == 0 {
			addItem := models.NewAddItem().Add(nodeReward.Reward)
			// 领取
			p.Player.Item.AddItem(nodeReward.Reward)
			p.Player.SinIn.NodeReward[nodeReward.NodeId].IsClaim = 1
			p.Player.SetPlayerRewardLog(models.SourceSigInNode, nodeReward.NodeId, nodeReward.Reward, []data.ItemReward{}, nil)
			// 统计收益
			p.itemStat(addItem)

			sign.NodeAddItem = addItem
			break
		}
	}
	sign.CurGold = p.Player.Item[models.ItemGold].Num
	return &sign, code.OK
}

func (p *actorPlayer) SetPlayerRewardLog(tp, tid int, addReward, subReward []data.ItemReward, desc interface{}) {
	p.Player.Index += 1
	if len(p.Player.PlayerReward) == 0 {
		p.Player.PlayerReward = make(map[int]*models.PlayerRewardBase)
	}
	p.Player.PlayerReward[p.Player.Index] = &models.PlayerRewardBase{
		UserName:   p.Player.UserName,
		AddReward:  addReward,
		SubReward:  subReward,
		Desc:       desc,
		Source:     tp,
		SourceId:   tid,
		CreateTime: mhayaTime.Now().Unix(),
	}

}

// 领取签到节点奖励
func (p *actorPlayer) ClaimSignNodeReward() {
	for _, nodeReward := range p.Player.SinIn.NodeReward {
		if p.Player.SinIn.NodeProgress.Curr >= nodeReward.NodePos && nodeReward.IsClaim == 0 {
			addItem := models.NewAddItem().Add(nodeReward.Reward)
			// 领取
			p.Player.Item.AddItem(nodeReward.Reward)
			p.Player.SinIn.NodeReward[nodeReward.NodeId].IsClaim = 1
			p.Player.SetPlayerRewardLog(models.SourceSigInNode, nodeReward.NodeId, nodeReward.Reward, []data.ItemReward{}, nil)
			// 统计收益
			p.itemStat(addItem)
			break
		}
	}
}
