package activityCfg

import (
	"server/GoUtil"
	"server/game/mod/item"
	"server/gamedata"
	"server/pkg/github.com/name5566/leaf/log"
	"strconv"
)

const (
	CFG_ACTIVITY      = "Activity"
	CFG_ACTIVITY_GIFT = "ActivityGift"
)

func init() {
	gamedata.InitCfg(CFG_ACTIVITY)
	gamedata.InitCfg(CFG_ACTIVITY_GIFT)
}

func GetActivityGiftId(chargeId int) int {
	data, err := gamedata.GetData(CFG_ACTIVITY_GIFT)
	if err != nil {
		log.Debug("GetActivityGift err:%v", err)
	}
	for _, v := range data {
		ChargeId := gamedata.GetIntValue(v, "ChargeId")
		if ChargeId == chargeId {
			return gamedata.GetIntValue(v, "AId")
		}
	}
	return 0
}

func GetAcitivityGiftItems(chargeId int) []*item.Item {
	data, err := gamedata.GetData(CFG_ACTIVITY_GIFT)
	if err != nil {
		log.Debug("GetAcitivityGiftItems err:%v", err)
	}
	for _, v := range data {
		ChargeId := gamedata.GetIntValue(v, "ChargeId")
		if ChargeId == chargeId {
			Items := gamedata.GetItemList(v, "Items")
			return Items
		}
	}
	return nil
}

func GetActivityList() []*gamedata.ActivityData {
	data, err := gamedata.GetData(CFG_ACTIVITY)
	if err != nil {
		log.Debug("GetActivityList err:%v", err)
	}
	Now := GoUtil.Now()
	var List []*gamedata.ActivityData

	for k, v := range data {
		StartTimeStr := gamedata.GetStringValue(v, "StartTime")
		EndTimeStr := gamedata.GetStringValue(v, "EndTime")
		StartTime := GoUtil.ParseTime(StartTimeStr)
		EndTime := GoUtil.ParseTime(EndTimeStr)
		if Now >= StartTime && Now <= EndTime {
			Id, _ := strconv.Atoi(k)
			Type := gamedata.GetIntValue(v, "Type")
			Name := gamedata.GetStringValue(v, "Name")
			Title := gamedata.GetStringValue(v, "Title")
			Level := gamedata.GetIntValue(v, "Level")
			ActivityData := &gamedata.ActivityData{
				Id:        Id,
				Type:      Type,
				AType:     gamedata.GetIntValue(v, "AType"),
				Name:      Name,
				Title:     Title,
				Level:     Level,
				StartTime: StartTime,
				EndTime:   EndTime,
			}
			List = append(List, ActivityData)
		}
	}
	return List
}
