package controller

import (
	"nms_nbi/src/framework/config"
	"nms_nbi/src/framework/constants/admin"
	"nms_nbi/src/framework/constants/uploadsubpath"
	"nms_nbi/src/framework/i18n"
	"nms_nbi/src/framework/utils/crypto"
	"nms_nbi/src/framework/utils/ctx"
	"nms_nbi/src/framework/utils/file"
	"nms_nbi/src/framework/utils/parse"
	"nms_nbi/src/framework/utils/regular"
	"nms_nbi/src/framework/utils/token"
	"nms_nbi/src/framework/vo/result"
	"nms_nbi/src/modules/system/model"
	"nms_nbi/src/modules/system/service"

	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
)

// 实例化控制层 SysProfileController 结构体
var NewSysProfile = &SysProfileController{
	sysUserService: service.NewSysUserImpl,
	sysRoleService: service.NewSysRoleImpl,
	sysPostService: service.NewSysPostImpl,
	sysMenuService: service.NewSysMenuImpl,
}

// 个人信息
//
// PATH /system/user/profile
type SysProfileController struct {
	// 用户服务
	sysUserService service.ISysUser
	// 角色服务
	sysRoleService service.ISysRole
	// 岗位服务
	sysPostService service.ISysPost
	// 菜单服务
	sysMenuService service.ISysMenu
}

// 个人信息
//
// GET /
func (s *SysProfileController) Info(c *gin.Context) {
	language := ctx.AcceptLanguage(c)
	loginUser, err := ctx.LoginUser(c)
	if err != nil {
		c.JSON(401, result.CodeMsg(401, i18n.TKey(language, err.Error())))
		return
	}

	// 查询用户所属角色组
	roleGroup := []string{}
	roles := s.sysRoleService.SelectRoleListByUserId(loginUser.UserID)
	for _, role := range roles {
		roleGroup = append(roleGroup, i18n.TKey(language, role.RoleName))
	}
	isAdmin := config.IsAdmin(loginUser.UserID)
	if isAdmin {
		roleGroup = append(roleGroup, i18n.TKey(language, "role.admin"))
	}

	// 查询用户所属岗位组
	postGroup := []string{}
	posts := s.sysPostService.SelectPostListByUserId(loginUser.UserID)
	for _, post := range posts {
		postGroup = append(postGroup, i18n.TKey(language, post.PostName))
	}

	loginUser.User.NickName = i18n.TKey(language, loginUser.User.NickName)
	loginUser.User.Remark = i18n.TKey(language, loginUser.User.Remark)
	loginUser.User.Dept.DeptName = i18n.TKey(language, loginUser.User.Dept.DeptName)
	for ri := range loginUser.User.Roles {
		loginUser.User.Roles[ri].RoleName = i18n.TKey(language, loginUser.User.Roles[ri].RoleName)
	}
	c.JSON(200, result.OkData(map[string]any{
		"user":      loginUser.User,
		"roleGroup": parse.RemoveDuplicates(roleGroup),
		"postGroup": parse.RemoveDuplicates(postGroup),
	}))
}

// 个人信息修改
//
// PUT /
func (s *SysProfileController) UpdateProfile(c *gin.Context) {
	language := ctx.AcceptLanguage(c)
	var body struct {
		// 昵称
		NickName string `json:"nickName" binding:"required"`
		// 性别
		Sex string `json:"sex" binding:"required"`
		// 手机号
		PhoneNumber string `json:"phonenumber"`
		// 邮箱
		Email string `json:"email"`
	}
	err := c.ShouldBindBodyWith(&body, binding.JSON)
	if err != nil || body.Sex == "" {
		c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
		return
	}

	// 登录用户信息
	loginUser, err := ctx.LoginUser(c)
	if err != nil {
		c.JSON(401, result.CodeMsg(401, i18n.TKey(language, err.Error())))
		return
	}
	userId := loginUser.UserID
	userName := loginUser.User.UserName

	// 检查手机号码格式并判断是否唯一
	if body.PhoneNumber != "" {
		if regular.ValidMobile(body.PhoneNumber) {
			uniquePhone := s.sysUserService.CheckUniquePhone(body.PhoneNumber, userId)
			if !uniquePhone {
				// 修改用户【%s】失败，手机号码已存在
				msg := i18n.TTemplate(language, "user.errPhoneExists", map[string]any{"name": userName})
				c.JSON(200, result.ErrMsg(msg))
				return
			}
		} else {
			// 修改用户【%s】失败，手机号码格式错误
			msg := i18n.TTemplate(language, "user.errPhoneFormat", map[string]any{"name": userName})
			c.JSON(200, result.ErrMsg(msg))
			return
		}
	}

	// 检查邮箱格式并判断是否唯一
	if body.Email != "" {
		if regular.ValidEmail(body.Email) {
			uniqueEmail := s.sysUserService.CheckUniqueEmail(body.Email, userId)
			if !uniqueEmail {
				// 修改用户【%s】失败，邮箱已存在
				msg := i18n.TTemplate(language, "user.errEmailExists", map[string]any{"name": userName})
				c.JSON(200, result.ErrMsg(msg))
				return
			}
		} else {
			// 修改用户【%s】失败，邮箱格式错误
			msg := i18n.TTemplate(language, "user.errEmailFormat", map[string]any{"name": userName})
			c.JSON(200, result.ErrMsg(msg))
			return
		}
	}

	// 用户基本资料
	sysUser := model.SysUser{
		UserID:      userId,
		UpdateBy:    userName,
		NickName:    body.NickName,
		PhoneNumber: body.PhoneNumber,
		Email:       body.Email,
		Sex:         body.Sex,
	}
	rows := s.sysUserService.UpdateUser(sysUser)
	if rows > 0 {
		// 更新缓存用户信息
		loginUser.User = s.sysUserService.SelectUserByUserName(userName)
		// 用户权限组标识
		isAdmin := config.IsAdmin(sysUser.UserID)
		if isAdmin {
			loginUser.Permissions = []string{admin.PERMISSION}
		} else {
			perms := s.sysMenuService.SelectMenuPermsByUserId(sysUser.UserID)
			loginUser.Permissions = parse.RemoveDuplicates(perms)
		}
		// 刷新令牌信息
		token.Cache(&loginUser)

		c.JSON(200, result.Ok(nil))
		return
	}
	c.JSON(200, result.Err(nil))
}

