package sevenLogin

import (
	"fmt"
	sevenLoginCfg "server/conf/seven_login"
	"server/game/mod/item"
	GoUtil "server/game_util"
	"server/msg"
)

type SevenLoginMod struct {
	LoginReward    map[int]Reward // 7天登录奖励
	MonthReward    map[int]Reward // 月登录奖励
	DayR           int            // 登录天数
	Active         int            // 活跃度
	IsLogin        bool           // 今日是否登录
	IsBack         bool           // 是否召回
	WeekResetTime  int64          // 7天重置时间
	MonthResetTime int64          // 月重置时间
}

type Reward struct {
	Item1  []*item.Item
	Item2  []*item.Item
	Item3  []*item.Item
	Status bool // 是否已领取
}

func (s *SevenLoginMod) InitData() {

}

func (s *SevenLoginMod) ZeroUpdate(Add, LastLoginTime int) {
	WeekZeroTimestamp := GoUtil.WeekZeroTimestamp()
	Now := GoUtil.Now()
	if WeekZeroTimestamp > s.WeekResetTime {
		RewardType := GoUtil.IfTrue(Now-int64(LastLoginTime) > GoUtil.SEVENDAYS && LastLoginTime > 0, BACK_REWARD, DEFAULT_REWARD).(int)
		s.IsBack = RewardType == BACK_REWARD
		s.LoginReward = randWeekReward(Add, RewardType)
		s.WeekResetTime = WeekZeroTimestamp
		s.DayR = 0
	}

	if Now > s.MonthResetTime+GoUtil.MONTHDAYS {
		s.MonthReward = randMonthReward()
		s.Active = 0
		s.MonthResetTime = s.MonthResetTime + (Now-s.MonthResetTime)/GoUtil.MONTHDAYS*GoUtil.MONTHDAYS
	}
	s.DayR++
}

func (s *SevenLoginMod) GetReward(Id int) ([]*item.Item, error) {
	if Id > s.DayR {
		return nil, fmt.Errorf("id greater than login days")
	}
	Reward := s.LoginReward[Id]
	if Reward.Status {
		return nil, fmt.Errorf("reward already received")
	}
	Reward.Status = true
	s.LoginReward[Id] = Reward
	s.Active++
	item := make([]*item.Item, 0)
	item = append(item, Reward.Item1...)
	item = append(item, Reward.Item2...)
	item = append(item, Reward.Item3...)
	return item, nil
}

func (s *SevenLoginMod) GetMonthReward(Id int) ([]*item.Item, error) {
	NeedActive := sevenLoginCfg.GetMonthActive(Id)
	if NeedActive > s.Active {
		return nil, fmt.Errorf("id greater than active")
	}
	Reward := s.MonthReward[Id]
	if Reward.Status {
		return nil, fmt.Errorf("reward already received")
	}
	Reward.Status = true
	s.MonthReward[Id] = Reward
	item := make([]*item.Item, 0)
	item = append(item, Reward.Item1...)
	item = append(item, Reward.Item2...)
	item = append(item, Reward.Item3...)
	return item, nil
}

func (s *SevenLoginMod) BackData() *msg.ResSevenLogin {
	WeekReward := make([]*msg.SevenLoginReward, 0)
	MonthReward := make([]*msg.SevenLoginReward, 0)
	for k, v := range s.LoginReward {
		Status := GoUtil.IfTrue(!v.Status && s.DayR >= k, 1, 0).(int)
		Status = GoUtil.IfTrue(v.Status, 2, Status).(int)
		WeekReward = append(WeekReward, &msg.SevenLoginReward{
			Item1:  item.ItemToMsg(v.Item1),
			Item2:  item.ItemToMsg(v.Item2),
			Item3:  item.ItemToMsg(v.Item3),
			Status: int32(Status),
			Id:     int32(k),
		})
	}

	for k, v := range s.MonthReward {
		if k < 0 {
			continue
		}
		Active := sevenLoginCfg.GetMonthActive(k)
		Status := GoUtil.IfTrue(!v.Status && s.Active >= Active, 1, 0).(int)
		Status = GoUtil.IfTrue(v.Status, 2, Status).(int)
		MonthReward = append(MonthReward, &msg.SevenLoginReward{
			Item1:  item.ItemToMsg(v.Item1),
			Item2:  item.ItemToMsg(v.Item2),
			Item3:  item.ItemToMsg(v.Item3),
			Status: int32(Status),
			Id:     int32(k),
		})
	}

	return &msg.ResSevenLogin{
		WeekReward:  WeekReward,
		MonthReward: MonthReward,
		Active:      int32(s.Active),
		IsBack:      s.IsBack,
	}
}
