package routers

import (
	"github.com/gin-gonic/gin"
	_ "chain33-pai/docs"
	"chain33-pai/routers/api/v1"
	"net/http"
	"reflect"
	"strconv"
	"gopkg.in/go-playground/validator.v8"
	"github.com/gin-gonic/gin/binding"
	"chain33-pai/pkg/logging"
)

// InitRouter initialize routing information
func InitRouter() *gin.Engine {
	r := gin.Default()
	//Custom Validators
	if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
		err := v.RegisterValidation("LegalNumber", CheckNumber)
		if err != nil {
			logging.Error("gin custom validator init error, field:LegalNumber")
		}
	}
	r.Use(gin.Logger())
	r.Use(gin.Recovery())
	r.Use(Cors())
	//r.StaticFS("/export", http.Dir(export.GetExcelFullPath()))
	//r.StaticFS("/upload/images", http.Dir(upload.GetImageFullPath()))
	//r.StaticFS("/qrcode", http.Dir(qrcode.GetQrCodeFullPath()))

	//r.GET("/auth", api.GetAuth)
	//r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
	//r.POST("/upload", api.UploadImage)
	apiv1 := r.Group("/pai")
	//获取树莓派基本信息
	//apiv1.Any("",Cors())
	apiv1.POST("/ticketinfo",v1.GetTicketInfo)
	apiv1.POST("/minetime",v1.RecordMineTime)
	//apiv1.Use(jwt.JWT())
	//{
	//
	//	//获取标签列表
	//	apiv1.GET("/tags", v1.GetTags)
	//	//新建标签
	//	apiv1.POST("/tags", v1.AddTag)
	//	//更新指定标签
	//	apiv1.PUT("/tags/:id", v1.EditTag)
	//	//删除指定标签
	//	apiv1.DELETE("/tags/:id", v1.DeleteTag)
	//	//导出标签
	//	r.POST("/tags/export", v1.ExportTag)
	//	//导入标签
	//	r.POST("/tags/import", v1.ImportTag)
	//
	//	//获取文章列表
	//	apiv1.GET("/articles", v1.GetArticles)
	//	//获取指定文章
	//	apiv1.GET("/articles/:id", v1.GetArticle)
	//	//新建文章
	//	apiv1.POST("/articles", v1.AddArticle)
	//	//更新指定文章
	//	apiv1.PUT("/articles/:id", v1.EditArticle)
	//	//删除指定文章
	//	apiv1.DELETE("/articles/:id", v1.DeleteArticle)
	//	//生成文章海报
	//	apiv1.POST("/articles/poster/generate", v1.GenerateArticlePoster)
	//}

	return r
}

// 处理跨域请求,支持options访问
func Cors() gin.HandlerFunc {
	return func(c *gin.Context) {
		method := c.Request.Method
		c.Header("Access-Control-Allow-Origin", "*")
		c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token,Authorization") //Content-Type,AccessToken,X-CSRF-Token,Authorization,Token,FZM-APP-ID
		c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, PATCH, DELETE")
		c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type")
		c.Header("Access-Control-Allow-Credentials", "true")

		// 放行所有OPTIONS方法，因为有的模板是要请求两次的
		if method == "OPTIONS" {
			c.AbortWithStatus(http.StatusNoContent)
		}

		// 处理请求
		c.Next()
	}
}


func CheckNumber(
	v *validator.Validate, topStruct reflect.Value, currentStructOrField reflect.Value,
	field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string,
) bool {
	val := field.Interface()

	switch val.(type) {
	case int:
		return true
	case string:
		if val.(string) == "" {
			return true
		}
		_, err := strconv.ParseInt(val.(string), 10, 64)
		if err != nil {
			return false
		}
		return true
	case int64:
		return true
	default:
		//utility_log.Error("func ToInt error unknow type")
		return false
	}
}
