package activityCfg

import (
	languageCfg "server/conf/language"
	"server/game/mod/item"
	GoUtil "server/game_util"
	"server/gamedata"
	"server/msg"
	"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("GetActivityGiftId err:%v", err)
		return 0
	}
	for _, v := range data {
		ChargeId := gamedata.GetIntValue(v, "ChargeId")
		if ChargeId == chargeId {
			return gamedata.GetIntValue(v, "AId")
		}
	}
	return 0
}

func GetActivityGiftItems(chargeId int) []*item.Item {
	data, err := gamedata.GetData(CFG_ACTIVITY_GIFT)
	if err != nil {
		log.Debug("GetActivityGiftItems err:%v", err)
		return nil
	}
	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)
		return nil
	}
	Now := GoUtil.Now()
	List := make([]*gamedata.ActivityData, 0, len(data))

	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, err := strconv.Atoi(k)
			if err != nil {
				log.Debug("GetActivityList parse id err:%v, key:%s", err, k)
				continue
			}
			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
}

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

func GetActivityTitle(ActId int) (string, string) {
	data, err := gamedata.GetDataByIntKey(CFG_ACTIVITY, ActId)
	if err != nil {
		log.Debug("GetActivityTitle err:%v", err)
		return "", ""
	}
	title := gamedata.GetStringValue(data, "Title")
	return languageCfg.GetLanguage(msg.LANG_TYPE_LANG_CN, title), languageCfg.GetLanguage(msg.LANG_TYPE_LANG_EN, title)
}

func GetActivityRecycleMail(ActId int) (string, string, string) {
	data, err := gamedata.GetDataByIntKey(CFG_ACTIVITY, ActId)
	if err != nil {
		log.Debug("GetActivityRecycleMail err:%v", err)
		return "", "", ""
	}
	return gamedata.GetStringValue(data, "Title"), gamedata.GetStringValue(data, "Mail_Title"), gamedata.GetStringValue(data, "Mail_Content")
}
