package middleware

import (
	"bytes"
	"dsbqj-admin/pkg/serializer"
	"dsbqj-admin/pkg/util"
	"github.com/gin-gonic/gin"
	"github.com/goccy/go-json"
	"io/ioutil"
	"os"
)

// CurrentUser 获取登录用户
//func CurrentUser() gin.HandlerFunc {
//	return func(c *gin.Context) {
//		session := sessions.Default(c)
//		uid := session.Get("user_id")
//		if uid != nil {
//			user, err := model.GetUser(uid)
//			if err == nil {
//				c.Set("user", &user)
//			}
//		}
//		c.Next()
//	}
//}

// CurrentAdmin 获取登录用户
//func CurrentAdmin() gin.HandlerFunc {
//	return func(c *gin.Context) {
//		session := sessions.Default(c)
//		uid := session.Get("admin_id")
//		if uid != nil {
//			admin, err := model.GetAdmin(uid)
//			if err == nil {
//				c.Set("admin", &admin)
//			}
//		}
//		c.Next()
//	}
//}

// AuthRequired 需要登录
func AuthRequired() gin.HandlerFunc {
	return func(c *gin.Context) {
		var loginToken util.Token
		token := c.GetHeader("Token")
		info := util.ParseToken(token, os.Getenv("TOKEN_SECRET"))
		if len(info) == 0 {
			c.JSON(200, serializer.Response{
				Code: 403,
				Msg:  "token解析失败",
			})
			c.Abort()
			return
		}
		json.Unmarshal(info, &loginToken)

		if loginToken.Status == "active" || loginToken.Status == "host" {
			if util.GetNowSecond()-loginToken.ExpiresIn < 24*60*60*1000 {
				c.Set("user", &loginToken)
				c.Next()
				return
			}
		} else {
			c.JSON(200, serializer.Response{
				Code: 402,
				Msg:  "需要完善数据",
			})
			c.Abort()
			return
		}

		c.JSON(200, serializer.Response{
			Code: 401,
			Msg:  "需要登录",
		})
		c.Abort()
	}
}

func AdminRequired() gin.HandlerFunc {
	return func(c *gin.Context) {
		var loginToken util.Token
		token := c.GetHeader("Token")
		info := util.ParseToken(token, os.Getenv("TOKEN_SECRET"))
		if len(info) == 0 {
			c.JSON(200, serializer.Response{
				Code: 403,
				Msg:  "token解析失败",
			})
			c.Abort()
			return
		}
		json.Unmarshal(info, &loginToken)

		if util.GetNowSecond()-loginToken.ExpiresIn < 24*60*60*1000 {
			c.Set("user", &loginToken)
			c.Next()
			return
		}

		c.JSON(200, serializer.Response{
			Code: 401,
			Msg:  "需要登录",
		})
		c.Abort()
	}
}

func WebRequired() gin.HandlerFunc {
	return func(c *gin.Context) {
		var loginToken util.Token
		token := c.GetHeader("Token")
		if token == "DCciDkTCPwFxTYxFzdgxYoOQVXJesKwQccNddefRMmYUfXnXcgQdPaAHcVJWENJHBh" { // 管理平台发送的请求 特殊处理
			loginToken.Role = "web"
			c.Set("user", &loginToken)
			c.Next()
			return
		}

		c.JSON(200, serializer.Response{
			Code: 401,
			Msg:  "没有请求权限",
		})
		c.Abort()
	}
}

func BodyHandler() gin.HandlerFunc {
	return func(c *gin.Context) {
		payload, err := c.GetRawData()
		if err != nil {
			c.JSON(200, serializer.Response{
				Code: 501,
				Msg:  "数据读取错误",
			})
			c.Abort()
			return
		}

		//todo test use it, need delete it after test
		// payloadstr := util.EncryptDES_ECB(payload, os.Getenv("CRYPRO_SECRET"))
		// payload = []byte(payloadstr)

		if len(payload) != 0 {
			res, err := util.DecryptDES_ECB(string(payload), os.Getenv("CRYPRO_SECRET"))
			if err != nil {
				c.JSON(200, serializer.Response{
					Code: 501,
					Msg:  "数据解析错误",
				})
				c.Abort()
				return
			}
			j := map[string]interface{}{}
			json.Unmarshal([]byte(res), &j)
			buf, _ := json.Marshal(j)
			c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(buf))
		}

		c.Next()
	}
}
