package domain

import (
	"errors"
	"fmt"
	"github.com/fonchain_enterprise/micro-account/pkg/cache"
	"github.com/fonchain_enterprise/micro-account/pkg/common/redis_key"
	"github.com/fonchain_enterprise/micro-account/pkg/m"
	"time"
)

func CheckMsg(key string) error {

	n, err := cache.RedisClient.Exists(key).Result()
	fmt.Println("n", n)
	if err != nil {
		return err
	}

	if n == 0 {
		return nil
	}

	str := cache.RedisClient.Get(key)

	num, err := str.Int()

	if err != nil {
		return err
	}

	if num > 30 {
		return errors.New(m.Mobile_Send_Over)
	}

	return nil
}

// CheckMsgPre  检测短信发送限制
func CheckMsgPre(domain string, telNum string, scope string) error {

	key := redis_key.GetAccountKeyCountTodayHour(domain, telNum, scope)

	//一分钟最多发一条
	minStr := cache.RedisClient.Get(redis_key.MinLimit(domain, telNum, scope))

	if minStr.Val() != "" {
		return errors.New(m.Mobile_Sended)
	}

	//一个小时最多10次
	n, err := cache.RedisClient.Exists(key).Result()
	fmt.Println("n", n)
	if err != nil {
		return err
	}

	if n == 0 {
		return nil
	}

	str := cache.RedisClient.Get(key)

	num, err := str.Int()

	if err != nil {
		return err
	}

	if num > 10 {
		return errors.New(m.Mobile_Send_Over)
	}

	return nil
}

// SetOnlyRecordTelMsgCode 记录验证码
func SetOnlyRecordTelMsgCode(domain, tel, code, scope string) error {

	//发送验证码
	if merr := cache.RedisClient.Set(redis_key.GetOnlyAccountKey(domain, tel), code, 15*60*1000*time.Millisecond).Err(); merr != nil {
		return merr
	}

	//发送验证码
	if merr := cache.RedisClient.Set(redis_key.MinLimit(domain, tel, scope), code, 1*60*1000*time.Millisecond).Err(); merr != nil {
		return merr
	}

	return SetTodayNum(domain, tel)
}

// InBlockList 黑名单
func InBlockList(domain string, tel string) bool {

	isExist, err := cache.RedisClient.HExists(redis_key.BlackListKey(domain), tel).Result() //发送注册验证码 校验验证码时间限制

	if err != nil {
		panic(err)
		return false
	}

	if isExist == true {
		fmt.Println("黑名单")
		return true
	}

	fmt.Println("白名单")

	return false
}

// CodeLive 发送的验证码是否活着
func CodeLive(domain string, tel string) error {

	str := cache.RedisClient.Get(redis_key.VerityCode(domain, tel)) //发送注册验证码 校验验证码时间限制

	if str.Val() != "" {
		return errors.New(m.Mobile_Sended)
	}

	return nil
}

// SetRecordTelMsgCode 发送短信成功之后记录部分信息
func SetRecordTelMsgCode(domain string, tel string, code string) error {

	//发送验证码
	if merr := cache.RedisClient.Set(redis_key.GetAccountKey(domain, tel), code, 15*60*1000*time.Millisecond).Err(); merr != nil {
		return merr
	}

	//发送验证码
	if merr := cache.RedisClient.Set(redis_key.VerityCode(domain, tel), code, 55*1000*time.Millisecond).Err(); merr != nil {
		return merr
	}

	//注册的时候验证码
	if merr := cache.RedisClient.Set(redis_key.GetAccountRegisterKey(domain, tel), code, 60*60*1000*time.Millisecond).Err(); merr != nil {
		return merr
	}

	return SetTodayNum(domain, tel)
}

// SetRecordTelMsgCodeLogin 登录之后发送的短信
func SetRecordTelMsgCodeLogin(domain string, tel string, code string, id string) error {

	//一分钟之内的
	fmt.Println(redis_key.GetOneMinuteKey(domain, tel, id))
	if merr := cache.RedisClient.Set(redis_key.GetOneMinuteKey(domain, tel, id), code, 1*60*1000*time.Millisecond).Err(); merr != nil {
		return merr
	}

	//15分钟之内的
	if merr := cache.RedisClient.Set(redis_key.GetChangeTelKey(domain, tel, id), code, 15*60*1000*time.Millisecond).Err(); merr != nil {
		return merr
	}

	if merr := cache.RedisClient.Set(redis_key.GetNowNewTelById(domain, id), tel, 24*60*60*1000*time.Millisecond).Err(); merr != nil {
		return merr
	}

	return SetTodayNum(domain, tel)
}

// DelRecordTelMsgCodeLogin 登录之后发送的短信
func DelRecordTelMsgCodeLogin(domain string, tel string, id string) error {

	//15分钟之内的
	if merr := cache.RedisClient.Del(redis_key.GetChangeTelKey(domain, tel, id), redis_key.GetOneMinuteKey(domain, tel, id)).Err(); merr != nil {
		return merr
	}

	return SetTodayNum(domain, tel)
}

// SetTodayNum 当日记录发送记录
func SetTodayNum(domain string, tel string) error {
	//当日记录发送记录
	telTodayNum := redis_key.GetAccountKeyCountToday(domain, tel)
	cache.RedisClient.Incr(telTodayNum)
	cache.RedisClient.Expire(telTodayNum, 24*60*60*1000*time.Millisecond)

	return nil
}

func CheckRegisterCode(domain string, tel string, code string) error {

	str := cache.RedisClient.Get(redis_key.GetAccountRegisterKey(domain, tel))

	cacheCode := str.Val()

	if cacheCode == "" {
		return errors.New("没有发送数据")
	}

	if code != cacheCode {
		return errors.New("验证码错误")
	}

	return nil
}
