package piggyBank

import (
	chargeCfg "server/conf/charge"
	"server/game/mod/item"
	GoUtil "server/game_util"
	"server/msg"
)

type PiggyBankMod struct {
	Type    int
	EndTime int64
	Diamond int
	List    []int
}

const (
	PIGGY_BANK_TYPE_AD     = 2
	PIGGY_BANK_TYPE_CHARGE = 1
)

func (p *PiggyBankMod) InitData() {
}

func (p *PiggyBankMod) AddPiggyBank(Type int) {
	p.List = append(p.List, Type)
}

func (p *PiggyBankMod) Fire(ChargeId int) []*item.Item {
	PiggyBankChargeId := chargeCfg.GetPiggyBankChargeId()
	if ChargeId != PiggyBankChargeId && ChargeId != 0 {
		return nil
	}
	reward := []*item.Item{item.NewItem(item.ITEM_DIAMOND_ID, p.Diamond)}
	p.Diamond = 0
	p.EndTime = 0
	p.TimeOut()
	return reward
}

func (p *PiggyBankMod) TimeOut() int64 {
	Now := GoUtil.Now()
	Remain := p.EndTime - Now
	if Remain > 0 {
		return Remain
	}
	if len(p.List) == 0 {
		return 0
	}
	var Type int
	Type, p.List = GoUtil.PopSlice(p.List)
	p.Type = Type
	Duration := int64(chargeCfg.GetPiggyDuration())
	p.EndTime = Now + Duration
	return Duration
}

func (p *PiggyBankMod) Trigger() {
	Now := GoUtil.Now()
	if p.EndTime < Now {
		return
	}
	minDia, maxDia := chargeCfg.GetPiggyRandDia()
	p.Diamond += GoUtil.RandNum(minDia, maxDia)
	p.Diamond = min(p.Diamond, chargeCfg.GetPiggyMaxDia())
}

func (p *PiggyBankMod) BackData() *msg.ResPiggyBank {
	return &msg.ResPiggyBank{
		Type:    int32(p.Type),
		EndTime: int32(p.EndTime),
		Diamond: int32(p.Diamond),
		Count:   int32(len(p.List)),
	}
}
