package h5video

import (
	"active/internal/model/goldbrick"
	"active/tools"
	"fmt"
	"git.jiaxianghudong.com/webs/pkg/rds"
	"git.jiaxianghudong.com/webs/pkg/xgorm"
	"log"
	"time"
)

var (
	H5VideoConf = &H5VideoConfig{}
	RdH5video   = "joyh5draw_%v"
	RdH5video2  = "joyh5draw%v_%v"
)

type H5VideoConfig struct {
	DataId     int32             `json:"data_id" yaml:"data_id"`
	Awards     []int64           `json:"awards" yaml:"awards"`
	Rate       []int             `json:"rate" yaml:"rate"`
	RateV2     []int             `json:"rate_v2" yaml:"rate_v2"`
	IndexLimit map[int]int64     `json:"index_limit" yaml:"index_limit"`
	AwardsMap  map[int][][]int64 `json:"awards_map" yaml:"awards_map"`
	AppId      string            `json:"app_id" yaml:"app_id"`
	PingUrl    string            `json:"ping_url" yaml:"ping_url"`
	AppSecret  string            `json:"app_secret" yaml:"app_secret"`
}

func PreConfig() {
	tools.ReloadYaml("h5_video.yaml", H5VideoConf)
	rate := 0
	for i := range H5VideoConf.Rate {
		rate += H5VideoConf.Rate[i]
		H5VideoConf.Rate[i] = rate
	}
	rate = 0
	for i := range H5VideoConf.RateV2 {
		rate += H5VideoConf.RateV2[i]
		H5VideoConf.RateV2[i] = rate
	}
}

func GetVideoDrawHistory(userId string, nickname string, add bool, tm string, v string) (*VideoDrawHistory, error) {
	videoDrawHistory := &VideoDrawHistory{}
	if userId == "" {
		return videoDrawHistory, nil
	}
	update := false
	xdb := xgorm.NewConn(goldbrick.GoldBrickGorm)
	tableName := VideoDrawHistory{}.TableName()
	if v != "" {
		tableName += v
	}

	err := xdb.Table(tableName).Where("user_id = ?", userId).First(videoDrawHistory).Error
	if (err != nil || videoDrawHistory.Id == 0) && add && userId != "" {
		videoDrawHistory.UserId = userId
		videoDrawHistory.Nickname = nickname
		update = true
	}
	if !judgeDrawTm(userId, tm, v) {
		update = true
		videoDrawHistory.Number = 0
		videoDrawHistory.NumberLimit = 11
	}

	if update {
		err = xdb.Table(tableName).Save(videoDrawHistory).Error
	}
	if userId == "oHdo261vjs61C7kOYeBqDN1HZlTA" {
		log.Println("joydebug", userId, update, videoDrawHistory, err)
	}
	return videoDrawHistory, err
}

func judgeDrawTm(userId string, tm string, v string) bool {
	demo := fmt.Sprintf(RdH5video, tm)
	if v != "" {
		demo = fmt.Sprintf(RdH5video2, v, tm)
	}
	val := rds.Redis.HGet(demo, userId).Val()
	if userId == "oHdo261vjs61C7kOYeBqDN1HZlTA" {
		log.Println("joydebug", userId, demo, val)
	}
	if val == "" {
		rds.Redis.HSet(demo, userId, "1").Val()
		rds.Redis.ExpireAt(demo, time.Now().Add(24*time.Hour))
		return false
	} else {
		return true
	}
}

func GetCodeList(userId string, v string) ([]*VideoCode, error) {
	xdb := xgorm.NewConn(goldbrick.GoldBrickGorm)
	videoCodeList := make([]*VideoCode, 0)
	tableName := VideoCode{}.TableName()
	if v != "" {
		tableName += v
	}
	err := xdb.Table(tableName).Where("user_id = ? and award_id>=0", userId).Find(&videoCodeList).Error
	return videoCodeList, err
}

func GetCodeAllList(userId string, v string) ([]*VideoCode, error) {
	xdb := xgorm.NewConn(goldbrick.GoldBrickGorm)
	videoCodeList := make([]*VideoCode, 0)

	tableName := VideoCode{}.TableName()
	if v != "" {
		tableName += v
	}
	var err error
	if userId != "" {
		xdb = xdb.Table(tableName).Where("user_id = ? and award_id >= 0", userId).Order("create_time desc")
	} else {
		xdb = xdb.Table(tableName).Order("create_time desc").Limit(20)
	}
	err = xdb.Find(&videoCodeList).Error
	for i := range videoCodeList {
		videoCodeList[i].AwardId = videoCodeList[i].AwardsId
		if userId == "" {
			videoCodeList[i].Code = ""
			videoCodeList[i].UserId = ""
		}
	}
	return videoCodeList, err
}
