package limitedTimeEvent

import (
	"fmt"
	"math"
	"server/GoUtil"
	limitedTimeEventCfg "server/conf/limitedTimeEvent"
	mergeDataCfg "server/conf/mergeData"
	"server/game/mod/item"
	"server/msg"
)

const (
	EVENT_TYPE_HIGH_ROLLER   = 1  // high roller
	EVENT_TYPE_SUPER_EMIT    = 2  // 超级发射器
	EVENT_TYPE_METEOR_SHOW   = 3  // 流星雨
	EVENT_TYPE_CHEST_RAIN    = 4  // 宝箱雨
	EVENT_TYPE_SUPER_ORDER   = 5  // 超级订单
	EVENT_TYPE_SENCE_DASH    = 6  // 场景冲刺
	EVENT_TYPE_CARD_FESTIVAL = 7  // 卡牌节
	EVENT_TYPE_GOLDCARD_EX   = 8  // 金卡交换
	EVENT_TYPE_PET_THIEF     = 9  // 宠物小偷
	EVENT_TYPE_FAST_PRODUCE  = 10 // 连击快手
	EVENT_TYPE_CAT_CYCLONE   = 11 // 猫咪飓风
	EVENT_TYPE_MONEY_CAT     = 12 // 招财猫
	EVENT_TYPE_LUCKY_CAT     = 13 // 幸运猫
)

const (
	EVENT_NOTIFY_TYPE_ADD = 0
	EVENT_NOTIFY_TYPE_DEL = 1
)

type LimitedTimeEventMod struct {
	EventList      map[int]*LTEInfo
	Triggered      map[int]struct{}
	Progress       int
	ProgressReward map[int]int
	LastSelect     int
	Lv             int // 玩家等级 零点更新
	ProgressMax    int
	LastOption     []int
	BonusNum       int
	First          bool
}

type LTEInfo struct {
	StartT int64                  // 开始时间
	Remian int64                  // 剩余时间
	Info   map[string]interface{} // 事件信息
	D      interface{}
}

type MoneyCat struct {
	Id      int
	EndTime int64
	Mul     float64
}

type LuckyCat struct {
	Earnings int
}

func (l *LimitedTimeEventMod) InitData(Lv int) {
	if l.EventList == nil {
		l.EventList = make(map[int]*LTEInfo)
	}
	if l.Triggered == nil {
		l.Triggered = make(map[int]struct{})
	}
	if l.ProgressReward == nil {
		l.ProgressReward = make(map[int]int)
	}
	if l.ProgressMax == 0 {
		l.Lv = Lv
		l.ProgressMax = limitedTimeEventCfg.GetProgressMax(Lv, l.BonusNum)
	}
}

func (l *LimitedTimeEventMod) ZeroUpdate(Lv int) {
	l.Lv = Lv
	l.BonusNum = 0
	l.ProgressMax = limitedTimeEventCfg.GetProgressMax(Lv, l.BonusNum)
}

// 判断限时事件是否存在
func (l *LimitedTimeEventMod) CheckExist(EventId int) bool {
	info, ok := l.EventList[EventId]
	if !ok {
		return false
	}

	return GoUtil.Now() <= info.StartT+info.Remian
}

// 触发限时事件
func (l *LimitedTimeEventMod) Trigger(Lv int) (int, []int, []int) {
	if Lv < 4 {
		return 0, nil, nil
	}
	TriggerEvent := limitedTimeEventCfg.GetLimitedTimeEventCfg()
	TimeoutEvent := make([]int, 0)
	AddEvent := make([]int, 0)
	Now := GoUtil.Now()

	for _, v := range TriggerEvent {
		if _, ok := l.Triggered[v.Id]; ok { //不重复触发
			continue
		}
		if info, ok := l.EventList[v.EventId]; ok {
			info.Remian += int64(v.Duration)
		} else {
			LTEInfo := &LTEInfo{
				StartT: Now,
				Remian: int64(v.Duration),
			}
			initEventInfo(LTEInfo, v.EventId)
			l.EventList[v.EventId] = LTEInfo
			AddEvent = append(AddEvent, v.EventId)
		}
		l.Triggered[v.Id] = struct{}{}
	}

	Remain := 0
	//删除过期事件
	for k, v := range l.EventList {
		if v.StartT+v.Remian <= Now {
			delete(l.EventList, k)
			TimeoutEvent = append(TimeoutEvent, k)
			continue
		}
		EndTime := (v.StartT + v.Remian) - Now
		Remain = GoUtil.IfTrue(Remain > 0, min(Remain, int(EndTime)), int(EndTime)).(int)
	}
	return Remain, TimeoutEvent, AddEvent
}

