package home

import (
	"LAPP_ACURA_MOM_BACKEND/global"
	model "LAPP_ACURA_MOM_BACKEND/models/base"
	"LAPP_ACURA_MOM_BACKEND/utils"
	"LAPP_ACURA_MOM_BACKEND/web/middleware/jwts"
	"LAPP_ACURA_MOM_BACKEND/web/supports"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/core/router"
)

/******************************************************************************
 *
 * @Function Name            : RegisterLogin
 *-----------------------------------------------------------------------------
 *
 * @Description              : 登录接口
 *
 * @Function Parameters      : 路由分组
 *
 * @Function Parameters      : HTTP方法
 *
 * @Function Parameters      : 路径
 *
 * @Function Parameters      : 实际处理请求的方法
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 15:04:54
 *
 ******************************************************************************/
func RegisterLogin(party router.Party, httpMethod string, path string, method func(int, string) (*model.User, error)) {

	party.Handle(httpMethod, path, func(ctx iris.Context) {
		var err error = nil
		entity := new(model.User)
		if ctx.GetContentLength() > 0 {
			err := ctx.ReadJSON(entity)
			if err != nil {
				supports.Error(ctx, iris.StatusBadRequest, err.Error(), nil)
				return
			}
		} else {
			entity = nil
			supports.Error(ctx, iris.StatusBadRequest, "参数缺少！", nil)
			return
		}
		mUser, err := method(entity.PlantNr, entity.UserId)
		if err != nil {
			supports.Error(ctx, iris.StatusBadRequest, err.Error(), nil)
			return
		}

		if mUser == nil { // 用户名不正确
			supports.Error(ctx, iris.StatusPaymentRequired, supports.UsernameFailur, nil)
			return
		}

		ckPassword, err := utils.CompareHashAndPassword(mUser.Password, entity.Password)
		if !ckPassword {
			supports.Error(ctx, iris.StatusPaymentRequired, supports.PasswordFailur, nil)
			return
		}
		gobalUser := new(global.User)
		gobalUser.UserId = mUser.UserId
		gobalUser.Role = mUser.Role
		gobalUser.RoleId = mUser.RoleId
		gobalUser.PlantNr = mUser.PlantNr
		token, err := jwts.GenerateToken(gobalUser)
		if err != nil {
			supports.Error(ctx, iris.StatusInternalServerError, supports.TokenCreateFailur, nil)
			return
		}
		data := make(map[string]string)
		data["token"] = token
		supports.Ok(ctx, supports.LoginSuccess, data)
	})
}

/******************************************************************************
 *
 * @Function Name            : RegisterDynamicMenu
 *-----------------------------------------------------------------------------
 *
 * @Description              : 动态获取路由
 *
 * @Function Parameters      : 路由分组
 *
 * @Function Parameters      : 路径
 *
 * @Function Parameters      : 实际处理请求的方法
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 15:04:54
 *
 ******************************************************************************/
func RegisterDynamicMenu(party router.Party, httpMethod string, path string, method func(*global.User) ([]model.Menu, error)) {

	party.Handle(httpMethod, path, func(ctx iris.Context) {
		user, ok := jwts.ParseToken(ctx)
		if !ok {
			return
		}

		result, err := method(user)
		if err != nil {
			supports.Error(ctx, iris.StatusBadRequest, err.Error(), nil)
			return
		}
		supports.Ok(ctx, supports.OptionSuccess, result)
	})
}

/******************************************************************************
 *
 * @Function Name            : RegisterSelectPlant
 *-----------------------------------------------------------------------------
 *
 * @Description              : 为一个查询Plant的方法注册路由
 *
 * @Function Parameters      : 路由分组
 *
 * @Function Parameters      : 路径
 *
 * @Function Parameters      : 实际处理请求的方法
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-18 22:49:12
 *
 ******************************************************************************/
func RegisterSelectPlants(party router.Party, httpMethod, path string, method func() ([]model.Plant, error)) {

	party.Handle(httpMethod, path, func(ctx iris.Context) {
		result, err := method()
		if err != nil {
			supports.Error(ctx, iris.StatusBadRequest, err.Error(), nil)
			return
		}
		supports.Ok(ctx, supports.OptionSuccess, result)
	})
}

/******************************************************************************
 *
 * @Function Name            : RegisterGetUserInfo
 *-----------------------------------------------------------------------------
 *
 * @Description              : 为一个查询Plant的方法注册路由
 *
 * @Function Parameters      : 路由分组
 *
 * @Function Parameters      : 路径
 *
 * @Function Parameters      : 实际处理请求的方法
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-18 22:49:12
 *
 ******************************************************************************/
func RegisterGetUserInfo(party router.Party, httpMethod, path string, method func(*global.User, string) (*model.User, error)) {

	party.Handle(httpMethod, path, func(ctx iris.Context) {
		user, ok := jwts.ParseToken(ctx)
		if !ok {
			return
		}

		mUser, err := method(user, user.UserId)
		if err != nil {
			supports.Error(ctx, iris.StatusBadRequest, err.Error(), nil)
			return
		}

		var mp = make(map[string]interface{})
		var roles = make([]string, 1)
		roles[0] = user.Role
		var permissions = make([]string, 1)
		permissions[0] = "*:*:*"
		mp["permissions"] = permissions
		mp["introduction"] = " am a super administrator"
		mp["avatar"] = "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif"
		if mUser.Avatar != "" {
			mp["avatar"] = mUser.Avatar
		}
		mp["name"] = mUser.Name
		mp["userId"] = mUser.UserId
		mp["roleId"] = mUser.RoleId
		mp["roleName"] = mUser.Role
		mp["nickName"] = mUser.NickName
		supports.Ok(ctx, supports.Success, mp)
	})
}
