package controller

import (
	"crypto/sha1"
	"fmt"
	"github.com/gin-gonic/gin"
	"io"
	"iot_manager_service/app/middleware"
	"iot_manager_service/app/system/dao"
	"iot_manager_service/app/system/model"
	"iot_manager_service/app/system/service"
	"iot_manager_service/util/common"
	"math"
	"net/http"
	"strconv"
	"strings"
)

var User = new(user)

type user struct{}

func (c *user) GetDetail(ctx *gin.Context) {
	id := ctx.Query("id")
	iId, err := strconv.ParseInt(id, 10, 64)
	if err != nil {
		ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
		return
	}
	detail, err := service.UserService.Get(iId)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, detail))

}

func (c *user) List(ctx *gin.Context) {
	current, _ := strconv.Atoi(ctx.Query("current"))
	size, _ := strconv.Atoi(ctx.Query("size"))
	account := ctx.Query("account")
	realName := ctx.Query("realName")
	if current == 0 {
		current = 1
	}
	if size <= 0 || size > 100 {
		size = 10
	}

	users, counts, err := service.UserService.List(account, realName, current, size)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	pages := int(math.Ceil(float64(counts) / float64(size)))
	rsp := model.RsqUserList{
		Current: current,
		Size:    size,
		Total:   counts,
		Pages:   pages,
		Records: users,
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, rsp))
}

func (c *user) Info(ctx *gin.Context) {
	authorization := ctx.GetHeader("Authorization")
	claims := middleware.ParseAccessToken(authorization)
	var userInfo = &dao.User{ID: claims.UserId}

	err := service.UserService.Info(userInfo)

	if err == nil {
		ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, userInfo))
	} else {
		ctx.JSON(http.StatusOK, err)
	}
}

func (c *user) Submit(ctx *gin.Context) {
	var req dao.User
	if err := ctx.ShouldBindJSON(&req); err != nil {
		ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
		return
	}
	//判断Account是否存在
	if c.IsExist(req) {
		ctx.JSON(http.StatusOK, common.FailResponse("账号已存在!", nil))
		return
	}
	// 密码sha1加密
	t := sha1.New()
	_, _ = io.WriteString(t, req.Password)
	password := fmt.Sprintf("%x", t.Sum(nil))
	req.Password = password

	err := service.UserService.Submit(req)
	if err == nil {
		ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, nil))
	} else {
		ctx.JSON(http.StatusOK, err)
	}
}

func (c *user) Update(ctx *gin.Context) {
	var req dao.User
	if err := ctx.ShouldBindJSON(&req); err != nil {
		ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
		return
	}
	err := service.UserService.Update(req)
	if err == nil {
		ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, nil))
	} else {
		ctx.JSON(http.StatusOK, err)
	}
}

func (c *user) Remove(ctx *gin.Context) {
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)

	var req *model.ReqUserRemove
	if err := ctx.ShouldBindJSON(&req); err != nil {
		ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
		return
	}
	err := service.UserService.Remove(claims.UserId, req.IDs)
	if err == nil {
		ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, nil))
	} else {
		ctx.JSON(http.StatusOK, err)
	}
}

func (c *user) ResetPwd(ctx *gin.Context) {
	var req *model.ReqUserResetPwd
	if err := ctx.ShouldBindJSON(&req); err != nil {
		ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
		return
	}
	err := service.UserService.ResetPwd(req.IDs)
	ctx.JSON(http.StatusOK, err)
}

func (c *user) GetList(ctx *gin.Context) {
	users, err := service.UserService.GetList()
	if err != nil {
		ctx.JSON(http.StatusOK, common.FailResponse(err.Error(), nil))
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, users))
}

func (c *user) Grant(ctx *gin.Context) {
	var req *model.ReqUserGrant
	if err := ctx.ShouldBindJSON(&req); err != nil {
		ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
		return
	}
	uIds := strings.Split(req.UserIds, ",")
	rIds := strings.Split(req.RoleIds, ",")
	if len(uIds) < 1 || len(rIds) < 1 {
		ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(model.GrantInvalid, nil))
		return
	}
	err := service.UserService.Grant(uIds, req.RoleIds)
	ctx.JSON(http.StatusOK, err)
}

func (c *user) IsExist(user dao.User) bool {
	return service.UserService.IsExist(user)
}

func (c *user) PutFile(ctx *gin.Context) {
	file, err := ctx.FormFile("file")
	if err != nil {
		ctx.JSON(http.StatusOK, common.FailResponse(err.Error(), nil))
		return
	}
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)

	link := service.UserService.UpAvatar(claims.TenantId, claims.UserId, file)
	if link == nil {
		ctx.JSON(http.StatusOK, common.FailResponse("图片上传失败", nil))
	} else {
		ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, link))
	}
}
