package activity

import (
	"fmt"
	activityCfg "server/conf/activity"
	"server/game/mod/item"
	GoUtil "server/game_util"
	"server/gamedata"
)

const (
	ACT_STATUS_NOT_START = 0
	ACT_STATUS_START     = 1
	ACT_STATUS_END       = 2
)

const (
	ACT_TYPE_MINING        = 1 // 挖矿
	ACT_TYPE_GUESS_COLOR   = 2 // 猜颜色
	ACT_TYPE_RACE          = 3 // 赛跑
	ACT_TYPE_DISCOUNT_GIFT = 4 // 折扣礼包
	ACT_TYPE_ADD_GIFT      = 5 // 加送礼包
	ACT_TYPE_SUPER_GIFT    = 6 // 超值加购礼包
	ACT_TYPE_CATNIP        = 7 // 猫草大作战
	ACT_TYPE_PASS          = 8 // 通行证
)

const (
	ACT_ATYPE_NORMAL     = 1 // 普通活动
	ACT_ATYPE_LIMIT_GIFT = 2 // 限时礼包活动
)

type Activity struct {
	Var map[int]interface{}
	// 活动数据
}

type Gift struct {
	Buy        bool
	Reward     bool
	Time       int64
	RewardTime int64
}

func (a *Activity) InitData() {
	if a.Var == nil {
		a.Var = make(map[int]interface{}, 8)
	}
}

// 获取活动数据
func (a *Activity) GetVar(key int) interface{} {
	return a.Var[key]
}

// 设置活动数据
func (a *Activity) SetVar(key int, value interface{}) {
	a.Var[key] = value
}

// 获取活动礼包数据
func (a *Activity) getGiftVar(key int) *Gift {
	if v, ok := a.Var[key].(*Gift); ok {
		return v
	}
	// nil 或类型不匹配，重置为新的 Gift 对象
	gift := &Gift{}
	a.Var[key] = gift
	return gift
}

// 获取活动状态
func (a *Activity) GetActivityStatus(data *gamedata.ActivityData) int {
	switch data.AType {
	case ACT_ATYPE_NORMAL:
		return ACT_STATUS_START
	case ACT_ATYPE_LIMIT_GIFT:
		v := a.getGiftVar(data.Id)
		switch data.Type {
		case ACT_TYPE_ADD_GIFT, ACT_TYPE_SUPER_GIFT:
			if v.Reward {
				return ACT_STATUS_NOT_START
			}
			if v.Buy {
				return ACT_STATUS_END
			}
		default:
			if v.Buy {
				return ACT_STATUS_NOT_START
			}
		}
	}
	return ACT_STATUS_START
}

// 活动礼包-获取奖励
func (a *Activity) GetReward(ActId int) ([]*item.Item, error) {
	Var := a.getGiftVar(ActId)
	if !Var.Buy {
		return nil, fmt.Errorf("ActId:%d not buy", ActId)
	}
	if Var.Reward {
		return nil, fmt.Errorf("ActId:%d reward already get", ActId)
	}
	Var.Reward = true
	Var.RewardTime = GoUtil.Now()
	return activityCfg.GetActivityRewardItems(ActId), nil
}