// 增加限时事件
func (l *LimitedTimeEventMod) AddEvent(EventId, Duration int) int64 {
	if v, ok := l.EventList[EventId]; ok {
		v.Remian += int64(Duration)
	} else {
		Event := &LTEInfo{
			Remian: int64(Duration),
			StartT: GoUtil.Now(),
		}
		initEventInfo(Event, EventId)
		l.EventList[EventId] = Event
	}
	return l.EventList[EventId].Remian + l.EventList[EventId].StartT
}

// 每周刷新
func (l *LimitedTimeEventMod) WeekUpdate() {
	l.Triggered = make(map[int]struct{})
}

func (l *LimitedTimeEventMod) GetMoneyCatReward(ChessList []int) []*item.Item {
	L := l.EventList[EVENT_TYPE_MONEY_CAT]
	if L == nil {
		return nil
	}
	Star := mergeDataCfg.GetMergeStar(ChessList)
	d := L.D.(*MoneyCat)
	mul := d.Mul
	// 超时
	if d.EndTime > 0 && GoUtil.Now() >= d.EndTime {
		NextMul, Cd := limitedTimeEventCfg.GetMoneyCat(1)
		d.EndTime = int64(Cd)
		d.Mul = NextMul
		Star = int(float64(Star) * NextMul)
	} else {
		MaxId := limitedTimeEventCfg.GetMoneyCatMax()
		NextId := min(d.Id+1, MaxId)
		NextMul, Cd := limitedTimeEventCfg.GetMoneyCat(NextId)
		d.Id = NextId
		d.Mul = NextMul
		d.EndTime = GoUtil.Now() + int64(Cd)
		Star = int(float64(Star) * mul)
	}

	return []*item.Item{{Id: item.ITEM_STAR_ID, Num: Star}}
}

func (l *LimitedTimeEventMod) BackData() *msg.ResLimitEvent {
	Res := &msg.ResLimitEvent{}
	EventList := make(map[int32]*msg.LimitEvent)
	for k, v := range l.EventList {
		EventList[int32(k)] = getLimitEventMsg(k, v)
	}
	Res.LimitEventList = EventList
	return Res
}

func (l *LimitedTimeEventMod) ProgressBackData() *msg.ResLimitEventProgress {
	r := make(map[int32]int32)
	for k, v := range l.ProgressReward {
		Type := limitedTimeEventCfg.GetProgressRewardType(v)
		r[int32(k)] = int32(Type)
	}
	return &msg.ResLimitEventProgress{
		Progress:       int32(l.Progress),
		ProgressReward: r,
		ProgressMax:    int32(l.ProgressMax),
	}
}

// 获取流星雨奖励
func (l *LimitedTimeEventMod) GetMeteorReward(MergeList []int) []*item.Item {
	MaxLv := 0
	Star := 0
	for _, v := range MergeList {
		ChessLv := mergeDataCfg.GetLvById(v)
		Star += mergeDataCfg.GetStarById(v)
		if ChessLv > MaxLv {
			MaxLv = ChessLv
		}
	}
	Add := limitedTimeEventCfg.GetMeteorAdd(MaxLv)
	NewStar := int(float64(Star) * (float64(Add) / 100))
	NewStar = max(NewStar, 1)
	return []*item.Item{{Id: item.ITEM_STAR_ID, Num: NewStar}}
}

// 获取宝箱雨奖励
func (l *LimitedTimeEventMod) GetChestReward(MergeList []int) []*item.Item {
	Star := 0
	for _, v := range MergeList {
		Star += mergeDataCfg.GetStarById(v)
	}

	return limitedTimeEventCfg.GetChestReward(Star)
}

// 获取场景冲刺奖励
func (l *LimitedTimeEventMod) GetSceneDashReward() (int, []*item.Item) {
	ProbList := limitedTimeEventCfg.GetSenceJackpotProb()
	Id := GoUtil.RandMap(ProbList)
	return Id, limitedTimeEventCfg.GetSenceJackpotReward(Id)
}

// 获取连击快手奖励
func (l *LimitedTimeEventMod) GetFastProduceReward(Energy int) ([]*item.Item, int64, int, error) {
	Event, ok := l.EventList[EVENT_TYPE_FAST_PRODUCE]
	if !ok {
		return nil, 0, 0, fmt.Errorf("FastProduce event not exist")
	}

	Now := GoUtil.Now()
	if Now < GoUtil.Int64(Event.Info["NextPlay"]) {
		return nil, 0, 0, fmt.Errorf("FastProduce CD")
	}
	Times := GoUtil.Int(Event.Info["Times"])
	Times++
	Event.Info["Times"] = Times
	MaxTimes := limitedTimeEventCfg.GetFastProduceMaxTimes()
	Times = min(Times, MaxTimes)
	CD := limitedTimeEventCfg.GetFastCD()
	Event.Info["NextPlay"] = GoUtil.Now() + int64(CD) // CD5分钟
	return limitedTimeEventCfg.GetFastProduceReward(Times, Energy), GoUtil.Now() + int64(CD), Event.Info["Times"].(int), nil
}

