package utils

import (
	"fmt"
	"strconv"

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

var (
	RunMode        string //运行模式
	MYSQL_URL      string //数据库连接
	MYSQL_URL_DATA string
	PYTHON_PATH    string // python可执行文件地址
)

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

var BusinessCode string //商户号

// 卓创资讯
var (
	SCI99_COOKIE_PATH string
	IS_INIT_SCI99     string
)

// 粮油商务网
var (
	LY_USERNAME  string
	LY_PASSWORD  string
	LY_JSON_PATH string
	LY_OPEN      string
)

var (
	EDB_LIB_URL         string
	APP_EDB_LIB_NAME_EN string
	EDB_LIB_Md5_KEY     string
)

var (
	OLD_EXCEL_PATH_JR string
)

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

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

	// 商家编码
	BusinessCode = config["business_code"]
	EDB_LIB_URL = config["edb_lib_url"]
	APP_EDB_LIB_NAME_EN = config["app_edb_lib_name_en"]
	EDB_LIB_Md5_KEY = config["edb_lib_md5_key"]

	if BusinessCode == BusinessCodeRelease {
		MYSQL_URL = config["mysql_url"]
		MYSQL_URL_DATA = config["mysql_url_data"]
	}

	if BusinessCode == BusinessCodeFuBang {
		SCI99_COOKIE_PATH = config["sci99_cookie_path"]
		IS_INIT_SCI99 = config["is_init_sci99"]
		EDB_LIB_URL = config["edb_lib_url"]
		APP_EDB_LIB_NAME_EN = config["app_edb_lib_name_en"]
		EDB_LIB_Md5_KEY = config["edb_lib_md5_key"]
	}
	if BusinessCode == BusinessCodeJinRui {
		APP_EDB_LIB_NAME_EN = config["app_edb_lib_name_en"]
		EDB_LIB_Md5_KEY = config["edb_lib_md5_key"]
		EDB_LIB_URL = config["edb_lib_url"]
		OLD_EXCEL_PATH_JR = config["old_excel_path_jr"]
	}

	if RunMode == "release" {

	} else {

	}
	//日志配置
	{
		LogPath = config["log_path"]
		LogFile = config["log_file"]
		BinLogPath = config["binlog_path"]
		BinLogFile = config["binlog_file"]
		ApiLogPath = config["apilog_path"]
		ApiLogFile = config["apilog_file"]
		logMaxDaysStr := config["log_max_day"]
		LogMaxDays, _ = strconv.Atoi(logMaxDaysStr)
	}

	PYTHON_PATH = config["python_path"]
	if PYTHON_PATH == "" {
		PYTHON_PATH = "python3"
	}

	{
		LY_USERNAME = config["ly_username"]
		LY_PASSWORD = config["ly_password"]
		LY_JSON_PATH = config["ly_json_path"]
		LY_OPEN = config["ly_open"]

	}
}