package base

import (
	"fmt"
	"server/conf"
	baseCfg "server/conf/base"
	"server/game/mod/item"
	GoUtil "server/game_util"
	"server/msg"
)

const (
	ModuleName = "base"
)

type Base struct {
	Account         string
	Uid             int64
	Level           int
	Exp             int
	PExp            int
	Energy          int
	Star            int
	Diamond         int
	LoginTime       int64
	LoginDay        int // 登录天数
	LogoutTime      int64
	RecoverTime     int64
	FackBookId      string
	EnergyMul       int
	IsFirstBuy      bool
	EnergyBuy       int
	NickName        string
	PetName         string
	FacebookUrl     string
	Cumulative      int  // 累计在线时间
	TodayCumulative int  // 今日累计在线时间
	LoginBack       bool // 回归补偿
	EnergyAD        int  // 每日看广告获得的能量次数
	Seed            bool
	Source          int           // 体力资源
	Lang            msg.LANG_TYPE // 语言
	IdCardName      string
	IdCardNum       string
	AddCode         string // 用于添加好友的code
	DiviceId        string // 设备id
}

func (b *Base) InitData(Uid int, Ip string) {
	if b.AddCode == "" && Ip != "" {
		Code, _ := GoUtil.GetCountryByIP(Ip)
		CountryCode := conf.Server.CountryCode
		if CountryCode == "" {
			CountryCode = "000"
		}
		if Code != "" && len(Code) == 3 {
			CountryCode = Code
		}
		b.AddCode = fmt.Sprintf("MMM-%s-%s", CountryCode, GoUtil.UniqueStringFromInt(Uid))
	}
	if b.NickName == "" {
		b.NickName = fmt.Sprintf("Player_%d", Uid)
	}
}

func (b *Base) Login() int64 {
	Now := GoUtil.Now()
	if !GoUtil.IsSameDay(b.LoginTime, Now) {
		b.LoginDay += 1
	}
	logoutDuration := int64(0)
	if b.LogoutTime != 0 {
		logoutDuration = Now - b.LogoutTime
	}
	b.LoginTime = Now
	b.LogoutTime = 0
	return logoutDuration
}

func (b *Base) GetSeed() bool {
	return b.Seed
}

func (b *Base) SetSeed(Seed bool) {
	b.Seed = Seed
}

func (b *Base) GetSource() int {
	return b.Source
}

func (b *Base) SetSource(Source int) {
	b.Source = Source
}

func (b *Base) GetNickName() string {
	return b.NickName
}

func (b *Base) GetFacebookUrl() string {
	return b.FacebookUrl
}

func (b *Base) GetEnergyMul() int {
	return b.EnergyMul
}

func (b *Base) SetEnergyMul(mul int) {
	b.EnergyMul = mul
}

func (b *Base) SetNickName(Name string) {
	b.NickName = Name
}

func (b *Base) SetPetName(Name string) {
	b.PetName = Name
}

func (b *Base) GetPetName() string {
	return b.PetName
}

func (b *Base) GetIsFirstBuy() bool {
	return b.IsFirstBuy
}

func (b *Base) SetFacebookUrl(Url string) {
	b.FacebookUrl = Url
}

func (b *Base) GetEnergyBuy() int {
	return b.EnergyBuy
}

func (b *Base) GetEnergy() int {
	return b.Energy
}

func (b *Base) GetLevel() int {
	return b.Level
}

func (b *Base) GetExp() int {
	return b.Exp
}
func (b *Base) GetPExp() int {
	return b.PExp
}

func (b *Base) GetStar() int {
	return b.Star
}

func (b *Base) GetDiamond() int {
	return b.Diamond
}

func (b *Base) GetCumulative() int {
	return b.Cumulative
}

func (b *Base) GetLoginTime() int64 {
	return b.LoginTime
}

func (b *Base) GetLogoutTime() int64 {
	return b.LogoutTime
}

func (b *Base) GetLoginDay() int {
	return b.LoginDay
}

func (b *Base) GetRecoverTime() int64 {
	return b.RecoverTime
}

func (b *Base) SetAccount(Account string) {
	b.Account = Account
}

func (b *Base) SetUid(Uid int64) {
	b.Uid = Uid
}

func (b *Base) SetLevel(Level int) {
	b.Level = Level
}

func (b *Base) SetExp(Exp int) {
	b.Exp = Exp
}

func (b *Base) SetEnergy(Energy int) {
	b.Energy = Energy
}

func (b *Base) SetStar(Star int) {
	b.Star = Star
}

func (b *Base) SetDiamond(Diamond int) {
	b.Diamond = Diamond
}

func (b *Base) SetRecoverTime(RecoverTime int64) {
	b.RecoverTime = RecoverTime
}

func (b *Base) SetLoginTime(LoginTime int64) {
	b.LoginTime = LoginTime
}

func (b *Base) SetLogoutTime(LogoutTime int64) {
	b.LogoutTime = LogoutTime
}

func (b *Base) GetLang() msg.LANG_TYPE {
	return b.Lang
}

func (b *Base) SetLang(lang msg.LANG_TYPE) {
	b.Lang = lang
}

func (b *Base) BuyEnergy(Energy int) ([]*item.Item, int, int) {
	b.EnergyBuy += 1
	Mul := baseCfg.GetEnergyByMul(b.EnergyBuy)
	Diamond := int(float64(Energy) / Mul)
	return []*item.Item{{Id: item.ITEM_DIAMOND_ID, Num: -Diamond}, {Id: item.ITEM_ENERGY_ID, Num: Energy}}, Energy, Diamond
}

// HighRoller 结束时重置能量倍数
func (b *Base) ResetEnergyMul(Mul int) {
	b.EnergyMul = Mul
}

func (b *Base) BackData() *msg.BaseInfo {
	return &msg.BaseInfo{
		EnergyMul:  int32(b.EnergyMul),
		IsFirstBuy: b.IsFirstBuy,
		EnergyBuy:  int32(b.EnergyBuy),
		EnergyAD:   int32(b.EnergyAD),
		Lang:       b.Lang,
	}
}

func (b *Base) ZeroUpdate() {
	b.EnergyBuy = 0
	b.EnergyAD = 0
	b.TodayCumulative = 0
}

func (b *Base) Outline(Time int) {
	b.LogoutTime = GoUtil.Now()
	b.TodayCumulative += Time
	b.Cumulative += Time
}

func (b *Base) GetEnergyByAD() ([]*item.Item, error) {
	if b.EnergyAD >= 5 {
		return nil, fmt.Errorf("energy ad is max")
	}
	b.EnergyAD += 1
	return baseCfg.GetEnergyByADNum(), nil
}

func (b *Base) FormatEnergyMul(Energy int) {
	for {
		if b.EnergyMul <= 0 {
			b.EnergyMul = 0
			return
		}
		EnergyLimit := baseCfg.GetLimitEnergyMul(b.Level)
		if EnergyLimit == 0 {
			return
		}
		if Energy > EnergyLimit {
			return
		}
		b.EnergyMul--
	}
}

func (b *Base) SetId2Verify(UserName, Num string) {
	b.IdCardName = UserName
	b.IdCardNum = Num
}
