package router

import (
	"fonchain-fiee/pkg/middleware"
	"fonchain-fiee/pkg/service"
	"fonchain-fiee/pkg/service/account"
	"fonchain-fiee/pkg/service/asChat"
	"fonchain-fiee/pkg/service/auth"
	"fonchain-fiee/pkg/service/file"
	"fonchain-fiee/pkg/service/governance"
	"fonchain-fiee/pkg/service/lang"
	"fonchain-fiee/pkg/service/pressreleases"
	"fonchain-fiee/pkg/service/qr"
	"fonchain-fiee/pkg/service/redirect"
	"fonchain-fiee/pkg/service/upload"
	"fonchain-fiee/pkg/service/version"
	"net/http"

	"github.com/gin-contrib/gzip"

	"github.com/gin-contrib/pprof"
	"github.com/gin-gonic/gin"
)

// 路由配置
func NewRouter() *gin.Engine {
	//使用默认gin路由
	r := gin.Default()
	wsGroup := r.Group("api/fiee")
	wsGroup.Use(
		middleware.GinRecovery(true),
	)
	r.Use(gzip.Gzip(gzip.BestSpeed)) // 中间件占用绝大部分内存
	//加入日志中间件，跨域中间件
	r.Use(middleware.NewLogger(), middleware.Cors(), middleware.GinRecovery(true))
	privateGroup := r.Group("api/fiee")
	privateGroup.Use(middleware.NewLogger(), middleware.Cors(), middleware.GinRecovery(true))
	//加入日志中间件，跨域中间件
	v1 := r.Group("api/fiee")
	GiteaRoute(v1)

	v1.GET("test", func(c *gin.Context) {
		c.String(200, "你好")
		// 或者使用 c.Writer.WriteString("你好")
		return
	})

	BundleRouter(privateGroup)
	BundleOrderRouter(privateGroup)
	ValueAddBundleRouter(privateGroup)
	MediaRouter(privateGroup)

	{
		v1.POST("version", version.Version) //版本号公共
	}
	//账号模块
	{
		privateGroup.POST("user/register", account.UserRegister)       //h5注册登录
		privateGroup.POST("user/login", account.UserLogin)             //后台登录
		privateGroup.POST("user/send", account.SendMsg)                //发送验证码
		privateGroup.POST("user/logout", account.UserLogout)           //登出
		privateGroup.POST("user/check/msg", account.CheckMsg)          //校验验证码
		privateGroup.POST("generate/captcha", account.GenerateCaptcha) //生成滑块验证码
		privateGroup.POST("validate/captcha", account.ValidateCaptcha) //验证滑块验证码
		acRoute := privateGroup.Group("/user")
		acRoute.Use(middleware.CheckLogin(service.AccountFieeProvider))
		{
			acRoute.POST("real-name", account.RealName) //实名
			acRoute.POST("info", account.UserInfo)      //用户详情
			acRoute.POST("update", account.UserUpdate)  //用户更新
		}
		webAcRouter := privateGroup.Group("/user")
		//webAcRouter.Use(middleware.CheckWebLogin(service.AccountProvider))
		webAcRouter.Use(middleware.CheckLogin(service.AccountFieeProvider))
		{
			webAcRouter.POST("list", account.UserList)         //用户列表
			webAcRouter.POST("approval", account.UserApproval) //实名审核
		}
	}
	{
		v1.GET("chart/forward/test", redirect.ForwardTest)
	}
	// 上传
	upData := privateGroup.Group("upload")
	upData.Use(middleware.CheckLogin(service.AccountFieeProvider))
	{
		upData.POST("img", upload.UploadImg)
	}
	{
		//图片相关
		imageRoute := v1.Group("image")
		imageRoute.POST("upload", qr.Upload)
		imageRoute.GET("qr/url", qr.CreateQr)
		imageRoute.GET("qr/redirect/url", qr.CreateRedirectQr)
		imageRoute.GET("qr/url/base64", qr.CreateQrBase64)
		imageRoute.GET("qr/redirect/url/base64", qr.CreateRedirectQrBase64)
	}

	{
		//跳转相关
		redirectRoute := v1.Group("redirect")
		redirectRoute.GET("url", redirect.ToRedirectRealUrl)
		redirectRoute.GET("url/rand", redirect.ToRedirectRealUrlAdnRand) //重定向代理
	}

	{
		translateRoute := v1.Group("msg/translate")
		translateRoute.POST("cn", lang.ChangeLandMsg) //中文提示翻译成指定语言，自动化翻译
	}

	{
		//图片相关
		redirectRoute := v1.Group("tt")
		redirectRoute.POST("down", auth.DownImg)
		redirectRoute.POST("sdk/down/v2", auth.DownImgV2)
		redirectRoute.POST("sdk/down/v3", auth.DownImgV3)
	}
	//========================================================================================
	//                                    客户聊天
	{
		// websocket数据接收
		wsGroup.GET("aschat/ws", asChat.ChatHandlerIns.Connection)
		v1.POST("aschat/message/new", asChat.ChatHandlerIns.NewMessage)
		v1.POST("aschat/media/upload", asChat.ChatHandlerIns.Upload)
		v1.POST("aschat/message/list", asChat.ChatHandlerIns.MessageList)
		v1.POST("aschat/user/stat", asChat.ChatHandlerIns.UserMessageStat)
		v1.POST("aschat/voicetotext", asChat.ChatHandlerIns.VoiceToText)
		v1.POST("aschat/artistDetail", asChat.ChatHandlerIns.ArtistDetail)
	}

	{
		// 素材库
		resourceRoute := v1.Group("/resource")

		resourceRoute.GET("", file.Info)
		resourceRoute.DELETE("", file.Delete)
		resourceRoute.PUT("", file.Action)
		resourceRoute.POST("", file.Create)
		resourceRoute.GET("/search", file.Search)
		resourceRoute.POST("/upload", file.Upload)
		resourceRoute.POST("/tus/create", file.TusCreate)
		resourceRoute.POST("/tus/upload", file.TusUpload)
		resourceRoute.GET("/raw", file.Raw)
		resourceRoute.GET("/raw/dir", file.DirDownload)
		resourceRoute.GET("/preview", file.Preview)
		resourceRoute.GET("/list", file.List)
	}

	{
		// 官网文档
		governanceRoute := v1.Group("/governance")
		governanceRouteLogin := governanceRoute.Group("", middleware.CheckWebLogin(service.AccountProvider))

		governanceRoute.GET("/display", governance.Display)
		governanceRouteLogin.POST("/list", governance.List)
		governanceRouteLogin.POST("", governance.Create)
		governanceRouteLogin.POST("/edit", governance.Edit)
		governanceRouteLogin.POST("/delete", governance.Delete)

	}
	{
		// 官网新闻
		pressreleasesRoute := v1.Group("/pressreleases")
		pressreleasesRouteLogin := pressreleasesRoute.Group("", middleware.CheckWebLogin(service.AccountProvider))

		pressreleasesRoute.GET("/display", pressreleases.Display)
		pressreleasesRoute.GET("", pressreleases.Get)
		pressreleasesRouteLogin.POST("/list", pressreleases.List)
		pressreleasesRouteLogin.POST("", pressreleases.Create)
		pressreleasesRouteLogin.POST("/edit", pressreleases.Edit)
		pressreleasesRouteLogin.POST("/delete", pressreleases.Delete)

	}
	//静态文件
	r.StaticFS("/api/static", http.Dir("./runtime"))
	r.NoRoute(func(c *gin.Context) {
		c.JSON(http.StatusNotFound, gin.H{
			"status": 1,
			"msg":    "不存在的路由",
		})
	})
	pprof.Register(r)
	return r
}
