package player

import (
	"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"
	clog "github.com/mhaya/logger"
)

func (p *actorPlayer) jump(req *param.JumpReq) (*param.JumpResp, int32) {
	clog.Debugf("jump tp = %v", req.Tp)
	if !p.isOnline {
		clog.Infof("[jump] PlayerNotLogin err node=%v", p.App().NodeId())
		return nil, code.PlayerNotLogin
	}
	ret := p.Player.FinishAchieveTaskByType1(req.Tp)
	if ret {
		p.dirty = true
		p.TonWallFlag = true
		clog.Debugf("jump return true openid = %s", p.Player.OpenId)
		return &param.JumpResp{Dirty: true}, code.OK
	}
	clog.Debugf("jump return flase openid = %s", p.Player.OpenId)
	p.TonWallFlag = false
	return &param.JumpResp{Dirty: false}, code.OK
}

func (p *actorPlayer) savex(req *param.SaveXReq) (*param.ChangeData, int32) {
	if !p.isOnline {
		clog.Infof("[savex] PlayerNotLogin err node=%v", p.App().NodeId())
		return nil, code.PlayerNotLogin
	}

	if p.Player.XID == req.Twitter {
		return nil, code.XAccountAlreadyExistsError
	}
	var resp param.ChangeData
	if p.Player.IsFirstBindingXID == 0 {
		ret, ok := data.DiscreteRuleConfig.Get(models.FirstBindingTwitter)
		if !ok {
			return nil, code.Error
		}
		p.Player.IsFirstBindingXID = 1
		addItem := models.NewAddItem()
		p.Player.Item.AddItem(ret.Reward)
		at := addItem.Add(ret.Reward)
		resp.AddItem = at
		resp.IsLevelUp = p.Player.LevelUp()
	}
	p.Player.XID = req.Twitter

	p.itemStat(resp.AddItem)

	return &resp, code.OK
}

func (p *actorPlayer) savetonwall(req *param.SaveTonWallReq) int32 {
	if !p.isOnline {
		clog.Infof("[savetonwall] PlayerNotLogin err node=%v", p.App().NodeId())
		return code.PlayerNotLogin
	}

	/*	errCode := p.VerifyPwd(req.PassWord)
		if errCode > 0 {
			return errCode
		}*/

	if p.Player.TonWall == req.TonWall {
		return code.TonWallAlreadyExistsError
	}

	p.Player.TonWall = req.TonWall

	return code.OK
}
