package player

import (
	"context"
	"fmt"
	mhayaTime "github.com/mhaya/extend/time"
	"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"
	clog "github.com/mhaya/logger"
)

func (p *actorPlayer) start() (*models.HomeData, int32) {
	if !p.isOnline {
		clog.Infof("[start] PlayerNotLogin err node=%v", p.App().NodeId())
		return nil, code.PlayerNotLogin
	}
	p.dirty = true

	clog.Debugf("start() nickName:%s openId:%s ip:%s", p.Player.NickName, p.Player.OpenId, p.Player.LoginIP)
	if !mhayaTime.CreateFromTimestamp(p.Player.LoginTime).IsToday() {
		p.Player.Successions += 1
		if mhayaTime.CreateFromTimestamp(p.Player.LoginTime).DiffInDays(mhayaTime.Now()) > 1 {
			p.Player.MaxSuccessions = 1
		} else {
			p.Player.MaxSuccessions += 1
		}
	}

	// 币安等级
	curLv, _ := mdb.RDB.Get(context.Background(), constant.RocketLvKey).Int()

	rocketLv := models.RocketLvProgress{
		Max: 1000,
		Cur: curLv,
	}

	totalIcorme, _ := mdb.RDB.Get(context.Background(), constant.TotalIncomeKey).Int64()

	botConfig := data.SdkConfig.Get(2)

	var inviteStatus int
	exists := mdb.RDB.Exists(context.Background(), fmt.Sprintf("%v:1:%v", constant.InviteKey, p.Player.UserName)).Val()
	if exists == 0 {
		kol, _ := data.KolConfig.Get(p.Player.UserName)
		if kol != nil && kol.PptBoolean == 1 {
			inviteStatus = 1
		}
	}

	homeData := &models.HomeData{
		Item:         p.Player.Item,
		FirstItem:    p.Player.FirstClaimReward,
		RocketLv:     rocketLv,
		Dirty:        p.TonWallFlag,
		TotalIcorme:  totalIcorme,
		ChatIDName:   botConfig.Params.ChatIDName,
		BtUserName:   botConfig.Params.BtUserName,
		Avatar:       botConfig.Params.Avatar,
		QuickLink:    botConfig.Params.QuickLink,
		InviteStatus: inviteStatus,
	}
	return homeData, code.OK
}