func (l *LimitedTimeEventMod) ResetFastProduceCD() {
	Event, ok := l.EventList[EVENT_TYPE_FAST_PRODUCE]
	if !ok {
		return
	}
	Event.Info["NextPlay"] = 0
}

func (l *LimitedTimeEventMod) GetFastProduceInfo() (int64, int) {
	Event, ok := l.EventList[EVENT_TYPE_FAST_PRODUCE]
	if !ok {
		return 0, 0
	}
	return GoUtil.Int64(Event.Info["NextPlay"]), GoUtil.Int(Event.Info["Times"])
}

// 增加进度
func (l *LimitedTimeEventMod) AddProgress(Lv int) {
	UnlockLv := limitedTimeEventCfg.GetUnlockLv()
	if Lv < UnlockLv {
		return
	}
	l.Progress++
	if l.Progress == l.ProgressMax {
		if !l.First {
			l.First = true
			EventId := limitedTimeEventCfg.GetFirstEvent()
			l.ProgressReward = map[int]int{
				1: EventId,
			}
			l.LastOption = []int{EventId}
			return
		}
		SelectNum := limitedTimeEventCfg.GetProgressSelectNum(Lv)
		BonusLv := limitedTimeEventCfg.GetBonusLv(Lv)
		RandMap := limitedTimeEventCfg.GetProgressRewardRand(BonusLv)
		n := 0
		r := make([]int, 0)
		LastOption := l.LastOption
		for n < 10 {
			n++
			r = GoUtil.RandMapNum(RandMap, SelectNum)
			Id := 1
			for _, v := range r {
				l.ProgressReward[Id] = v
				Id++
			}

			if !GoUtil.SliceEqual(LastOption, r) {
				break
			}
		}
		l.LastOption = r
	}
}

// 选择进度奖励
func (l *LimitedTimeEventMod) SelectProgressReward(Id int) ([]*item.Item, map[int]int, int, error) {
	RewardId, ok := l.ProgressReward[Id]
	if !ok {
		return nil, nil, 0, fmt.Errorf("RewardId not exist")
	}
	r1 := l.ProgressReward
	r2 := l.ProgressMax
	Item := limitedTimeEventCfg.GetProgressReward(RewardId)
	l.LastSelect = RewardId
	l.ProgressReward = make(map[int]int)
	l.Progress = 0
	l.BonusNum++
	l.ProgressMax = limitedTimeEventCfg.GetProgressMax(l.Lv, l.BonusNum)
	return Item, r1, r2, nil
}

func (l *LimitedTimeEventMod) RemoveSuperOrder() bool {
	if _, ok := l.EventList[EVENT_TYPE_SUPER_ORDER]; ok {
		delete(l.EventList, EVENT_TYPE_SUPER_ORDER)
		return true
	}
	return false
}

func (l *LimitedTimeEventMod) LuckyCat(ChessId int) error {
	L := l.EventList[EVENT_TYPE_LUCKY_CAT]
	if L == nil {
		return fmt.Errorf("LuckyCat event not exist")
	}
	MaxEarning := limitedTimeEventCfg.GetLuckyCatMaxEarning(int(L.Remian))
	d := L.D.(*LuckyCat)
	Lv := mergeDataCfg.GetLvById(ChessId)
	Earn := math.Pow(2, float64(Lv-1))
	d.Earnings += int(Earn)
	if d.Earnings >= MaxEarning {
		delete(l.EventList, EVENT_TYPE_LUCKY_CAT)
	}
	return nil
}

// 获取超级订单奖励
func initEventInfo(E *LTEInfo, EventType int) {
	switch EventType {
	case EVENT_TYPE_FAST_PRODUCE:
		E.Info = map[string]interface{}{
			"Times":    0,
			"NextPlay": 0,
		}
	case EVENT_TYPE_MONEY_CAT:
		E.D = &MoneyCat{
			Id:      1,
			EndTime: 0,
			Mul:     1.1,
		}
	case EVENT_TYPE_LUCKY_CAT:
		E.D = &LuckyCat{
			Earnings: 0,
		}
	}
}

func getLimitEventMsg(t int, e *LTEInfo) *msg.LimitEvent {
	Cd := 0
	Mul := 0.0

	switch t {
	case EVENT_TYPE_FAST_PRODUCE:
		NextPlay := GoUtil.Int64(e.Info["NextPlay"])
		Cd = int(max(NextPlay-GoUtil.Now(), 0))
	case EVENT_TYPE_MONEY_CAT:
		d := e.D.(*MoneyCat)
		Cd = int(d.EndTime)
		Mul = d.Mul
	}
	return &msg.LimitEvent{
		EndTime:   int32(e.StartT + e.Remian),
		Cd:        int32(Cd),
		Mul:       float32(Mul),
		StartTime: int32(e.StartT),
	}
}
