package utils

import (
	"crypto/rsa"
	"database/sql"
	_ "github.com/go-sql-driver/mysql"
	"github.com/gorilla/websocket"
	"github.com/redis/go-redis/v9"
	"sync"
)

const (
	HttpOk       = 200
	HttpAuthFail = 403
	HttpNotFound = 404
	HttpError    = 500

	FullPatten      = "QWERTYUIOPLKJHGFDSAZXCVBNM0123456789zxcvbnmlkjhgfdsaqwertyuiop-_"
	NumberPatten    = "0123456789"
	AlphaPatten     = "QWERTYUIOPLKJHGFDSAZXCVBNMzxcvbnmlkjhgfdsaqwertyuiop"
	AlphaNumPatten  = "QWERTYUIOPLKJHGFDSAZXCVBNM0123456789zxcvbnmlkjhgfdsaqwertyuiop"
	UppercasePatten = "QWERTYUIOPLKJHGFDSAZXCVBNM"
	LowercasePatten = "zxcvbnmlkjhgfdsaqwertyuiop"

	RedisNil = redis.Nil
)

type WsMsg struct {
	Event string `json:"event"`
	Data  any    `json:"data"`
}

type Response struct {
	Status bool   `json:"status"`
	Msg    string `json:"msg"`
	Data   any    `json:"data"`
}

type JsonType = map[string]any

type Config struct {
	Release    bool   `json:"release"`
	ServerAddr string `json:"server_addr"`
	TimeFormat string `json:"time_format"`

	MysqlHost     string `json:"mysql_host"`
	MysqlPort     int    `json:"mysql_port"`
	MysqlUser     string `json:"mysql_user"`
	MysqlPass     string `json:"mysql_pass"`
	MysqlDatabase string `json:"mysql_database"`

	RedisHost     string `json:"redis_host"`
	RedisPort     int    `json:"redis_port"`
	RedisPass     string `json:"redis_pass"`
	RedisDatabase int    `json:"redis_database"`

	WxMerchantAcc string `json:"wx_merchant_acc"`
	WxApiCertSeq  string `json:"wx_api_cert_seq"`
	WxApiCertPath string `json:"wx_api_cert_path"`
	WxApiV3Key    string `json:"wx_api_v3_key"`
}

// database
var (
	Redis        *redis.Client
	Mysql        *sql.DB
)

// de-encrypt
var (
	ClientPri string
	ClientPub string
	ServerPri *rsa.PrivateKey
	ServerPub *rsa.PublicKey
)

// wechat pay
var (
	WxMerchantAcc string
	WxApiCertSeq  string
	WxApiCertPath string
	WxApiV3Key    string
)

// websocket
var (
	UpgradeHttp2Ws = websocket.Upgrader{
		ReadBufferSize:  1024,
		WriteBufferSize: 1024,
		CheckOrigin:     CheckOrigin,
	}
	SellerWss   = make(map[string]*websocket.Conn)
	SellerLock  sync.Mutex
	ManagerWss  = make(map[string]*websocket.Conn)
	ManagerLock sync.Mutex
	WorkerWss   = make(map[string]*websocket.Conn)
	WorkerLock  sync.Mutex
)
