package router

import (
	"fonchain-fiee/pkg/middleware"
	"fonchain-fiee/pkg/service"
	"fonchain-fiee/pkg/service/account"
	"fonchain-fiee/pkg/service/auth"
	"fonchain-fiee/pkg/service/lang"
	"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()

	r.Use(gzip.Gzip(gzip.BestSpeed)) // 中间件占用绝大部分内存
	//加入日志中间件，跨域中间件
	r.Use(middleware.NewLogger(), middleware.Cors(), middleware.GinRecovery(true))
	privateGroup := r.Group("api")
	privateGroup.Use(middleware.NewLogger(), middleware.Cors(), middleware.GinRecovery(true))
	//加入日志中间件，跨域中间件
	v1 := r.Group("api")
	GiteaRoute(v1)

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

	BundleRouter(privateGroup)
	BundleOrderRouter(privateGroup)
	ValueAddBundleRouter(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.AccountProvider))
		{
			acRoute.POST("real-name", account.RealName) //实名
			acRoute.POST("info", account.UserInfo)      //用户详情
			acRoute.POST("update", account.UserUpdate)  //用户更新
		}
		{
			acRoute.POST("list", account.UserList)         //用户列表
			acRoute.POST("approval", account.UserApproval) //实名审核
		}
	}
	// 上传
	upData := privateGroup.Group("upload")
	upData.Use(middleware.CheckLogin(service.AccountProvider))
	{
		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)
	}

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