package initialize

import (
	_ "gin-vue-admin/docs"
	"gin-vue-admin/global"
	"gin-vue-admin/middleware"
	"gin-vue-admin/router"
	"net/http"

	"github.com/gin-gonic/gin"
	ginSwagger "github.com/swaggo/gin-swagger"
	"github.com/swaggo/gin-swagger/swaggerFiles"
)

// 初始化总路由

func Routers() *gin.Engine {
	var Router = gin.Default()
	Router.StaticFS(global.GVA_CONFIG.Local.Path, http.Dir(global.GVA_CONFIG.Local.Path)) // 为用户头像和文件提供静态地址
	// Router.Use(middleware.LoadTls())  // 打开就能玩https了
	global.GVA_LOG.Info("use middleware logger")
	// 跨域
	//Router.Use(middleware.Cors()) // 如需跨域可以打开
	global.GVA_LOG.Info("use middleware cors")
	Router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
	global.GVA_LOG.Info("register swagger handler")
	//异常捕获并记录
	Router.Use(middleware.GinRecovery(false))
	LoginGroup := Router.Group("")
	LoginGroup.Use()
	{
		router.InitBaseRouter(LoginGroup)            // 注册基础功能路由 不做鉴权
		router.InitMobileUserLoginRouter(LoginGroup) // 注册基础功能路由 不做鉴权
	}
	// 方便统一添加路由组前缀 多服务器上线使用
	PublicGroup := Router.Group("")
	PublicGroup.Use(middleware.NoJWTAuth())
	{
		//router.InitBaseRouter(PublicGroup) // 注册基础功能路由 不做鉴权
		router.InitMobileUserPublicRouter(PublicGroup)
		router.InitVipPublicRouter(PublicGroup)
		router.InitPointsPublicRouter(PublicGroup)
		router.InitCommonPublicRouter(PublicGroup)
		router.InitSurveyPublicRouter(PublicGroup)
		router.InitCustoemrUserPublicRouter(PublicGroup)
	}

	AdminPublicGroup := Router.Group("")
	AdminPublicGroup.Use(middleware.NoJWTAuth())
	{
		router.InitAdminUserPublicRouter(AdminPublicGroup)
		router.InitAdminVipPublicRouter(AdminPublicGroup)
	}
	MobileGroup := Router.Group("")
	MobileGroup.Use(middleware.JWTAuth()).Use(middleware.Authe())
	{
		router.InitVipRouter(MobileGroup)
		router.InitPointsRouter(MobileGroup)
		router.InitSurveyRouter(MobileGroup)
		router.InitCustoemrUserRouter(MobileGroup)
	}
	PrivateGroup := Router.Group("")
	PrivateGroup.Use(middleware.JWTAuth())
	{ //.Use(middleware.CasbinHandler())
		router.InitApiRouter(PrivateGroup)                   // 注册功能api路由
		router.InitJwtRouter(PrivateGroup)                   // jwt相关路由
		router.InitUserRouter(PrivateGroup)                  // 注册用户路由
		router.InitMenuRouter(PrivateGroup)                  // 注册menu路由
		router.InitEmailRouter(PrivateGroup)                 // 邮件相关路由
		router.InitSystemRouter(PrivateGroup)                // system相关路由
		router.InitCasbinRouter(PrivateGroup)                // 权限相关路由
		router.InitCustomerRouter(PrivateGroup)              // 客户路由
		router.InitAutoCodeRouter(PrivateGroup)              // 创建自动化代码
		router.InitAuthorityRouter(PrivateGroup)             // 注册角色路由
		router.InitSimpleUploaderRouter(PrivateGroup)        // 断点续传（插件版）
		router.InitSysDictionaryRouter(PrivateGroup)         // 字典管理
		router.InitSysOperationRecordRouter(PrivateGroup)    // 操作记录
		router.InitSysDictionaryDetailRouter(PrivateGroup)   // 字典详情管理
		router.InitFileUploadAndDownloadRouter(PrivateGroup) // 文件上传下载功能路由
		router.InitExcelRouter(PrivateGroup)                 // 表格导入导出
		router.InitMobileUserPrivateRouter(PrivateGroup)     // 需要token
		// Code generated by gin-vue-admin Begin; DO NOT EDIT.
		// Code generated by gin-vue-admin End; DO NOT EDIT.
	}

	AdminPrivateGroup := Router.Group("")
	AdminPrivateGroup.Use(middleware.JWTAuth())
	{
		router.InitAdminUserPrivateRouter(AdminPrivateGroup) // 用户
		router.InitAdminVipPrivateRouter(AdminPrivateGroup)  // vip
	}
	global.GVA_LOG.Info("router register success")
	return Router
}
