package api

import (
	"encoding/base64"
	"entrance-grpc/iam"
	"gadmin/config"
	"gadmin/internal/admin/forms"
	"gadmin/internal/admin/service"
	"gadmin/internal/gorm/query"
	"gadmin/utility/serializer"
	"gadmin/utility/token"
	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
)

func UserRegister(c *gin.Context) {
	var req forms.UserRegisterReq
	if err := c.ShouldBind(&req); err == nil {
		res := service.User.Register(req)
		c.JSON(200, res)
	} else {
		c.JSON(200, ErrorResponse(err))
	}
}

//func UserLogin(c *gin.Context) {
//	ip := c.ClientIP()
//	var req forms.UserLoginReq
//	if err := c.ShouldBind(&req); err == nil {
//		res := service.User.Login(req, ip)
//		c.JSON(200, res)
//	} else {
//		c.JSON(200, ErrorResponse(err))
//	}
//}

func UserMe(c *gin.Context) {
	var (
		user = token.CurrentUser(c)
	)
	permissions := make([]forms.UserLoginPermissions, 0)
	permissions = append(permissions, forms.UserLoginPermissions{
		Label: "控制台",
		Value: "value",
	})
	//获取用户角色权限
	//roleInfo, err := service.AdminRole.GetRole(c, forms.AdminRoleReq{ID: user.RoleId})
	//if err != nil || roleInfo == nil {
	//	c.JSON(200, ErrorResponse(err))
	//	return
	//}
	if user.RoleID == 1 {
		for k, _ := range config.AuthMenuMap {
			permissions = append(permissions, forms.UserLoginPermissions{
				Label: config.AuthNameMap[k],
				Value: config.AuthMenuMap[k],
			})
		}
	} else {
		rpdb := query.Use(config.DB).AdminRolePermission
		rolePermission := make([]int, 0)
		err := rpdb.Pluck(rpdb.PermissionID, &rolePermission)
		//err = json.Unmarshal([]byte(roleInfo.Permissions), &rolePermission)
		if err != nil {
			c.JSON(200, ErrorResponse(err))
			return
		}
		for _, v := range rolePermission {
			if _, ok := config.AuthMenuMap[v]; ok {
				permissions = append(permissions, forms.UserLoginPermissions{
					Label: config.AuthNameMap[v],
					Value: config.AuthMenuMap[v],
				})
			}
		}
	}

	//将权限赋值给permission

	info := forms.UserMeReq{
		ID:          user.ID,
		UserName:    user.UserName,
		RoleId:      user.RoleID,
		Avatar:      user.Avatar,
		Nickname:    user.NickName,
		Permissions: permissions,
		IsSuper:     config.IsSuperRole(user.RoleID),
	}

	c.JSON(200, serializer.Suc(info, "获取成功"))
}

func UserLogout(c *gin.Context) {
	encodeToken := token.GetAuthorization(c)
	if encodeToken == "" {
		c.JSON(200, serializer.CheckLogin())
		return
	}
	bytesT, err := base64.URLEncoding.DecodeString(encodeToken)
	if err != nil {
		logrus.Warningf("middleware base64.URLEncoding.DecodeString:%+v", err.Error())
		c.JSON(200, serializer.CheckLogin())
		return
	}
	t := string(bytesT)
	resp, err := config.GetIamClient().DeleteToken(c, &iam.DeleteTokenReq{
		Token: t,
	})
	if err != nil {
		logrus.Warningf("middleware config.GetIamClient().DeleteToken:%+v", err.Error())
		c.JSON(200, serializer.CheckLogin())
		return
	}
	if resp.Code != 0 {
		logrus.Warningf("middleware config.GetIamClient().DeleteToken code:%+v,msg:%+v", resp.Code, resp.Msg)
		c.JSON(200, serializer.CheckLogin())
		return
	}

	c.JSON(200, serializer.Suc(nil, "退出成功"))
}

//func AdminUserList(c *gin.Context) {
//	var req forms.AdminUserListReq
//	if err := c.ShouldBind(&req); err != nil {
//		c.JSON(200, ErrorResponse(err))
//		return
//	}
//	if err := forms.ParseParams(&req); err != nil {
//		c.JSON(200, ErrorResponse(err))
//		return
//	}
//
//	c.JSON(200, service.User.List(c, req))
//}

//func AdminUserEdit(c *gin.Context) {
//	var req forms.AdminUserEditReq
//	if err := c.ShouldBind(&req); err != nil {
//		c.JSON(200, ErrorResponse(err))
//		return
//	}
//	if err := forms.ParseParams(&req); err != nil {
//		c.JSON(200, ErrorResponse(err))
//		return
//	}
//
//	c.JSON(200, service.User.Edit(c, req))
//}

//func ResetPassword(c *gin.Context) {
//	var req forms.AdminUserResetPasswordReq
//	if err := c.ShouldBind(&req); err != nil {
//		c.JSON(200, ErrorResponse(err))
//		return
//	}
//	if err := forms.ParseParams(&req); err != nil {
//		c.JSON(200, ErrorResponse(err))
//		return
//	}
//
//	c.JSON(200, service.User.ResetPassword(c, req))
//}
//
//func UpdatePassword(c *gin.Context) {
//	var req forms.AdminUserUpdatePasswordReq
//	if err := c.ShouldBind(&req); err != nil {
//		c.JSON(200, ErrorResponse(err))
//		return
//	}
//	if err := forms.ParseParams(&req); err != nil {
//		c.JSON(200, ErrorResponse(err))
//		return
//	}
//
//	c.JSON(200, service.User.UpdatePassword(c, req))
//}
//
//func RolePermission(c *gin.Context) {
//	is, err := service.User.GetUserRolePermission(c)
//	if err != nil {
//		c.JSON(200, ErrorResponse(err))
//		return
//	}
//	c.JSON(200, serializer.Suc(is, "获取成功"))
//}
