package main

import (
	"../mod"
	"../nt"
	pb "../protos"
	"github.com/pkg/errors"
	"time"
)

var (
	errNoBonus = errors.New("vip level no bonus")

	_mapWeekInt = map[time.Weekday]int{
		time.Monday:    1,
		time.Tuesday:   2,
		time.Wednesday: 3,
		time.Thursday:  4,
		time.Friday:    5,
		time.Saturday:  6,
		time.Sunday:    7,
	}
)

// 得到当前 vip 的下一级总共需要多少钱
func GetNextVipNeed(curVip int32) int64 {
	if curVip < 1 || curVip >= int32(len(nt.DynConf.Vip)) {
		return 0
	}
	return nt.DynConf.Vip[curVip].Lowest * 100
}

// 得到当前时间的上个1号
func Last1stMonthday(tm int64) int64 {
	tmNow := time.Unix(tm, 0)
	d := tmNow.Day()
	h := tmNow.Hour()
	m := tmNow.Minute()
	s := tmNow.Second()
	return tm - int64((d-1)*24*60*60+h*60*60+m*60+s)
}

func LastMonday(tm int64) int64 {
	now := time.Unix(tm, 0)
	week := now.Weekday()
	h := now.Hour()
	m := now.Minute()
	s := now.Second()
	return tm - int64((_mapWeekInt[week]-1)*24*60*60+h*60*60+m*60+s)
}

func UserGetSingle(guid uint64, level int32) (int64, int64, pb.ErrCode) {
	var idx int = int(level - 1)
	if idx < 0 || idx >= len(nt.DynConf.Vip) {
		return 0, 0, pb.ErrCode_UNKNOWN
	}
	vip := nt.DynConf.Vip[idx]
	if vip.Bonus <= 0 {
		return 0, 0, pb.ErrCode_INSUFFICIENT
	}
	should_bonus := vip.Bonus * 100

	last_bonus, err := mod.VipGetBonus(guid)
	if err != nil {
		return 0, 0, pb.ErrCode_UNKNOWN
	}
	if last_bonus >= should_bonus {
		return 0, 0, pb.ErrCode_DUP_OPER
	}
	// TODO: 优化为脚本
	err = mod.VipUpdateBonus(guid, should_bonus)
	if err != nil {
		return 0, 0, pb.ErrCode_FAILED
	}
	newBalance, err := mod.AddUserBalance(guid, should_bonus)
	if err != nil {
		return 0, 0, pb.ErrCode_FAILED
	}

	return newBalance, should_bonus, pb.ErrCode_OK
}

func UserGetWeekly(guid uint64, level int32) (int64, int64, pb.ErrCode) {
	var idx int = int(level - 1)
	if idx < 0 || idx >= len(nt.DynConf.Vip) {
		return 0, 0, pb.ErrCode_UNKNOWN
	}
	vip := nt.DynConf.Vip[idx]
	if vip.Weekly <= 0 {
		return 0, 0, pb.ErrCode_INSUFFICIENT
	}
	should_bonus := vip.Weekly * 100

	last_time, err := mod.VipGetWeekly(guid)
	if err != nil {
		return 0, 0, pb.ErrCode_UNKNOWN
	}
	tnow := time.Now().Unix()
	if LastMonday(tnow) == LastMonday(last_time) {
		return 0, 0, pb.ErrCode_DUP_OPER
	}
	// TODO: 优化为脚本
	err = mod.VipUpdateWeekly(guid, tnow)
	if err != nil {
		return 0, 0, pb.ErrCode_FAILED
	}
	newBalance, err := mod.AddUserBalance(guid, should_bonus)
	if err != nil {
		return 0, 0, pb.ErrCode_FAILED
	}

	return newBalance, should_bonus, pb.ErrCode_OK
}

func UserGetMonthly(guid uint64, level int32) (int64, int64, pb.ErrCode) {
	var idx int = int(level - 1)
	if idx < 0 || idx >= len(nt.DynConf.Vip) {
		return 0, 0, pb.ErrCode_UNKNOWN
	}
	vip := nt.DynConf.Vip[idx]
	if vip.Monthly <= 0 {
		return 0, 0, pb.ErrCode_INSUFFICIENT
	}
	should_bonus := vip.Monthly * 100

	last_time, err := mod.VipGetMonthly(guid)
	if err != nil {
		return 0, 0, pb.ErrCode_UNKNOWN
	}
	tnow := time.Now().Unix()
	if Last1stMonthday(tnow) == Last1stMonthday(last_time) {
		return 0, 0, pb.ErrCode_DUP_OPER
	}
	// TODO: 优化为脚本
	err = mod.VipUpdateMonthly(guid, tnow)
	if err != nil {
		return 0, 0, pb.ErrCode_FAILED
	}
	newBalance, err := mod.AddUserBalance(guid, should_bonus)
	if err != nil {
		return 0, 0, pb.ErrCode_FAILED
	}

	return newBalance, should_bonus, pb.ErrCode_OK
}
