package game

import (
	activityCfg "server/conf/activity"
	"server/game/mod/piggyBank"
	"server/msg"
	"server/pkg/github.com/name5566/leaf/log"
)

func Charge(p *Player, ChargeId int) {
	ChargeFire(p, ChargeId)    // 充值
	EndlessFire(p, ChargeId)   // 无尽礼包
	PiggyBankFire(p, ChargeId) // 猪猪银行
	PlayroomFire(p, ChargeId)  // 游乐场
	ActivityFire(p, ChargeId)  // 活动礼包
}

func ActivityFire(p *Player, ChargeId int) {
	ActivityMod := p.PlayMod.getActivityMod()
	ActivityId := activityCfg.GetActivityGiftId(ChargeId)
	ActivityInfo := GetActivityInfoById(p, ActivityId)
	if ActivityInfo == nil {
		log.Debug("ActivityFire ActivityInfo nil : player id :%d, charge id:%d", p.M_DwUin, ChargeId)
		return
	}
	Item, err := ActivityMod.Fire(ChargeId)
	if err != nil {
		log.Debug("ActivityFire err : %s", err)
		return
	}
	err = p.HandleItem(Item, msg.ITEM_POP_LABEL_ActivityGift.String())
	if err != nil {
		log.Debug("ChargeFire err : %s", err)
		return
	}
	p.PlayMod.save()
	p.initAcitivity()
	p.BackDataActivity()
}

func PlayroomFire(p *Player, ChargeId int) {
	PlayroomMod := p.PlayMod.getPlayroomMod()
	Item := PlayroomMod.Fire(ChargeId)
	err := p.HandleItem(Item, msg.ITEM_POP_LABEL_Playroom.String())
	if err != nil {
		log.Debug("ChargeFire err : %s", err)
	}
	p.PlayMod.save()
}

func PiggyBankFire(p *Player, ChargeId int) {
	PiggyBankMod := p.PlayMod.getPiggyBankMod()
	Item := PiggyBankMod.Fire(ChargeId)
	err := p.HandleItem(Item, msg.ITEM_POP_LABEL_PiggyBank.String())
	if err != nil {
		log.Debug("ChargeFire err : %s", err)
	}
	LimitedTimePiggyBankTrigger(p)
	p.PlayMod.save()
	p.PushClientRes(PiggyBankMod.BackData())
	p.TeLog("piggy_bank_open", map[string]interface{}{
		"piggy_bank_type": piggyBank.PIGGY_BANK_TYPE_CHARGE,
		"item_list":       Item,
		"open_cost":       ChargeId,
	})
}

// 处理玩家充值
func ChargeFire(p *Player, ChargeId int) {
	ChargeMod := p.PlayMod.getChargeMod()
	Item := ChargeMod.Fire(ChargeId)
	err := p.HandleItem(Item, msg.ITEM_POP_LABEL_Charge.String())
	if err != nil {
		log.Debug("ChargeFire err : %s", err)
	}
	p.PlayMod.save()
	p.PushClientRes(ChargeMod.BackData())
}

// 处理玩家充值
func EndlessFire(p *Player, ChargeId int) {
	EndlessMod := p.PlayMod.getEndlessMod()
	Item := EndlessMod.Fire(ChargeId)
	if Item == nil {
		return
	}
	err := p.HandleItem(Item, msg.ITEM_POP_LABEL_Endless.String())
	if err != nil {
		log.Debug("ChargeFire err : %s", err)
	}
	p.PlayMod.save()
	p.PushClientRes(EndlessMod.BackData())
}
