package main

import (
	"WorldEpcho/src/config"
	"WorldEpcho/src/datasource"
	"WorldEpcho/src/https_auth"
	"WorldEpcho/src/routers"
	"WorldEpcho/src/service"
	"WorldEpcho/src/utils"
	"fmt"
	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

func main() {
	//时区设置
	loc, _ := time.LoadLocation("Asia/Shanghai")
	time.Local = loc

	config.InitConfig()
	//全局随机种子
	config.RandInit()
	datasource.InitMysql()
	// 同步结构体与数据库表
	/*	err := datasource.Engine.Sync2(new(models.WorldDeductionResult))
		if err != nil {
			log.Fatal("创建表失败:", err)
		}*/

	//生成一个9位的邀请码
	//println("生成一个9位的邀请码: ", utils.GenerateInviteCode(1))
	//info, _ := models.GetConfigDataByAppInfo(2, "心理咨询")
	//fmt.Println("config_data: ", info)
	fmt.Println("time.Now()  >>>>>>>>>>>>>>>>>>>>>>>>> ", time.Now().Unix())
	timeString := utils.GetNormalTimeString(time.Now())
	fmt.Println(timeString)

	//启动聊天协程监听连接
	go service.Manager.WebSocketStart()
	// 启动多数字人聊天协程监听连接
	go service.DpsManager.DpsWebSocketStart()
	// 启动世界推演
	go service.WorldManager.WorldWebSocketStart()
	router := routers.InitRouter()
	/*
	   设置打印gin日志的格式
	*/
	router.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
		// 自定义日志格式
		return fmt.Sprintf("[GIN] %s | %3d | %13v | %15s | %-7s %#v\n",
			param.TimeStamp.Format("2006/01/02 - 15:04:05"),
			param.StatusCode,
			param.Latency,
			param.ClientIP,
			param.Method,
			param.Path,
		)
	}))

	// 配置 CORS 中间件
	router.Use(cors.New(cors.Config{
		AllowOrigins:     []string{"*"},                                                // 允许访问的域名，可以设置为 "*"
		AllowMethods:     []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"}, // 允许的HTTP方法
		AllowHeaders:     []string{"Origin", "Content-Type"},                           // 允许的Header
		ExposeHeaders:    []string{"Content-Length"},                                   // 允许客户端访问的Header
		AllowCredentials: true,                                                         // 允许带上Cookie
		MaxAge:           12 * time.Hour,                                               // 缓存时间
	}))

	//configData, _ := models.GetWorldConfigDataByID(4)
	//fmt.Println("configData => ", configData)
	//world, _, _ := models.FindWorldsByTagAndName("业务经理考核系统", "业务经理考核")
	//fmt.Println(" world =>", world)
	//token, exp, _ := service.GetWeixinToken()
	//fmt.Println(" ======= token: ===== ", token, "过期时间: ", exp)
	/*
		    code := utils.GenerateActivationCode(21)
			fmt.Println("code >>>>> ", code)
	*/
	//var dpIdsarr []int64 = []int64{1, 4}
	//conversation, err := models.QueryConversationsByUserIdAndDpId(1, dpIdsarr, 0)
	//fmt.Println("conversation: ", conversation)

	/*user, uinfo, err := models.FindUserWithStudentID("1001201409260346")
	fmt.Println("user: ", user, "user_info: ", uinfo)*/
	//data, err := models.GetUserApplicationData(6, 46)
	//fmt.Println("data: ", data)

	/*
		    number, err := service.GetWechatUserPhoneNumber("f7454a77eab517352261f4755800613c507d9e0421eb9bd0f9d6042594e60d54")
			fmt.Println(" ====== phoneInfo ===", number)
			fmt.Println(" ====== phone  number ===", number.PhoneNumber)
	*/

	// 中间件对应的包：github.com/unrolled/secure

	//_, code, _ := models.GetActivationCode("migutest3")
	//fmt.Println("code => ", code)

	//service.ExampleSend()

	router.GET("/https_test", func(c *gin.Context) {
		fmt.Println(c.Request.Host)
		c.JSON(http.StatusOK, gin.H{
			"code":   http.StatusOK,
			"result": "测试成功",
		})
	})
	router.Use(https_auth.HttpsHandler())                                                  //https对应的中间件
	path := "https_auth/CA/"                                                               //证书的路径
	router.RunTLS(":8099", path+"admin.mindepoch.com.pem", path+"admin.mindepoch.com.key") //开启HTTPS服务

	//初始化redis配置
	//utils.InitRedisStore()
	//Run("里面不指定端口号默认为8080")
	//router.Run(":8099")

}