// 个人重置密码
//
// PUT /updatePwd
func (s *SysProfileController) UpdatePwd(c *gin.Context) {
	language := ctx.AcceptLanguage(c)
	var body struct {
		// 旧密码
		OldPassword string `json:"oldPassword" binding:"required"`
		// 新密码
		NewPassword string `json:"newPassword" binding:"required"`
	}
	err := c.ShouldBindBodyWith(&body, binding.JSON)
	if err != nil {
		c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
		return
	}

	// 登录用户信息
	loginUser, err := ctx.LoginUser(c)
	if err != nil {
		c.JSON(401, result.CodeMsg(401, i18n.TKey(language, err.Error())))
		return
	}
	userId := loginUser.UserID
	userName := loginUser.User.UserName

	// 查询当前登录用户信息得到密码值
	user := s.sysUserService.SelectUserById(userId)
	if user.UserID != userId {
		// 没有可访问用户数据！
		c.JSON(200, result.ErrMsg(i18n.TKey(language, "user.noData")))
		return
	}

	// 检查匹配用户密码
	oldCompare := crypto.BcryptCompare(body.OldPassword, user.Password)
	if !oldCompare {
		// 修改密码失败，旧密码错误
		c.JSON(200, result.ErrMsg(i18n.TKey(language, "user.errPasswdOld")))
		return
	}
	newCompare := crypto.BcryptCompare(body.NewPassword, user.Password)
	if newCompare {
		// 新密码不能与旧密码相同
		c.JSON(200, result.ErrMsg(i18n.TKey(language, "user.errPasswdEqOld")))
		return
	}

	// 修改新密码
	sysUser := model.SysUser{
		UserID:   userId,
		UpdateBy: userName,
		Password: body.NewPassword,
	}
	rows := s.sysUserService.UpdateUser(sysUser)
	if rows > 0 {
		c.JSON(200, result.Ok(nil))
		return
	}
	c.JSON(200, result.Err(nil))
}

// 个人头像上传
//
// POST /avatar
func (s *SysProfileController) Avatar(c *gin.Context) {
	language := ctx.AcceptLanguage(c)
	formFile, err := c.FormFile("file")
	if err != nil {
		c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
		return
	}

	// 上传文件转存
	filePath, err := file.TransferUploadFile(formFile, uploadsubpath.AVATART, []string{".jpg", ".jpeg", ".png"})
	if err != nil {
		c.JSON(200, result.ErrMsg(err.Error()))
		return
	}

	// 登录用户信息
	loginUser, err := ctx.LoginUser(c)
	if err != nil {
		c.JSON(401, result.CodeMsg(401, i18n.TKey(language, err.Error())))
		return
	}

	// 更新头像地址
	sysUser := model.SysUser{
		UserID:   loginUser.UserID,
		UpdateBy: loginUser.User.UserName,
		Avatar:   filePath,
	}
	rows := s.sysUserService.UpdateUser(sysUser)
	if rows > 0 {
		// 更新缓存用户信息
		loginUser.User = s.sysUserService.SelectUserByUserName(loginUser.User.UserName)
		// 用户权限组标识
		isAdmin := config.IsAdmin(sysUser.UserID)
		if isAdmin {
			loginUser.Permissions = []string{admin.PERMISSION}
		} else {
			perms := s.sysMenuService.SelectMenuPermsByUserId(sysUser.UserID)
			loginUser.Permissions = parse.RemoveDuplicates(perms)
		}
		// 刷新令牌信息
		token.Cache(&loginUser)

		c.JSON(200, result.OkData(filePath))
		return
	}
	c.JSON(200, result.Err(nil))
}
