package system

import (
	"context"
	"encoding/json"

	"go.uber.org/zap"
	"courseSign/server/model/twi"

	"courseSign/server/global"
	"courseSign/server/model/system"
	"courseSign/server/utils"
)

type JwtService struct{}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: JsonInBlacklist
//@description: 拉黑jwt
//@param: jwtList model.JwtBlacklist
//@return: err error

func (jwtService *JwtService) JsonInBlacklist(jwtList system.JwtBlacklist) (err error) {
	err = global.GVA_DB.Create(&jwtList).Error
	if err != nil {
		return
	}
	global.BlackCache.SetDefault(jwtList.Jwt, struct{}{})
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: IsBlacklist
//@description: 判断JWT是否在黑名单内部
//@param: jwt string
//@return: bool

func (jwtService *JwtService) IsBlacklist(jwt string) bool {
	_, ok := global.BlackCache.Get(jwt)
	return ok
	// err := global.GVA_DB.Where("jwt = ?", jwt).First(&system.JwtBlacklist{}).Error
	// isNotFound := errors.Is(err, gorm.ErrRecordNotFound)
	// return !isNotFound
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetRedisJWT
//@description: 从redis取jwt
//@param: userName string
//@return: redisJWT string, err error

func (jwtService *JwtService) GetRedisJWT(userName string) (redisJWT string, err error) {
	redisJWT, err = global.GVA_REDIS.Get(context.Background(), userName).Result()
	return redisJWT, err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: SetRedisJWT
//@description: jwt存入redis并设置过期时间
//@param: jwt string, userName string
//@return: err error

func (jwtService *JwtService) SetRedisJWT(jwt string, userName string) (err error) {
	// 此处过期时间等于jwt过期时间
	dr, err := utils.ParseDuration(global.GVA_CONFIG.JWT.ExpiresTime)
	if err != nil {
		return err
	}
	timer := dr
	err = global.GVA_REDIS.Set(context.Background(), userName, jwt, timer).Err()
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetOauthInfoJWT
//@description: 从redis取tokem
//@param: userName string
//@return: redisToken string, err error

func (jwtService *JwtService) GetOauthInfoTwi(userName string) (redisToken string, err error) {
	redisToken, err = global.GVA_REDIS.Get(context.Background(), userName).Result()
	return redisToken, err
}

func (jwtService *JwtService) GetOauthInfo(username string) (metamask twi.TwiMetamask, err error) {
	redisToken, err := global.GVA_REDIS.Get(context.Background(), username).Result()
	if err != nil {
		return
	}
	err = json.Unmarshal([]byte(redisToken),&metamask)
	return
}

func (jwtService *JwtService) SetOauthInfo(metamask twi.TwiMetamask, userName string) (err error) {
	userInfo,err := json.Marshal(metamask)
	if err != nil {
		return
	}
	// 此处过期时间等于90天
	dr, err := utils.ParseDuration(global.GVA_CONFIG.Twitter.ExpiresTime)
	if err != nil {
		return
	}
	timer := dr
	err = global.GVA_REDIS.Set(context.Background(), userName, userInfo, timer).Err()
	return
}



//@author: [piexlmax](https://github.com/piexlmax)
//@function: SetOauthInfoJWT
//@description: oauthinfo存入redis并设置过期时间
//@param: userInfo []byte, userName string
//@return: err error

func (jwtService *JwtService) SetOauthInfoTwi(userInfo []byte, userName string) (err error) {
	// 此处过期时间等于90天
	dr, err := utils.ParseDuration(global.GVA_CONFIG.Twitter.ExpiresTime)
	if err != nil {
		return err
	}
	timer := dr
	err = global.GVA_REDIS.Set(context.Background(), userName, userInfo, timer).Err()
	return err
}


func (jwtService *JwtService) SetReqTokenInfoTwi(userInfo []byte, userName string) (err error) {
	// 此处过期时间等于90天 回调成功删除
	dr, err := utils.ParseDuration(global.GVA_CONFIG.Twitter.ExpiresTime)
	if err != nil {
		return err
	}
	timer := dr
	err = global.GVA_REDIS.Set(context.Background(), userName, userInfo, timer).Err()
	return err
}

func (jwtService *JwtService) GetReqTokenInfoTwi(userName string) (redisToken string, err error) {
	redisToken, err = global.GVA_REDIS.Get(context.Background(), userName).Result()
	return redisToken, err
}

func LoadAll() {
	var data []string
	err := global.GVA_DB.Model(&system.JwtBlacklist{}).Select("jwt").Find(&data).Error
	if err != nil {
		global.GVA_LOG.Error("加载数据库jwt黑名单失败!", zap.Error(err))
		return
	}
	for i := 0; i < len(data); i++ {
		global.BlackCache.SetDefault(data[i], struct{}{})
	} // jwt黑名单 加入 BlackCache 中
}
