package utils

import (
	"fmt"

	beeLogger "github.com/beego/bee/v2/logger"
	"github.com/beego/beego/v2/server/web"
)

// 数据库配置
var (
	RunMode          string // 运行模式
	MYSQL_URL_MASTER string // 数据库地址
	MYSQL_URL_RDDP   string // 数据库地址

	REDIS_CACHE string      //缓存地址
	Rc          RedisClient //redis缓存
)
var ObjectStorageClient string // 目前有oss minio，默认oss

// 阿里云配置
var (
	Bucketname      string
	Endpoint        string
	Imghost         string
	UploadDir       string
	AccessKeyId     string
	AccessKeySecret string
)

// MinIo配置
var (
	MinIoBucketname      string
	MinIoEndpoint        string
	MinIoBackEndpoint    string
	MinIoPdfhost         string
	MinIoUploadDir       string
	MinIoAccessKeyId     string
	MinIoAccessKeySecret string
	MinIoUseSSL          string
	MinIoPort            string
	MinIoRegion          string
)

// 阿里云oss前端上传用
var (
	AliStsScheme       string
	RegionId           string
	RoleArn            string
	RoleSessionName    string
	RAMAccessKeyId     string
	RAMAccessKeySecret string
	STSTokenCacheKey   string
)

// 基础配置
var (
	STATIC_DIR        string
	RESOURCE_DIR      string
	UPLOAD_PDF_SIZE   string // 单位MB
	UPLOAD_IMG_SIZE   string
	UPLOAD_AUDIO_SIZE string
	UPLOAD_VIDEO_SIZE string
)

// ES配置
var (
	ES_URL      string // ES服务器地址
	ES_USERNAME string // ES账号
	ES_PASSWORD string // ES密码
	MEDIA_INDEX string
)
var (
	HT_WX_APP_SECRET       string
	HT_WX_APPID            string
	HT_MINI_APPID          string
	TEMPLATE_ID_BY_PRODUCT string
)

// ES索引配置
var (
	MINI_REPORT_INDEX_NAME string // 小程序的pdf报告索引
)

var (
	LogPath    string //调用过程中的日志存放地址
	LogFile    string
	ApiLogPath string // 接口请求地址和接口返回值日志存放地址
	ApiLogFile string
	BinLogPath string // 数据库相关的日志存放地址
	BinLogFile string
	LogMaxDays int // 日志最大保留天数
)

var DesKey string // 接口返回加密KEY

func init() {
	tmpRunMode, err := web.AppConfig.String("run_mode")
	if err != nil {
		panic(any("配置文件读取run_mode错误 " + err.Error()))
	}
	RunMode = tmpRunMode
	fmt.Println("RunMode:", RunMode)
	if RunMode == "" {
		configPath := `/home/code/config/eta_mini_crm/conf/app.conf`
		fmt.Println("configPath:", configPath)
		err = web.LoadAppConfig("ini", configPath)
		if err != nil {
			fmt.Println("web.LoadAppConfig Err:" + err.Error())
		}
		tmpRunMode, _ := web.AppConfig.String("run_mode")
		RunMode = tmpRunMode
	}

	config, err := web.AppConfig.GetSection(RunMode)
	if err != nil {
		panic(any("配置文件读取错误 " + err.Error()))
	}
	beeLogger.Log.Info(RunMode + " 模式")
	MYSQL_URL_RDDP = config["mysql_url_rddp"]
	MYSQL_URL_MASTER = config["mysql_url_master"]

	REDIS_CACHE = config["beego_cache"]
	if len(REDIS_CACHE) <= 0 {
		panic(any("redis链接参数没有配置"))
	}
	// 接口返回加密KEY
	DesKey = config["des_key"]

	// 对象存储客户端
	ObjectStorageClient = config["object_storage_client"]
	if ObjectStorageClient == "" {
		ObjectStorageClient = "oss"
	}

	// OSS相关
	{
		Endpoint = config["endpoint"]
		Bucketname = config["bucket_name"]
		Imghost = config["img_host"]
		UploadDir = config["upload_dir"]
		AccessKeyId = config["access_key_id"]
		AccessKeySecret = config["access_key_secret"]
	}
	// MinIo相关
	{
		MinIoEndpoint = config["minio_endpoint"]
		MinIoBackEndpoint = config["minio_back_endpoint"]
		MinIoBucketname = config["minio_bucket_name"]
		MinIoPdfhost = config["minio_pdf_host"]
		MinIoUploadDir = config["minio_upload_dir"]
		MinIoAccessKeyId = config["minio_access_key_id"]
		MinIoAccessKeySecret = config["minio_access_key_secret"]
		MinIoUseSSL = config["minio_use_ssl"]
		MinIoPort = config["minio_port"]
		MinIoRegion = config["minio_region"]
	}

	// OSS相关（前端使用）
	{
		AliStsScheme = config["ali_sts_scheme"]
		RegionId = config["region_id"]
		RoleArn = config["role_arn"]
		RoleSessionName = config["role_session_name"]
		RAMAccessKeyId = config["ram_access_key_id"]
		RAMAccessKeySecret = config["ram_access_key_secret"]
		STSTokenCacheKey = config["sts_token_cache_key"]
	}

	// ES配置
	{
		ES_URL = config["es_url"]
		ES_USERNAME = config["es_username"]
		ES_PASSWORD = config["es_password"]
	}

	// ES 索引
	{
		MINI_REPORT_INDEX_NAME = config["mini_report_index_name"]
		MEDIA_INDEX = config["media_index"]
	}
	{
		HT_WX_APPID = config["ht_wx_appid"]
		HT_WX_APP_SECRET = config["ht_wx_app_secret"]
		TEMPLATE_ID_BY_PRODUCT = config["template_id_by_product"]
		HT_MINI_APPID = config["ht_mini_appid"]
	}
	// 初始化ES
	initEs()

	// 静态文件目录
	STATIC_DIR = config["static_dir"]
	if STATIC_DIR == "" {
		STATIC_DIR = "./static"
	}
	RESOURCE_DIR = config["resource_dir"]
	if RESOURCE_DIR == "" {
		RESOURCE_DIR = "ht/"
	}
	UPLOAD_PDF_SIZE = config["upload_pdf_size"]
	if UPLOAD_PDF_SIZE == "" {
		UPLOAD_PDF_SIZE = "15"
	}
	UPLOAD_IMG_SIZE = config["upload_img_size"]
	if UPLOAD_IMG_SIZE == "" {
		UPLOAD_IMG_SIZE = "200"
	}
	UPLOAD_AUDIO_SIZE = config["upload_audio_size"]
	if UPLOAD_AUDIO_SIZE == "" {
		UPLOAD_AUDIO_SIZE = "100"
	}
	UPLOAD_VIDEO_SIZE = config["upload_video_size"]
	if UPLOAD_VIDEO_SIZE == "" {
		UPLOAD_VIDEO_SIZE = "1000"
	}
	// 初始化缓存
	redisClient, err := initRedis(config["redis_type"], config["beego_cache"])
	if err != nil {
		fmt.Println("redis链接异常：", err)
		panic(any("redis链接参数没有配置"))
	}

	Rc = redisClient

}
