package model

import (
	"active/constant"
	"encoding/json"
	"fmt"
	"log"
	"math"
	"sync/atomic"
	"time"

	"git.jiaxianghudong.com/go/logs"
	"git.jiaxianghudong.com/webs/pkg/rds"
)

var rebateExpireAt int32

//RebateData 投资返利存储
type RebateData struct {
	Levels map[int]*LevelData `json:"levels"` //档次
}

//LevelData 等级数据
type LevelData struct {
	Tm     time.Time `json:"paytime"` //支付时间  时间戳
	Status int       `json:"status"`  //领取状态  位运算1+2+4
}

//RebatePropData 投资奖励
type RebatePropData struct {
	Investment []RebatePropInfos `json:"investment_rebate"`
}

//RebatePropInfos 奖励具体信息
type RebatePropInfos struct {
	Amount    int64               `json:"investment_amount"`
	LoginDay1 []RebatePropDetails `json:"login_day1"`
	LoginDay2 []RebatePropDetails `json:"login_day2"`
	LoginDay3 []RebatePropDetails `json:"login_day3"`
}

//RebatePropDetails 奖励具体详细信息
type RebatePropDetails struct {
	Prop int32 `json:"props_id"`
	Num  int64 `json:"num"`
}

//GetRebateData 获取累充用户存储数据
func GetRebateData(UserID string, tm string) *RebateData {

	data := rds.Redis.HGet(fmt.Sprintf(constant.REBATEDATA, tm), UserID).Val()
	rd := new(RebateData)
	if data != "" {
		err := json.Unmarshal([]byte(data), &rd)
		if err != nil {
			logs.Errorf("redpacket[%s] GetRebateData json err:%v", UserID, err)
			return rd
		}
	}

	return rd
}

//SetRebateData 设置用户存储
func (t *RebateData) SetRebateData(UserID, tm, endtm string) bool {
	da, _ := json.Marshal(t)
	rds.Redis.HSet(fmt.Sprintf(constant.REBATEDATA, tm), UserID, string(da))

	if atomic.CompareAndSwapInt32(&rebateExpireAt, 0, 1) {
		end, _ := time.Parse("2006-01-02 15:04:05", endtm)
		expireAt := end.Add(constant.REBATEDATATIMEOUT)

		log.Printf("---更新投资返利数据过期时间: expireAt:%v", expireAt)
		rds.Redis.ExpireAt(fmt.Sprintf(constant.REBATEDATA, tm), expireAt)
	}

	return true
}

//GetPropByLevel 获取奖励
func GetPropByLevel(info string, typ, level int) map[int32]int64 {

	r, err := parseRebate(info)
	if err != nil {
		return nil
	}

	if len(r.Investment) < typ {
		return nil
	}

	rd := []RebatePropDetails{}

	switch level {
	case 1:
		rd = r.Investment[typ-1].LoginDay1
	case 2:
		rd = r.Investment[typ-1].LoginDay2
	case 3:
		rd = r.Investment[typ-1].LoginDay3
	}

	data := make(map[int32]int64, 0)
	for _, v := range rd {
		data[v.Prop] = v.Num
	}

	return data
}

//ConsumePropByType 消耗道具
func ConsumePropByType(info string, typ int) map[int32]int64 {

	r, err := parseRebate(info)
	if err != nil {
		return nil
	}

	if len(r.Investment) < typ {
		return nil
	}

	data := make(map[int32]int64, 0)
	amout := r.Investment[typ-1].Amount
	if amout < 0 {
		amout = -amout
	}
	data[17] = -amout
	return data
}

func parseRebate(info string) (RebatePropData, error) {
	var r RebatePropData
	if err := json.Unmarshal([]byte(info), &r); err != nil {
		logs.Errorf("parseRebate json data:%s err:%v", info, err)
		return r, err
	}

	return r, nil
}

//CheckSubDay 计算两个时间点的相差天数
func CheckSubDay(tm time.Time) int {
	dt1 := time.Date(tm.Year(), tm.Month(), tm.Day(), 0, 0, 0, 0, time.Local)
	dt2 := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.Local)
	return int(math.Ceil(dt2.Sub(dt1).Hours() / 24))
}
