package activity

import (
	"encoding/gob"
	"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 init() {
	gob.Register(&Gift{})
}

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

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 {
	Var := a.GetVar(key)
	if Var == nil {
		Var = &Gift{}
		a.SetVar(key, Var)
	}
	return Var.(*Gift)
}

func (a *Activity) setGiftVar(key int, Var *Gift) {
	a.SetVar(key, Var)
}

func (a *Activity) GetActivityStatus(data *gamedata.ActivityData) int {
	switch data.AType {
	case ACT_ATYPE_NORMAL:
		return ACT_STATUS_START
	case ACT_ATYPE_LIMIT_GIFT:
		switch data.Type {
		case ACT_TYPE_ADD_GIFT:
			Var := a.getGIftVar(data.Id)
			if Var.Reward {
				return ACT_STATUS_NOT_START
			}
			if Var.Buy {
				return ACT_STATUS_END
			}
		case ACT_TYPE_SUPER_GIFT:
			Var := a.getGIftVar(data.Id)
			if Var.Reward {
				return ACT_STATUS_NOT_START
			}
			if Var.Buy {
				return ACT_STATUS_END
			}
		default:
			Var := a.getGIftVar(data.Id)
			if Var.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("not buy")
	}
	if Var.Reward {
		return nil, fmt.Errorf("reward already get")
	}
	Var.Reward = true
	Var.RewardTime = GoUtil.Now()
	return activityCfg.GetAcitivityRewardItems(ActId), nil
}
