package game

import (
	"server/GoUtil"
	guesscolorCfg "server/conf/guessColor"
	miningCfg "server/conf/mining"
	raceCfg "server/conf/race"
	"server/game/mod/item"
	"server/msg"
)

// 活动模块 登录
func ActivityLogin(p *Player) {
	ActivityInfo := GetActivityInfo(p, ACT_TYPE_MINING)
	if ActivityInfo != nil {
		MiningMod := p.PlayMod.getMiningMod()
		MiningMod.Login(ActivityInfo.Id)
	}
	ActivityInfo = GetActivityInfo(p, ACT_TYPE_GUESS_COLOR)
	if ActivityInfo != nil {
		GuessColorMod := p.PlayMod.getGuessColorMod()
		GuessColorMod.Login(ActivityInfo.Id)
	}
}

// 活动模块 零点更新
func ActivityZeroUpdate(p *Player) {
	ActivityInfo := GetActivityInfo(p, ACT_TYPE_MINING)
	if ActivityInfo != nil {
		MiningMod := p.PlayMod.getMiningMod()
		MiningMod.ZeroUpdate(ActivityInfo.Id)
	}
	ActivityInfo = GetActivityInfo(p, ACT_TYPE_GUESS_COLOR)
	if ActivityInfo != nil {
		GuessColorMod := p.PlayMod.getGuessColorMod()
		GuessColorMod.ZeroUpdate(ActivityInfo.Id)
	}
}

func GetActivityInfo(p *Player, actType int) *ActivityInfo {
	for _, v := range p.activity {
		if v.Type == actType {
			return v
		}
	}
	return nil
}

func GetActivityStatus(p *Player, actType int) int {
	ActivityInfo := GetActivityInfo(p, actType)
	if ActivityInfo == nil {
		return 0
	}
	Now := GoUtil.Now()
	if Now < ActivityInfo.StartT {
		return 0
	}
	if Now > ActivityInfo.EndT {
		return 2
	}
	return 1
}

func MiningBackData(p *Player) {
	ActivityInfo := GetActivityInfo(p, ACT_TYPE_MINING)
	if ActivityInfo == nil {
		return
	}
	Status := GetActivityStatus(p, ACT_TYPE_MINING)
	Template := miningCfg.GetTemplate(ActivityInfo.Id)
	MiningMod := p.PlayMod.getMiningMod()
	p.PushClientRes(&msg.ResMining{
		Id:       int32(ActivityInfo.Id),
		Status:   int32(Status),
		EndTime:  int32(ActivityInfo.EndT),
		Template: int32(Template),
		Pass:     int32(MiningMod.GetPass()),
		Gem:      GoUtil.IntToInt32(MiningMod.GetGem()),
		Map:      MiningMod.GetMap(),
		Mining:   int32(MiningMod.GetMining()),
	})
}

func GuessColorBackData(p *Player) {
	ActivityInfo := GetActivityInfo(p, ACT_TYPE_GUESS_COLOR)
	if ActivityInfo == nil {
		return
	}
	Status := GetActivityStatus(p, ACT_TYPE_GUESS_COLOR)
	GuessColorMod := p.PlayMod.getGuessColorMod()
	Color := make([]int32, 0)
	Pos := make([]int32, 0)
	for k, v := range GuessColorMod.Answer {
		if GuessColorMod.Progress[k] > 0 {
			Pos = append(Pos, int32(k))
			continue
		}
		Color = append(Color, int32(v))
	}
	p.PushClientRes(&msg.ResGuessColor{
		Id:      int32(ActivityInfo.Id),
		Status:  int32(Status),
		EndTime: int32(ActivityInfo.EndT),
		Pass:    int32(GuessColorMod.Pass),
		Opponent: &msg.Opponent{
			Name:     GuessColorMod.Opponent.Name,
			Face:     int32(GuessColorMod.Opponent.Face),
			Avatar:   int32(GuessColorMod.Opponent.Avatar),
			Progress: int32(GuessColorMod.Opponent.Progress),
		},
		Color: Color,
		Pos:   Pos,
	})
}

func RaceBackData(p *Player) {
	ActivityInfo := GetActivityInfo(p, ACT_TYPE_RACE)
	if ActivityInfo == nil {
		return
	}
	Status := GetActivityStatus(p, ACT_TYPE_RACE)
	RaceMod := p.PlayMod.getRaceMod()
	Opponent := make([]*msg.Raceopponent, 0)
	for _, v := range RaceMod.Opponent {
		Opponent = append(Opponent, &msg.Raceopponent{
			Id:       int32(v.Id),
			Image:    int32(v.Image),
			Progress: int32(v.Progress),
		})
	}
	p.PushClientRes(&msg.ResRace{
		Id:            int32(ActivityInfo.Id),
		Status:        int32(Status),
		EndTime:       int32(ActivityInfo.EndT),
		Pass:          int32(RaceMod.Pass),
		GameStartTime: int32(RaceMod.StartTime),
		Progress:      int32(RaceMod.Progress),
		GameEndTime:   int32(RaceMod.EndTime),
		Opponent:      Opponent,
	})
}

func RedBackData(p *Player) {
	result := make(map[int32]int32)
	Now := GoUtil.Now()
	for _, v := range p.activity {
		if v.StartT < Now && v.EndT > Now {
			result[int32(v.Type)] = int32(p.GetRed(v))
		}
	}
	p.PushClientRes(&msg.ResActRed{Red: result})
}

func GetActivityItem(p *Player, ActType []int) []*item.Item {
	Items := make([]*item.Item, 0)
	for _, v := range ActType {
		Status := GetActivityStatus(p, v)
		ActivityInfo := GetActivityInfo(p, v)
		if ActivityInfo == nil {
			continue
		}
		if Status != ACT_STATUS_START {
			continue
		}
		switch v {
		case ACT_TYPE_MINING:
			Item := miningCfg.GetLoseItem(ActivityInfo.Id)
			Items = item.Merge(Items, Item)
		case ACT_TYPE_GUESS_COLOR:
			Item := guesscolorCfg.GetLoseItem(ActivityInfo.Id)
			Items = item.Merge(Items, Item)
		case ACT_TYPE_RACE:
			ItemId := raceCfg.GetCoin(ActivityInfo.Id)
			Item := item.NewItem(ItemId, 1)
			Items = append(Items, Item)
		}
	}
	return Items
}
